1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - demos - timer-1
3   File:     main.c
4 
5   Copyright 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-04-01#$
14   $Rev: 5208 $
15   $Author: yada $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 #include "data.h"
19 
20 #define MY_COUNT1   (33514000/1024/10)
21 #define MY_SCALE1   OS_TIMER_PRESCALER_1024
22 
23 #define MY_COUNT2   (33514000/256)
24 #define MY_SCALE2   OS_TIMER_PRESCALER_256
25 
26 void    MyInit(void);
27 void    DisplayCounter( int objNo, int y, u32 count );
28 void    VBlankIntr(void);
29 void    SetObj(int objNo, int x, int y, int charNo, int paletteNo);
30 
31 void    SetTimer_1(void);
32 void    TimerIntr_1(void);
33 
34 void    SetTimer_2(void);
35 void    TimerIntr_2(void);
36 
37 GXOamAttr oamBak[128];
38 volatile u32 timerCnt1 = 0;
39 volatile u32 timerCnt2 = 0;
40 BOOL pauseFlag = FALSE;
41 
42 /*---------------------------------------------------------------------------*
43   Name:         NitroMain
44 
45   Description:  main
46 
47   Arguments:    None
48 
49   Returns:      None
50  *---------------------------------------------------------------------------*/
NitroMain()51 void NitroMain()
52 {
53 	MyInit();
54 
55     //---- set TIMER
56     SetTimer_1();
57     SetTimer_2();
58 
59     while (1)
60     {
61 		static u16 preButton = 0;
62         u16  button = PAD_Read();
63 		u16 trigger = (u16)((button ^ preButton) & button);
64 		preButton = button;
65 
66         //---- wait VBlank
67         OS_WaitVBlankIntr();
68 
69 		//---- display counter
70 		DisplayCounter(0, 100, timerCnt1);
71 		DisplayCounter(8, 140, timerCnt2);
72 
73         //---- button A to enable/disable TIMER
74         if (trigger & PAD_BUTTON_A)
75         {
76 			if ( pauseFlag )
77 			{
78 				(void)OS_EnableIrqMask(OS_IE_TIMER0);
79 				(void)OS_EnableIrqMask(OS_IE_TIMER2);
80 				pauseFlag = FALSE;
81 			}
82 			else
83 			{
84 				(void)OS_DisableIrqMask(OS_IE_TIMER0);
85 				(void)OS_DisableIrqMask(OS_IE_TIMER2);
86 				pauseFlag = TRUE;
87 			}
88         }
89     }
90 }
91 
92 /*---------------------------------------------------------------------------*
93   Name:         MyInit
94 
95   Description:  initialize (about graphics mainly)
96 
97   Arguments:    None
98 
99   Returns:      None
100  *---------------------------------------------------------------------------*/
MyInit(void)101 void MyInit(void)
102 {
103     OS_Init();
104     GX_Init();
105 
106     //---- set power on
107     GX_SetPower(GX_POWER_ALL);
108 
109     //----  enable VBlank interrupt
110     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
111     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
112     (void)OS_EnableIrq();
113 
114     //---- setting VBlank
115     (void)GX_VBlankIntr(TRUE);
116 
117     //---- clae VRAM
118     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
119     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
120     (void)GX_DisableBankForLCDC();
121 
122     //---- clear OAM and palette
123     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
124     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
125 
126     //---- allocate bank A to OBJ
127     GX_SetBankForOBJ(GX_VRAM_OBJ_128_A);
128 
129     //---- set to graphics display mode
130     GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
131 
132     //---- set OBJ be visible
133     GX_SetVisiblePlane(GX_PLANEMASK_OBJ);
134 
135     //---- 32KByte OBJ, 2D map mode
136     GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
137 
138     //---- load character and palette data
139     MI_DmaCopy32(3, sampleCharData, (void *)HW_OBJ_VRAM, sizeof(sampleCharData));
140     MI_DmaCopy32(3, samplePlttData, (void *)HW_OBJ_PLTT, sizeof(samplePlttData));
141 
142     //---- drive unused OBJs out of screen
143     MI_DmaFill32(3, oamBak, 0xc0, sizeof(oamBak));
144 
145     //---- start displaying
146     OS_WaitVBlankIntr();
147     GX_DispOn();
148 }
149 
150 /*---------------------------------------------------------------------------*
151   Name:         DisplayCounter
152 
153   Description:  display counter
154 
155   Arguments:    objNo :
156                 y     :
157                 count :
158 
159   Returns:      None
160  *---------------------------------------------------------------------------*/
valToChar(int val)161 inline int valToChar(int val)
162 {
163     return (int)((val < 10) ? '0' + val : 'A' + val - 10);
164 }
165 
DisplayCounter(int objNo,int y,u32 count)166 void DisplayCounter( int objNo, int y, u32 count )
167 {
168 	SetObj(objNo+0,  50, y, valToChar((int)((count>>28)&0xf)), 2);
169 	SetObj(objNo+1,  60, y, valToChar((int)((count>>24)&0xf)), 2);
170 	SetObj(objNo+2,  70, y, valToChar((int)((count>>20)&0xf)), 2);
171 	SetObj(objNo+3,  80, y, valToChar((int)((count>>16)&0xf)), 2);
172 	SetObj(objNo+4,  90, y, valToChar((int)((count>>12)&0xf)), 2);
173 	SetObj(objNo+5, 100, y, valToChar((int)((count>> 8)&0xf)), 2);
174 	SetObj(objNo+6, 110, y, valToChar((int)((count>> 4)&0xf)), 2);
175 	SetObj(objNo+7, 120, y, valToChar((int)((count    )&0xf)), 2);
176 }
177 
178 /*---------------------------------------------------------------------------*
179   Name:         SetObj
180 
181   Description:  set a object
182 
183   Arguments:    objNo     : object No.
184                 x         : x
185                 y         : y
186                 charNo    : character No.
187                 paletteNo : palette No.
188 
189   Returns:      None
190  *---------------------------------------------------------------------------*/
SetObj(int objNo,int x,int y,int charNo,int paletteNo)191 void SetObj(int objNo, int x, int y, int charNo, int paletteNo)
192 {
193     G2_SetOBJAttr((GXOamAttr *)&oamBak[objNo],
194                   x,
195                   y,
196                   0,
197                   GX_OAM_MODE_NORMAL,
198                   FALSE,
199                   GX_OAM_EFFECT_NONE, GX_OAM_SHAPE_8x8, GX_OAM_COLOR_16, charNo, paletteNo, 0);
200 }
201 
202 /*---------------------------------------------------------------------------*
203   Name:         VBlankIntr
204 
205   Description:  VBlank interrupt handler
206 
207   Arguments:    None
208 
209   Returns:      None
210  *---------------------------------------------------------------------------*/
VBlankIntr(void)211 void VBlankIntr(void)
212 {
213     //---- renew OAM
214     DC_FlushRange(oamBak, sizeof(oamBak));
215     MI_DmaCopy32(3, oamBak, (void *)HW_OAM, sizeof(oamBak));
216 
217     //---- interrupt flag
218     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
219 }
220 
221 //================================================================================
222 //  TIMER 0 as 16bit timer
223 //================================================================================
224 /*---------------------------------------------------------------------------*
225   Name:         SetTimer_1
226 
227   Description:  Set timer
228 
229   Arguments:    None
230 
231   Returns:      None
232  *---------------------------------------------------------------------------*/
SetTimer_1(void)233 void SetTimer_1(void)
234 {
235     //---- set interrupt handler
236     OS_SetIrqFunction(OS_IE_TIMER0, TimerIntr_1);
237 
238     //---- enable TIMER 0
239     (void)OS_EnableIrqMask(OS_IE_TIMER0);
240 
241     //---- start TIMER 0
242     OS_StartTimer(OS_TIMER_0, MY_COUNT1, MY_SCALE1);
243 }
244 
245 /*---------------------------------------------------------------------------*
246   Name:         TimerIntr_1
247 
248   Description:  timer interrupt handler
249 
250   Arguments:    None
251 
252   Returns:      None
253  *---------------------------------------------------------------------------*/
TimerIntr_1(void)254 void TimerIntr_1(void)
255 {
256     timerCnt1 = timerCnt1 + 1;
257 
258     //---- interrupt flag
259     OS_SetIrqCheckFlag(OS_IE_TIMER0);
260 
261     //---- stop TIMER
262     OS_StopTimer(OS_TIMER_0);
263 
264     //---- re-set TIMER
265     SetTimer_1();
266 }
267 
268 //================================================================================
269 //  TIMER 1 and 2 as 32bit timer
270 //================================================================================
271 /*---------------------------------------------------------------------------*
272   Name:         SetTimer
273 
274   Description:  Set timer
275 
276   Arguments:    None
277 
278   Returns:      None
279  *---------------------------------------------------------------------------*/
SetTimer_2(void)280 void SetTimer_2(void)
281 {
282     //---- set interrupt handler
283     OS_SetIrqFunction(OS_IE_TIMER2, TimerIntr_2);
284 
285     //---- enable TIMER 1 and 2
286     (void)OS_EnableIrqMask(OS_IE_TIMER1 | OS_IE_TIMER2);
287 
288     //---- start TIMER 1 and 2
289     OS_StartTimer32(OS_TIMER32_12, MY_COUNT2, MY_SCALE2);
290 }
291 
292 /*---------------------------------------------------------------------------*
293   Name:         TimerIntr
294 
295   Description:  timer interrupt handler
296 
297   Arguments:    None
298 
299   Returns:      None
300  *---------------------------------------------------------------------------*/
TimerIntr_2(void)301 void TimerIntr_2(void)
302 {
303     timerCnt2 = timerCnt2 + 1;
304 
305     //---- interrupt flag
306     OS_SetIrqCheckFlag(OS_IE_TIMER2);
307 
308     //---- stop TIMER
309     OS_StopTimer32(OS_TIMER32_12);
310 
311     //---- re-set TIMER
312     SetTimer_2();
313 }
314 
315 
316 /*====== End of main.c ======*/
317