1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - nandApp - demos - SubBanner
3   File:     main.c
4 
5   Copyright 2007-2009 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:: 2011-04-12#$
14   $Rev: 11403 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include <twl/na.h>
20 #include <DEMO.h>
21 
22 #define MENU_ELEMENT_NUM 4  // Number of menu items
23 #define MENU_TEXT_SIZE 8
24 
25 #define COLOR_WHITE GX_RGBA(31, 31, 31, 1)
26 #define COLOR_RED   GX_RGBA(31,  0,  0, 1)
27 #define COLOR_GREEN GX_RGBA( 0, 31,  0, 1)
28 #define COLOR_BLUE  GX_RGBA( 0,  0, 31, 1)
29 #define COLOR_BLACK GX_RGBA( 0,  0,  0, 1)
30 
31 // Menu element coordinates
32 typedef struct MenuPos
33 {
34     BOOL   enable;
35     int    x;
36     int    y;
37 }MenuPos;
38 
39 // Structure holding parameters that determine menu composition
40 typedef struct MenuParam
41 {
42     int      num;
43     GXRgb    normal_color;
44     GXRgb    select_color;
45     GXRgb    disable_color;
46     u16      padding;
47     MenuPos *pos;
48     const char **str_elem;
49 }MenuParam;
50 
51 static const char *pStrMenu[ MENU_ELEMENT_NUM ] =
52 {
53     "write sub banner file",
54     "edit sub banner file",
55     "delete sub banner file",
56     "return to launcher",
57 };
58 
59 static MenuPos menuPos[] =
60 {
61     { TRUE,  3,   8 },
62     { TRUE,  3,   9 },
63     { TRUE,  3,   10 },
64     { TRUE,  3,   11 },
65 };
66 
67 static const MenuParam menuParam =
68 {
69     MENU_ELEMENT_NUM,
70     COLOR_BLACK,
71     COLOR_GREEN,
72     COLOR_RED,
73     0,
74     &menuPos[ 0 ],
75     (const char **)&pStrMenu,
76 };
77 
78 static void PrintBootType();
79 static void InitDEMOSystem();
80 static void InitInteruptSystem();
81 static void InitAllocSystem();
82 static void InitFileSystem();
83 
84 #define MESSAGE_SIZE 50
85 
86 static char message[MESSAGE_SIZE]="";
87 
88 /*---------------------------------------------------------------------------*
89   Name:         EditSubBanner
90 
91   Description:  Switches the sub-banner's RGB colors and flips it in the horizontal direction.
92 
93   Arguments:    None.
94 
95   Returns:      None.
96  *---------------------------------------------------------------------------*/
EditSubBanner(NASubBanner * banner)97 static BOOL EditSubBanner(NASubBanner* banner)
98 {
99     int i;
100     NASubBannerAnime* anime = &banner->anime;
101 
102     if (!NA_LoadSubBanner(banner))
103     {
104         return FALSE;
105     }
106 
107     // Rotate the RGB values for the palette data
108     for (i = 0; i < NA_SUBBANNER_PALETTE_NUM; i++)
109     {
110         anime->pltt[0][i] = (GXRgb)((GX_RGB_R_MASK & anime->pltt[0][i]) << 5 |  // R to G
111                                     (GX_RGB_G_MASK & anime->pltt[0][i]) << 5 |  // G to B
112                                     (GX_RGB_B_MASK & anime->pltt[0][i]) >> 10); // B to R
113     }
114 
115     NA_MakeSubBannerHeader(banner);
116     return TRUE;
117 }
118 
119 /*---------------------------------------------------------------------------*
120   Name:         TwlMain
121 
122   Description:  Main function.
123 
124   Arguments:    None.
125 
126   Returns:      None.
127  *---------------------------------------------------------------------------*/
TwlMain(void)128 void TwlMain(void)
129 {
130     int cursor = 0;
131     int i;
132 
133     OS_Init();
134     InitInteruptSystem();
135     InitFileSystem();
136     InitAllocSystem();
137     InitDEMOSystem();
138 
139     // Print usage
140     PrintBootType();
141     OS_TPrintf("=================================\n");
142     OS_TPrintf("USAGE: SubBanner demo\n");
143     OS_TPrintf(" UP DOWN : select menu\n");
144     OS_TPrintf(" A       : execute menu\n");
145     OS_TPrintf("=================================\n");
146 
147     for (;;)
148     {
149         DEMOReadKey();
150         if( DEMO_IS_TRIG(PAD_KEY_DOWN) )
151         {   // Move cursor
152             if( ++cursor == MENU_ELEMENT_NUM )
153             {
154                 cursor=0;
155             }
156         }
157         if( DEMO_IS_TRIG(PAD_KEY_UP) )
158         {
159             if( --cursor & 0x80 )
160             {
161                 cursor=MENU_ELEMENT_NUM - 1;
162             }
163         }
164 
165         // Render the menu
166         {
167             const MenuParam *param = &menuParam;
168 
169             DEMOFillRect(0, 0, 256, 192, COLOR_WHITE);
170             DEMOSetBitmapGroundColor(COLOR_WHITE);
171             DEMOSetBitmapTextColor( COLOR_BLUE );
172             DEMODrawText( 1 * MENU_TEXT_SIZE, 0 * MENU_TEXT_SIZE, "SubBanner");
173 
174             for( i = 0; i < param->num; i++)
175             {
176                 DEMOSetBitmapTextColor( i == cursor ? param->select_color : param->normal_color );
177                 DEMODrawText( param->pos[i].x * MENU_TEXT_SIZE, param->pos[i].y * MENU_TEXT_SIZE,
178                             ( i == cursor ? "=>%s" : "  %s" ), param->str_elem[i] );
179             }
180 
181             DEMOSetBitmapTextColor( COLOR_RED );
182             DEMODrawText( 1 * MENU_TEXT_SIZE, 3 * MENU_TEXT_SIZE, message);
183         }
184 
185         if( DEMO_IS_TRIG(PAD_BUTTON_A) )
186         {    // Branch to menu items
187              switch( cursor )
188              {
189                  NASubBanner subBanner;
190                  FSFile file[1];
191                  s32 readLen;
192                case 0:
193                  //Write the sub-banner
194                  FS_InitFile(file);
195                  if (FS_OpenFileEx(file, "rom:/sub_banner.bnr", FS_FILEMODE_R) )
196                  {
197                      readLen = FS_ReadFile(file, &subBanner, sizeof(NASubBanner));
198                      (void)FS_CloseFile(file);
199                      if( readLen == sizeof(NASubBanner) )
200                      {
201                          // Success
202                          if ( NA_SaveSubBanner( &subBanner ) )
203                          {
204                              (void)STD_CopyLString( message, "write succeed.", MESSAGE_SIZE );
205                          }
206                          else
207                          {
208                              (void)STD_CopyLString( message, "write failed.", MESSAGE_SIZE );
209                          }
210                      }
211                      else
212                      {
213                          (void)STD_CopyLString( message, "read rom failed.", MESSAGE_SIZE );
214                      }
215                  }
216                  else
217                  {
218                      (void)STD_CopyLString( message, "open rom failed.", MESSAGE_SIZE );
219                  }
220                  break;
221                case 1:
222                  if (EditSubBanner( &subBanner ))
223                  {   // Success
224                      if ( NA_SaveSubBanner( &subBanner ) )
225                      {
226                          (void)STD_CopyLString( message, "edit succeed.", MESSAGE_SIZE );
227                      }
228                      else
229                      {
230                          (void)STD_CopyLString( message, "edit failed.", MESSAGE_SIZE );
231                      }
232                  }
233                  else
234                  {   // Failed
235                      (void)STD_CopyLString( message, "load failed.", MESSAGE_SIZE );
236                  }
237                  break;
238                case 2:
239                  //Delete the sub-banner
240                  if ( NA_DeleteSubBanner(&subBanner) )
241                  {
242                      (void)STD_CopyLString( message, "delete succeed", MESSAGE_SIZE );
243                  }
244                  else
245                  {
246                      (void)STD_CopyLString( message, "delete failed", MESSAGE_SIZE );
247                  }
248                  break;
249                case 3:
250                  (void)OS_JumpToSystemMenu();
251                  //Restart
252                  break;
253              }
254         }
255         DEMO_DrawFlip();
256         OS_WaitVBlankIntr();
257     }
258     OS_Terminate();
259 }
260 
261 /*---------------------------------------------------------------------------*
262   Name:         PrintBootType
263 
264   Description:  Prints the BootType.
265 
266   Arguments:    None.
267 
268   Returns:      None.
269  *---------------------------------------------------------------------------*/
PrintBootType()270 static void PrintBootType()
271 {
272     const OSBootType btype = OS_GetBootType();
273 
274     switch( btype )
275     {
276       case OS_BOOTTYPE_ROM:   OS_TPrintf("OS_GetBootType:OS_BOOTTYPE_ROM\n"); break;
277       case OS_BOOTTYPE_NAND:  OS_TPrintf("OS_GetBootType:OS_BOOTTYPE_NAND\n"); break;
278     default:
279         {
280             OS_Warning("unknown BootType(=%d)", btype);
281         }
282         break;
283     }
284 }
285 
286 /*---------------------------------------------------------------------------*
287   Name:         InitDEMOSystem
288 
289   Description:  Configures display settings for console screen output.
290 
291   Arguments:    None.
292 
293   Returns:      None.
294  *---------------------------------------------------------------------------*/
InitDEMOSystem()295 static void InitDEMOSystem()
296 {
297     // Initialize screen display
298     DEMOInitCommon();
299     DEMOInitVRAM();
300     DEMOInitDisplayBitmap();
301     DEMOHookConsole();
302     DEMOSetBitmapTextColor(GX_RGBA(31, 31, 0, 1));
303     DEMOSetBitmapGroundColor(DEMO_RGB_CLEAR);
304     DEMOStartDisplay();
305 }
306 
307 /*---------------------------------------------------------------------------*
308   Name:         InitInteruptSystem
309 
310   Description:  Initializes interrupts.
311 
312   Arguments:    None.
313 
314   Returns:      None.
315  *---------------------------------------------------------------------------*/
InitInteruptSystem()316 static void InitInteruptSystem()
317 {
318     // Enable master interrupt flag
319     (void)OS_EnableIrq();
320 
321     // Allow IRQ interrupts
322     (void)OS_EnableInterrupts();
323 }
324 
325 /*---------------------------------------------------------------------------*
326   Name:         InitAllocSystem
327 
328   Description:  Creates a heap and makes OS_Alloc usable.
329 
330   Arguments:    None.
331 
332   Returns:      None.
333  *---------------------------------------------------------------------------*/
InitAllocSystem()334 static void InitAllocSystem()
335 {
336     void* newArenaLo;
337     OSHeapHandle hHeap;
338 
339     // Initialize the main arena's allocation system
340     newArenaLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
341     OS_SetMainArenaLo(newArenaLo);
342 
343     // Create a heap in the main arena
344     hHeap = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
345     (void)OS_SetCurrentHeap(OS_ARENA_MAIN, hHeap);
346 }
347 
348 /*---------------------------------------------------------------------------*
349   Name:         InitFileSystem
350 
351   Description:  Initializes the file system and makes the ROM accessible.
352                 The InitInteruptSystem function must have been called before this function is.
353 
354 
355   Arguments:    None.
356 
357   Returns:      None.
358  *---------------------------------------------------------------------------*/
InitFileSystem()359 static void InitFileSystem()
360 {
361     // Initialize file system
362     FS_Init( FS_DMA_NOT_USE );
363 }
364