1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - nandApp - demos - simple
3   File:     main.c
4 
5   Copyright 2007-2008 Nintendo. 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   $Date:: 2009-02-06#$
14   $Rev: 9986 $
15   $Author: yosizaki $
16  *---------------------------------------------------------------------------*/
17 #include <twl.h>
18 #include <DEMO.h>
19 
20 static void PrintBootType();
21 
22 static void InitDEMOSystem();
23 static void InitInteruptSystem();
24 static void InitAllocSystem();
25 static void InitFileSystem();
26 
27 // Hook function for OS_Printf
28 #ifndef SDK_FINALROM
OS_Printf(const char * fmt,...)29 void OS_Printf(const char *fmt, ...)
30 {
31     char dst[256];
32 
33     int     ret;
34     va_list va;
35     va_start(va, fmt);
36     ret = OS_VSPrintf(dst, fmt, va);
37     va_end(va);
38 
39     OS_PutString(dst);
40 }
41 #endif
42 
43 /*---------------------------------------------------------------------------*
44   Name:         TwlMain
45 
46   Description:  Main function.
47 
48   Arguments:    None.
49 
50   Returns:      None.
51  *---------------------------------------------------------------------------*/
TwlMain(void)52 void TwlMain(void)
53 {
54     OS_Init();
55     InitInteruptSystem();
56     InitFileSystem();
57     InitAllocSystem();
58     InitDEMOSystem();
59     OS_Printf("*** start nandApp demo\n");
60 
61     PrintBootType();
62 
63     // Load file from ROM and display it
64     // The ROM file system can also be used from NAND applications.
65     {
66         BOOL bSuccess;
67         FSFile f;
68         u32 fileSize;
69         s32 readSize;
70         void* pBuffer;
71 
72         FS_InitFile(&f);
73 
74         bSuccess = FS_OpenFileEx(&f, "build_time.txt", FS_FILEMODE_R);
75         SDK_ASSERT( bSuccess );
76 
77         fileSize = FS_GetFileLength(&f);
78         pBuffer = OS_Alloc(fileSize + 1);
79         SDK_POINTER_ASSERT(pBuffer);
80 
81         readSize = FS_ReadFile(&f, pBuffer, (s32)fileSize);
82         SDK_ASSERT( readSize == fileSize );
83 
84         bSuccess = FS_CloseFile(&f);
85         SDK_ASSERT( bSuccess );
86 
87         ((char*)pBuffer)[fileSize] = '\0';
88         OS_Printf("%s\n", pBuffer);
89         OS_Free(pBuffer);
90     }
91 
92     OS_Printf("*** End of demo\n");
93 
94     // Don't terminate because we want to be able to return to the launcher
95     //
96     for (;;)
97     {
98         // Frame update
99         {
100             DEMO_DrawFlip();
101             OS_WaitVBlankIntr();
102         }
103     }
104 
105     OS_Terminate();
106 }
107 
108 
109 /*---------------------------------------------------------------------------*
110   Name:         PrintBootType
111 
112   Description:  Prints the BootType.
113 
114   Arguments:    None.
115 
116   Returns:      None.
117  *---------------------------------------------------------------------------*/
PrintBootType()118 static void PrintBootType()
119 {
120     const OSBootType btype = OS_GetBootType();
121 
122     switch( btype )
123     {
124     case OS_BOOTTYPE_ROM:   OS_TPrintf("OS_GetBootType = OS_BOOTTYPE_ROM\n"); break;
125     case OS_BOOTTYPE_NAND:  OS_TPrintf("OS_GetBootType = OS_BOOTTYPE_NAND\n"); break;
126     default:
127         {
128             OS_Warning("unknown BootType(=%d)", btype);
129         }
130         break;
131     }
132 }
133 
134 /*---------------------------------------------------------------------------*
135   Name:         InitDEMOSystem
136 
137   Description:  Configures display settings for console screen output.
138 
139   Arguments:    None.
140 
141   Returns:      None.
142  *---------------------------------------------------------------------------*/
InitDEMOSystem()143 static void InitDEMOSystem()
144 {
145     // Initialize screen display
146     DEMOInitCommon();
147     DEMOInitVRAM();
148     DEMOInitDisplayBitmap();
149     DEMOHookConsole();
150     DEMOSetBitmapTextColor(GX_RGBA(31, 31, 0, 1));
151     DEMOSetBitmapGroundColor(DEMO_RGB_CLEAR);
152     DEMOStartDisplay();
153 }
154 
155 /*---------------------------------------------------------------------------*
156   Name:         InitInteruptSystem
157 
158   Description:  Initializes interrupts.
159 
160   Arguments:    None.
161 
162   Returns:      None.
163  *---------------------------------------------------------------------------*/
InitInteruptSystem()164 static void InitInteruptSystem()
165 {
166     // ESnable master interrupt flag
167     (void)OS_EnableIrq();
168 
169     // Allow IRQ interrupts
170     (void)OS_EnableInterrupts();
171 }
172 
173 /*---------------------------------------------------------------------------*
174   Name:         InitAllocSystem
175 
176   Description:  Creates a heap and makes OS_Alloc usable.
177 
178   Arguments:    None.
179 
180   Returns:      None.
181  *---------------------------------------------------------------------------*/
InitAllocSystem()182 static void InitAllocSystem()
183 {
184     void* newArenaLo;
185     OSHeapHandle hHeap;
186 
187     // Initialize the main arena's allocation system
188     newArenaLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
189     OS_SetMainArenaLo(newArenaLo);
190 
191     // Create a heap in the main arena
192     hHeap = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
193     (void)OS_SetCurrentHeap(OS_ARENA_MAIN, hHeap);
194 }
195 
196 /*---------------------------------------------------------------------------*
197   Name:         InitFileSystem
198 
199   Description:  Initializes the file system and makes the ROM accessible.
200                 The InitInteruptSystem function must have been called before this function is.
201 
202 
203   Arguments:    None.
204 
205   Returns:      None.
206  *---------------------------------------------------------------------------*/
InitFileSystem()207 static void InitFileSystem()
208 {
209     // Initialize file system
210     FS_Init( FS_DMA_NOT_USE );
211 }
212