1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - simple-1
3   File:     main.c
4 
5   Copyright 2005-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:: 2007-11-15#$
14   $Rev: 2414 $
15   $Author: hatamoto_minoru $
16  *---------------------------------------------------------------------------*/
17 
18 /*---------------------------------------------------------------------------*
19     This sample runs chance encounter communications over a wireless connection.
20     It automatically connects to the simple-1 demo in the area.
21  *---------------------------------------------------------------------------*/
22 
23 #include <nitro.h>
24 
25 #include <nitro/wxc.h>
26 #include "font.h"
27 #include "user.h"
28 #include "print.h"
29 
30 /*---------------------------------------------------------------------------*
31     Constant Definitions
32  *---------------------------------------------------------------------------*/
33 #define     KEY_REPEAT_START    25     // Number of frames until key repeat starts
34 #define     KEY_REPEAT_SPAN     10     // Number of frames between key repeats
35 
36 
37 /*---------------------------------------------------------------------------*
38     Structure Definitions
39  *---------------------------------------------------------------------------*/
40 // Key input data
41 typedef struct KeyInfo
42 {
43     u16     cnt;                       // Unprocessed input value
44     u16     trg;                       // Push trigger input
45     u16     up;                        // Release trigger input
46     u16     rep;                       // Press and hold repeat input
47 
48 }
49 KeyInfo;
50 
51 /*---------------------------------------------------------------------------*
52     Internal Function Definitions
53  *---------------------------------------------------------------------------*/
54 static void ModeSelect(void);          // Parent/child select screen
55 static void ModeError(void);           // Error display screen
56 static void VBlankIntr(void);          // V-Blank interrupt handler
57 
58 
59 
60 // General purpose subroutines
61 static void KeyRead(KeyInfo * pKey);
62 static void InitializeAllocateSystem(void);
63 
64 
65 /*---------------------------------------------------------------------------*
66     Internal Variable Definitions
67  *---------------------------------------------------------------------------*/
68 static KeyInfo gKey;                   // Key input
69 static s32 gFrame;                     // Frame counter
70 
71 u16 gScreen[32 * 32];           // Virtual screen
72 
73 
74 /*---------------------------------------------------------------------------*
75   Name:         NitroMain
76 
77   Description:  Initialization and main loop.
78 
79   Arguments:    None.
80 
81   Returns:      None.
82  *---------------------------------------------------------------------------*/
NitroMain(void)83 void NitroMain(void)
84 {
85     // Various types of initialization
86     OS_Init();
87     OS_InitTick();
88 
89     FX_Init();
90 
91     /* Initializes display settings */
92     {
93         GX_Init();
94         GX_DispOff();
95         GXS_DispOff();
96         /* Clears VRAM. */
97         GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
98         MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
99         (void)GX_DisableBankForLCDC();
100         MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
101         MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
102         MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);
103         MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);
104         /*
105          * VRAM-A  = main-background
106          * VRAM-B  = main-OBJ
107          * VRAM-HI = sub-background
108          * VRAM-J  = sub-OBJ
109          */
110         GX_SetBankForBG(GX_VRAM_BG_128_A);
111         GX_SetBankForOBJ(GX_VRAM_OBJ_128_B);
112         GX_SetBankForSubBG(GX_VRAM_SUB_BG_48_HI);
113         GX_SetBankForSubOBJ(GX_VRAM_SUB_OBJ_128_D);
114         /*
115          * Main screen:
116          *   BG0 = text-background
117          *   OBJ = 2D mode
118          */
119         GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
120         GX_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ);
121         GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
122         G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, GX_BG_COLORMODE_16,
123                          GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x00000,
124                          GX_BG_EXTPLTT_01);
125         G2_SetBG0Priority(0);
126         G2_BG0Mosaic(FALSE);
127         G2_SetBG0Offset(0, 0);
128         GX_LoadBG0Char(d_CharData, 0, sizeof(d_CharData));
129         GX_LoadBGPltt(d_PaletteData, 0, sizeof(d_PaletteData));
130         /*
131          * Sub-screen:
132          *   BG0 = text-background
133          *   OBJ = 2D mode
134          */
135         GXS_SetGraphicsMode(GX_BGMODE_0);
136         GXS_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ);
137         GXS_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
138         G2S_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, GX_BG_COLORMODE_16,
139                           GX_BG_SCRBASE_0xb800, GX_BG_CHARBASE_0x00000,
140                           GX_BG_EXTPLTT_01);
141         G2S_SetBG0Priority(0);
142         G2S_BG0Mosaic(FALSE);
143         G2S_SetBG0Offset(0, 0);
144         GXS_LoadBG0Char(d_CharData, 0, sizeof(d_CharData));
145         GXS_LoadBGPltt(d_PaletteData, 0, sizeof(d_PaletteData));
146         /* Enable display */
147         GX_DispOn();
148         GXS_DispOn();
149     }
150     /* V-Blank settings */
151     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
152     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
153     (void)OS_EnableIrq();
154     (void)OS_EnableInterrupts();
155     (void)GX_VBlankIntr(TRUE);
156 
157     /* Memory allocation initialization */
158     InitializeAllocateSystem();
159 
160     /* Main loop */
161     for (gFrame = 0; TRUE; gFrame++)
162     {
163         /* Get key input data */
164         KeyRead(&gKey);
165 
166         // Clear the screen
167         ClearStringY(1);
168         ClearStringY(2);
169 
170         /* Distributes processes based on communication status */
171         switch (WXC_GetStateCode())
172         {
173         case WXC_STATE_END:
174             ModeSelect();
175             break;
176         case WXC_STATE_ENDING:
177             break;
178         case WXC_STATE_ACTIVE:
179             if(WXC_IsParentMode() == TRUE)
180             {
181 
182                 PrintString(9, 2, 0xf, "Now parent...");
183             }
184             else
185             {
186                 PrintString(9, 2, 0xf, "Now child...");
187             }
188             break;
189         }
190         if (gKey.trg & PAD_BUTTON_START)
191         {
192             (void)WXC_End();
193         }
194 
195         // Waiting for the V-Blank
196         OS_WaitVBlankIntr();
197     }
198 }
199 
200 /*---------------------------------------------------------------------------*
201   Name:         ModeSelect
202 
203   Description:  Processing in parent/child selection screen.
204 
205   Arguments:    None.
206 
207   Returns:      None.
208  *---------------------------------------------------------------------------*/
ModeSelect(void)209 static void ModeSelect(void)
210 {
211     PrintString(3, 1, 0xf, "Push A to start");
212 
213     if (gKey.trg & PAD_BUTTON_A)
214     {
215         User_Init();
216     }
217 }
218 
219 /*---------------------------------------------------------------------------*
220   Name:         ModeError
221 
222   Description:  Processing in error display screen.
223 
224   Arguments:    None.
225 
226   Returns:      None.
227  *---------------------------------------------------------------------------*/
ModeError(void)228 static void ModeError(void)
229 {
230     PrintString(5, 10, 0x1, "======= ERROR! =======");
231     PrintString(5, 13, 0xf, " Fatal error occured.");
232     PrintString(5, 14, 0xf, "Please reboot program.");
233 }
234 
235 /*---------------------------------------------------------------------------*
236   Name:         VBlankIntr
237 
238   Description:  V-Blank interrupt handler.
239 
240   Arguments:    None.
241 
242   Returns:      None.
243  *---------------------------------------------------------------------------*/
VBlankIntr(void)244 static void VBlankIntr(void)
245 {
246     DC_FlushRange(&(gScreen), sizeof(gScreen));
247     GXS_LoadBG0Scr(&(gScreen), 0, sizeof(gScreen));
248 
249     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
250 }
251 
252 /*---------------------------------------------------------------------------*
253   Name:         KeyRead
254 
255   Description:  Edits key input data.
256                 Detects press trigger, release trigger, and press-and-hold repeat.
257 
258   Arguments:    pKey: Structure that holds key input data to be edited
259 
260   Returns:      None.
261  *---------------------------------------------------------------------------*/
KeyRead(KeyInfo * pKey)262 static void KeyRead(KeyInfo * pKey)
263 {
264     static u16 repeat_count[12];
265     int     i;
266     u16     r;
267 
268     r = PAD_Read();
269     pKey->trg = 0x0000;
270     pKey->up = 0x0000;
271     pKey->rep = 0x0000;
272 
273     for (i = 0; i < 12; i++)
274     {
275         if (r & (0x0001 << i))
276         {
277             if (!(pKey->cnt & (0x0001 << i)))
278             {
279                 pKey->trg |= (0x0001 << i);     // Press trigger
280                 repeat_count[i] = 1;
281             }
282             else
283             {
284                 if (repeat_count[i] > KEY_REPEAT_START)
285                 {
286                     pKey->rep |= (0x0001 << i); // Press-and-hold repeat
287                     repeat_count[i] = KEY_REPEAT_START - KEY_REPEAT_SPAN;
288                 }
289                 else
290                 {
291                     repeat_count[i]++;
292                 }
293             }
294         }
295         else
296         {
297             if (pKey->cnt & (0x0001 << i))
298             {
299                 pKey->up |= (0x0001 << i);      // Release trigger
300             }
301         }
302     }
303     pKey->cnt = r;                     // Unprocessed key input
304 }
305 
306 /*---------------------------------------------------------------------------*
307   Name:         InitializeAllocateSystem
308 
309   Description:  Initializes the memory allocation system in the main memory arena.
310 
311   Arguments:    None.
312 
313   Returns:      None.
314  *---------------------------------------------------------------------------*/
InitializeAllocateSystem(void)315 static void InitializeAllocateSystem(void)
316 {
317     void   *tempLo;
318     OSHeapHandle hh;
319 
320     OS_Printf(" arena lo = %08x\n", OS_GetMainArenaLo());
321     OS_Printf(" arena hi = %08x\n", OS_GetMainArenaHi());
322 
323     tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
324     OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
325     hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
326     if (hh < 0)
327     {
328         OS_Panic("ARM9: Fail to create heap...\n");
329     }
330     hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
331 }
332 
333 
334 /*---------------------------------------------------------------------------*
335   End of file
336  *---------------------------------------------------------------------------*/
337