1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - simple-2
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  *---------------------------------------------------------------------------*/
21 
22 #include <nitro.h>
23 
24 #include <nitro/wxc.h>
25 #include "user.h"
26 #include "common.h"
27 #include "dispfunc.h"
28 
29 
30 /*---------------------------------------------------------------------------*
31     Constant Definitions
32  *---------------------------------------------------------------------------*/
33 /* GGID used for testing */
34 #define SDK_MAKEGGID_SYSTEM(num)              (0x003FFF00 | (num))
35 #define GGID_ORG_1                            SDK_MAKEGGID_SYSTEM(0x53)
36 #define GGID_ORG_2                            SDK_MAKEGGID_SYSTEM(0x54)
37 
38 /*---------------------------------------------------------------------------*
39     Internal Function Definitions
40  *---------------------------------------------------------------------------*/
41 static void ModeSelect(void);          // Parent/child select screen
42 static void ModeError(void);           // Error display screen
43 static void VBlankIntr(void);          // V-Blank interrupt handler
44 
45 // General purpose subroutines
46 static void InitializeAllocateSystem(void);
47 
48 static void Menu(void);
49 
50 /*---------------------------------------------------------------------------*
51     Internal Variable Definitions
52  *---------------------------------------------------------------------------*/
53 static s32 gFrame;                     // Frame counter
54 
55 /*---------------------------------------------------------------------------*
56     External Variable Definitions
57  *---------------------------------------------------------------------------*/
58 int passby_endflg;
59 int menu_flg;
60 
61 BOOL passby_ggid[GGID_MAX];     // GGID that does chance encounter
62 BOOL send_comp_flg;
63 
64 u16 keyData;
65 
66 /*---------------------------------------------------------------------------*
67   Name:         NitroMain
68 
69   Description:  Initialization and main loop.
70 
71   Arguments:    None.
72 
73   Returns:      None.
74  *---------------------------------------------------------------------------*/
NitroMain(void)75 void NitroMain(void)
76 {
77     int i;
78 
79     // Various types of initialization
80     OS_Init();
81     OS_InitTick();
82 
83     OS_InitAlarm();
84 
85     FX_Init();
86 
87     {                                  /* Rendering settings initialization */
88         GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
89         MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
90         (void)GX_DisableBankForLCDC();
91         MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
92         MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
93         MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);
94         MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);
95         BgInitForPrintStr();
96         GX_SetBankForOBJ(GX_VRAM_OBJ_128_B);
97         GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
98         GX_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ);
99         GXS_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ);
100         GX_LoadOBJ(sampleCharData, 0, sizeof(sampleCharData));
101         GX_LoadOBJPltt(samplePlttData, 0, sizeof(samplePlttData));
102         MI_DmaFill32(3, oamBak, 0xc0, sizeof(oamBak));
103     }
104 
105     /* V-Blank interrupt configuration */
106     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
107     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
108     (void)OS_EnableIrqMask(OS_IE_FIFO_RECV);
109     (void)OS_EnableIrq();
110     (void)OS_EnableInterrupts();
111     (void)GX_VBlankIntr(TRUE);
112 
113     // RTC initialization
114     RTC_Init();
115 
116     /* Start display */
117     GX_DispOn();
118     GXS_DispOn();
119     G2_SetBG0Offset(0, 0);
120     G2S_SetBG0Offset(0, 0);
121 
122     /* Memory allocation initialization */
123     InitializeAllocateSystem();
124 
125     // Debug string output
126     OS_Printf("ARM9: WM simple demo started.");
127 
128 
129     passby_endflg = 0;
130     menu_flg = MENU_MIN;
131     send_comp_flg = TRUE;
132     // Set all initial GGID values as capable of chance encounter
133     for(i = 0; i < GGID_MAX; i++)
134     {
135         passby_ggid[i] = TRUE;
136     }
137 
138     /* Main loop */
139     while(1)
140     {
141         OS_WaitVBlankIntr();
142 
143         keyData = ReadKeySetTrigger(PAD_Read());
144 
145         /* Display clear */
146         BgClear();
147 
148         // Chance encounter communications configuration screen
149         Menu();
150 
151         // Transition to chance encounter communications
152         if(menu_flg == 9)
153         {
154             int i;
155 
156             // Initialize only the selected GGID
157             for(i = 0; i < GGID_MAX; i++)
158             {
159                 if(passby_ggid[i])
160                 {
161                     /* Initializes, registers, and runs chance encounter communications */
162                     User_Init();
163                     break;
164                 }
165             }
166             if(i >= GGID_MAX)
167             {
168                 /* Starts an error message display */
169                 BgClear();
170                 BgPutString(5, 19, WHITE, "Set at least one GGID");
171                 passby_endflg = 4;
172             }
173 
174             /* Display clear */
175             BgClear();
176 
177             /* Main chance encounter communications loop */
178             for (gFrame = 0; passby_endflg == 0; gFrame++)
179             {
180                 OS_WaitVBlankIntr();
181 
182                 keyData = ReadKeySetTrigger(PAD_Read());
183 
184                 /* Distributes processes based on communication status */
185                 switch (WXC_GetStateCode())
186                 {
187                 case WXC_STATE_END:
188                     ModeSelect();
189                     break;
190                 case WXC_STATE_ENDING:
191                     break;
192                 case WXC_STATE_ACTIVE:
193                     if(WXC_IsParentMode() == TRUE)
194                     {
195                         BgPutString(8, 2, WHITE, "Now parent...");
196                     }
197                     else
198                     {
199                         BgPutString(8, 2, WHITE, "Now child...");
200                     }
201                     break;
202                 }
203 
204                 BgPutString(5, 19, WHITE, "Push START to Reset");
205 
206                 if (keyData & PAD_BUTTON_START)
207                 {
208                     passby_endflg = 3;
209                     /* Display clear */
210                     BgClear();
211                 }
212                 // Waiting for the V-Blank
213                 OS_WaitVBlankIntr();
214             }
215 
216             // End here if the WXC_RegisterCommonData function is being called
217             if(passby_endflg != 4)
218             {
219                 (void)WXC_End();
220             }
221 
222             while( WXC_GetStateCode() != WXC_STATE_END )
223             {
224                 ;
225             }
226             // Wait for A button
227             while(1)
228             {
229                 keyData = ReadKeySetTrigger(PAD_Read());
230 
231                 BgPutString(3, 1, WHITE, "Push A to Reset");
232 
233                 if (keyData & PAD_BUTTON_A)
234                 {
235                     passby_endflg = 0;
236                     menu_flg = MENU_MIN;
237                     break;
238                 }
239                 OS_WaitVBlankIntr();
240             }
241         }
242     }
243 }
244 
245 /*---------------------------------------------------------------------------*
246   Name:         Menu
247 
248   Description:  Processing for the chance encounter configuration screen.
249 
250   Arguments:    None.
251 
252   Returns:      None.
253  *---------------------------------------------------------------------------*/
Menu(void)254  static void Menu(void)
255 {
256     int i;
257     enum
258     { DISP_OX = 5, DISP_OY = 5 };
259 
260     /* Change cursor number */
261     if (keyData & PAD_KEY_DOWN)
262     {
263         menu_flg++;
264         if(menu_flg > MENU_MAX)
265         {
266             menu_flg = MENU_MIN;
267         }
268     }
269     else if (keyData & PAD_KEY_UP)
270     {
271         menu_flg--;
272         if(menu_flg < MENU_MIN)
273         {
274             menu_flg = MENU_MAX;
275         }
276     }
277     else if (keyData & (PAD_BUTTON_A | PAD_BUTTON_START))
278     {
279         menu_flg = 9;
280     }
281 
282     /* Start display */
283     BgPutString((s16)(DISP_OX + 3), (s16)(DISP_OY - 3), WHITE, "Main Menu");
284 
285     for(i = 0; i < GGID_MAX; i++)
286     {
287         BgPrintf((s16)(DISP_OX), (s16)(DISP_OY + i), WHITE,
288                  "GGID No.%01d     %s", (i+1), (passby_ggid[i]) ? "[Yes]" : "[No ]");
289     }
290 
291     BgPrintf((s16)(DISP_OX), (s16)(DISP_OY + i), WHITE,
292                  "GGID ALL SEND?     %s", (send_comp_flg) ? "[Yes]" : "[No ]");
293 
294     /* Cursor display */
295     BgPutChar((s16)(DISP_OX - 2), (s16)(DISP_OY + menu_flg-1), RED, '*');
296     /* Wait for A button */
297     BgPutString(DISP_OX, DISP_OY + 14, WHITE, " Push A or START Button");
298 
299 
300     if (keyData & (PAD_KEY_RIGHT | PAD_KEY_LEFT))
301     {
302 		if(menu_flg <= GGID_MAX)
303 		{
304             passby_ggid[menu_flg-1] ^= TRUE;
305         }
306         else
307         {
308 			send_comp_flg ^= TRUE;
309 		}
310     }
311 }
312 
313 /*---------------------------------------------------------------------------*
314   Name:         ModeSelect
315 
316   Description:  Processing in parent/child selection screen.
317 
318   Arguments:    None.
319 
320   Returns:      None.
321  *---------------------------------------------------------------------------*/
ModeSelect(void)322 static void ModeSelect(void)
323 {
324     BgPutString(3, 1, WHITE, "Push A to start");
325 
326     if (keyData & PAD_BUTTON_A)
327     {
328         User_Init();
329     }
330 }
331 
332 /*---------------------------------------------------------------------------*
333   Name:         ModeError
334 
335   Description:  Processing in error display screen.
336 
337   Arguments:    None.
338 
339   Returns:      None.
340  *---------------------------------------------------------------------------*/
ModeError(void)341 static void ModeError(void)
342 {
343     BgPutString(5, 0, WHITE, "======= ERROR! =======");
344     BgPutString(5, 1, WHITE, " Fatal error occured.");
345     BgPutString(5, 2, WHITE, "Please reboot program.");
346 }
347 
348 /*---------------------------------------------------------------------------*
349   Name:         VBlankIntr
350 
351   Description:  V-Blank interrupt handler.
352 
353   Arguments:    None.
354 
355   Returns:      None.
356  *---------------------------------------------------------------------------*/
VBlankIntr(void)357 static void VBlankIntr(void)
358 {
359     //---- OAM updating
360     DC_FlushRange(oamBak, sizeof(oamBak));
361     /* I/O register is accessed using DMA operation, so cache wait is not needed */
362     // DC_WaitWriteBufferEmpty();
363     MI_DmaCopy32(3, oamBak, (void *)HW_OAM, sizeof(oamBak));
364 
365     //---- background screen update
366     BgScrSetVblank();
367 
368     //(void)rand();
369 
370     //---- Interrupt check flag
371     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
372 }
373 
374 /*---------------------------------------------------------------------------*
375   Name:         InitializeAllocateSystem
376 
377   Description:  Initializes the memory allocation system in the main memory arena.
378 
379   Arguments:    None.
380 
381   Returns:      None.
382  *---------------------------------------------------------------------------*/
InitializeAllocateSystem(void)383 static void InitializeAllocateSystem(void)
384 {
385     void   *tempLo;
386     OSHeapHandle hh;
387 
388     OS_Printf(" arena lo = %08x\n", OS_GetMainArenaLo());
389     OS_Printf(" arena hi = %08x\n", OS_GetMainArenaHi());
390 
391     tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
392     OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
393     hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
394     if (hh < 0)
395     {
396         OS_Panic("ARM9: Fail to create heap...\n");
397     }
398     hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
399 }
400 
401 
402 /*----------------------------------------------------------------------------*
403 
404 /*---------------------------------------------------------------------------*
405   End of file
406  *---------------------------------------------------------------------------*/
407