1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - demos - exceptionDisplay-3
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:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 #include "font.h"
19 #include "screen.h"
20 
21 static void myInit(void);
22 static void myVBlankIntr(void);
23 static void myExceptionCallback(u32 context, void *arg);
24 
25 /*---------------------------------------------------------------------------*
26   Name:         NitroMain
27 
28   Description:  main
29 
30   Arguments:    None
31 
32   Returns:      None
33  *---------------------------------------------------------------------------*/
34 #pragma profile off
NitroMain()35 void NitroMain()
36 {
37 	myInit();
38 
39 	OS_EnableUserExceptionHandlerOnDebugger();
40     OS_SetUserExceptionHandler(myExceptionCallback, (void *)0);
41     DC_FlushAll();
42     DC_WaitWriteBufferEmpty();
43 
44     while (TRUE)
45     {
46 		u16 button = PAD_Read();
47 
48         //---- clear screen buffer
49         ClearScreen();
50 		PrintString( 2, 1, 15, "PUSH [A] to cause EXCEPTION");
51 
52 		if ( button & PAD_BUTTON_A )
53 		{
54 			//---- force to occur exception
55 			OS_Printf("now force to occur exception...\n");
56 			asm
57 			{
58 				/* *INDENT-OFF* */
59 				ldr      r0, =0
60 				ldr      r1, =0x12345678
61 #ifdef SDK_ARM9
62 				ldr      r2, [r0,#0]
63 #else
64 				dcd      0x06000010
65 #endif
66 				/* *INDENT-ON* */
67 			}
68 			OS_Printf("not occurred exception.\n");
69 		}
70 
71         //---- wait vblank
72 		OS_WaitVBlankIntr();
73 	}
74 }
75 
76 /*---------------------------------------------------------------------------*
77   Name:         myInit
78 
79   Description:  initialize
80 
81   Arguments:    None
82 
83   Returns:      None
84  *---------------------------------------------------------------------------*/
myInit(void)85 static void myInit(void)
86 {
87     //---- init
88     OS_Init();
89     OS_InitTick();
90     OS_InitAlarm();
91     FX_Init();
92     GX_Init();
93     GX_DispOff();
94     GXS_DispOff();
95 
96     //---- init displaying
97     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
98     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
99     (void)GX_DisableBankForLCDC();
100 
101     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
102     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
103     MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);
104     MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);
105 
106     //---- setting 2D for top screen
107     GX_SetBankForBG(GX_VRAM_BG_128_A);
108 
109     G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256,
110                      GX_BG_COLORMODE_16,
111                      GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01);
112     G2_SetBG0Priority(0);
113     G2_BG0Mosaic(FALSE);
114     GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
115     GX_SetVisiblePlane(GX_PLANEMASK_BG0);
116 
117     GX_LoadBG0Char(d_CharData, 0, sizeof(d_CharData));
118     GX_LoadBGPltt(d_PaletteData, 0, sizeof(d_PaletteData));
119 
120 
121 
122     //---- setting 2D for bottom screen
123     GX_SetBankForSubBG(GX_VRAM_SUB_BG_128_C);
124 
125     G2S_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256,
126                       GX_BG_COLORMODE_16,
127                       GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01);
128     G2S_SetBG0Priority(0);
129     G2S_BG0Mosaic(FALSE);
130     GXS_SetGraphicsMode(GX_BGMODE_0);
131     GXS_SetVisiblePlane(GX_PLANEMASK_BG0);
132 
133     GXS_LoadBG0Char(d_CharData, 0, sizeof(d_CharData));
134     GXS_LoadBGPltt(d_PaletteData, 0, sizeof(d_PaletteData));
135 
136 
137     //---- screen
138     MI_CpuFillFast((void *)gScreen, 0, sizeof(gScreen));
139     DC_FlushRange(gScreen, sizeof(gScreen));
140     GX_LoadBG0Scr(gScreen, 0, sizeof(gScreen));
141     GXS_LoadBG0Scr(gScreen, 0, sizeof(gScreen));
142 
143     //---- init interrupt
144     OS_SetIrqFunction(OS_IE_V_BLANK, myVBlankIntr);
145     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
146     (void)GX_VBlankIntr(TRUE);
147     (void)OS_EnableIrq();
148     (void)OS_EnableInterrupts();
149 
150 	//---- FileSytem init
151 	FS_Init(FS_DMA_NOT_USE);
152 
153     //---- start displaying
154     GX_DispOn();
155     GXS_DispOn();
156 }
157 
158 /*---------------------------------------------------------------------------*
159   Name:         myVBlankIntr
160 
161   Description:  vblank interrupt handler
162 
163   Arguments:    None
164 
165   Returns:      None
166  *---------------------------------------------------------------------------*/
myVBlankIntr(void)167 static void myVBlankIntr(void)
168 {
169     //---- upload pseudo screen to VRAM
170     DC_FlushRange(gScreen, sizeof(gScreen));
171     GX_LoadBG0Scr(gScreen, 0, sizeof(gScreen));
172     GXS_LoadBG0Scr(gScreen, 0, sizeof(gScreen));
173 
174     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
175 }
176 
177 /*---------------------------------------------------------------------------*
178   Name:         myExceptionCallback
179 
180   Description:  user callback for exception
181 
182   Arguments:    context :
183                 arg     :
184 
185   Returns:      None
186  *---------------------------------------------------------------------------*/
myExceptionCallback(u32 context,void * arg)187 void myExceptionCallback(u32 context, void *arg)
188 {
189 #pragma unused( arg )
190 
191 	ClearScreen();
192     PrintString( 0, 0, 15, "**** Exception Occurred ****");
193 	{
194 		int i;
195 		OSExcpContext *c = (OSExcpContext*)context;
196 
197 		for (i = 0; i < 13; i++)
198 		{
199 			PrintString( (s16)((i&1)?16:1), (s16)((i/2)+1), 15, "R%02d=%08X", i, c->context.r[i] );
200 		}
201 
202 		PrintString(16, 7, 15, "SP =%08X", c->context.sp);
203 		PrintString( 1, 8, 15, "LR =%08X", c->context.lr);
204 		PrintString(16, 8, 15, "PC =%08X", c->context.pc_plus4);
205 
206 		PrintString( 0, 9, 15, "CPSR=%08X  SPSR=%08X", c->context.cpsr, c->spsr);
207 		PrintString( 0, 10, 15, "CP15=%08X",c->cp15);
208 	}
209 
210     DC_FlushRange(gScreen, sizeof(gScreen));
211     GX_LoadBG0Scr(gScreen, 0, sizeof(gScreen));
212     GXS_LoadBG0Scr(gScreen, 0, sizeof(gScreen));
213 
214 	while(1)
215 	{
216 		// do nothing
217 	}
218 }
219 
220 /*====== End of main.c ======*/
221