1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fs.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/fs.h>
17 #include "fs.h"
18 #include "applet.h"
19 
Initialize(demo::RenderSystemDrawing * p_RenderSystem,nn::fnd::ExpHeap * p_AppHeap)20 void FsDemo::Initialize(demo::RenderSystemDrawing* p_RenderSystem, nn::fnd::ExpHeap* p_AppHeap)
21 {
22     Device::Initialize(p_RenderSystem);
23 
24     mp_AppHeap = p_AppHeap;
25 }
26 
Finalize(void)27 void FsDemo::Finalize(void)
28 {
29     Device::Finalize();
30 }
31 
Start(void)32 void FsDemo::Start(void)
33 {
34     Device::Start();
35 
36     m_ExitEvent.Initialize(false);
37 
38     // Open the file
39     m_RomFsFile.Initialize(L"rom:/test.txt");
40 
41     // Create buffer to load file contents
42     s64 fileSize = m_RomFsFile.GetSize();
43     m_FileBuffer = reinterpret_cast<char*>(mp_AppHeap->Allocate(fileSize));
44 
45     m_Thread.Start(FsDemo::ThreadFunc, this, m_Stack);
46 }
47 
End(void)48 void FsDemo::End(void)
49 {
50     // Destroy the buffer
51     mp_AppHeap->Free(m_FileBuffer);
52 
53     m_ExitEvent.Signal();
54     m_Thread.Join();
55 
56     // Close the file
57     m_RomFsFile.Finalize();
58 
59     Device::End();
60 }
61 
DrawFrame(void)62 void FsDemo::DrawFrame(void)
63 {
64 }
65 
ThreadFuncImpl()66 void FsDemo::ThreadFuncImpl()
67 {
68     // Because file reads automatically stop in Sleep Mode, we do not need an exclusive lock of some kind.
69     //
70     while ( !m_ExitEvent.TryWait() )
71     {
72         TransitionHandler::Lock();
73         {
74             // Move to start of file
75             m_RomFsFile.SetPosition(0);
76 
77             // Copy file contents to buffer
78             s64 fileSize = m_RomFsFile.GetSize();
79             m_RomFsFile.Read(m_FileBuffer, fileSize);
80         }
81         TransitionHandler::Unlock();
82 
83         nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(100));
84     }
85 }
86 
ThreadFunc(FsDemo * pFsDemo)87 void FsDemo::ThreadFunc(FsDemo* pFsDemo)
88 {
89     pFsDemo->ThreadFuncImpl();
90 }
91 
92 
93 /*---------------------------------------------------------------------------*
94   End of file
95  *---------------------------------------------------------------------------*/
96