1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - MI - demos - dmaCallback-1
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-05-22#$
14 $Rev: 6123 $
15 $Author: yada $
16 *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 #include "data.h"
19
20 //---- DMA number
21 #define DMA_NO 3
22
23 GXOamAttr oamBak[128];
24 volatile u32 dmaCnt = 0;
25
26 void myInit(void);
27 void DisplayCounter( int objNo, int y, u32 count );
28 void myVBlankIntr(void);
29 void SetObj(int objNo, int x, int y, int charNo, int paletteNo);
30
31 //---- DMA callback
32 void myDmaCallback(void *arg);
33
34 //================================================================================
35 /*---------------------------------------------------------------------------*
36 Name: NitroMain
37
38 Description: main
39
40 Arguments: None
41
42 Returns: None
43 *---------------------------------------------------------------------------*/
44 OSAlarm alarm;
NitroMain()45 void NitroMain()
46 {
47 myInit();
48
49 while (1)
50 {
51 //---- wait vblank
52 OS_WaitVBlankIntr();
53
54 //---- display counter
55 DisplayCounter(0, 100, dmaCnt);
56 }
57 }
58
59 /*---------------------------------------------------------------------------*
60 Name: myInit
61
62 Description: initialize
63
64 Arguments: None
65
66 Returns: None
67 *---------------------------------------------------------------------------*/
myInit(void)68 static void myInit(void)
69 {
70 OS_Init();
71 GX_Init();
72
73 //---- set power on
74 GX_SetPower(GX_POWER_ALL);
75
76 //---- enable VBlank interrupt
77 (void)OS_SetIrqFunction(OS_IE_V_BLANK, myVBlankIntr);
78 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
79 (void)OS_EnableIrq();
80
81 //---- setting VBlank
82 (void)GX_VBlankIntr(TRUE);
83
84 //---- clae VRAM
85 GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
86 MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
87 (void)GX_DisableBankForLCDC();
88
89 //---- clear OAM and palette
90 MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
91 MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
92
93 //---- allocate bank A to OBJ
94 GX_SetBankForOBJ(GX_VRAM_OBJ_128_A);
95
96 //---- set to graphics display mode
97 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
98
99 //---- set OBJ be visible
100 GX_SetVisiblePlane(GX_PLANEMASK_OBJ);
101
102 //---- 32KByte OBJ, 2D map mode
103 GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
104
105 //---- load character and palette data
106 MI_DmaCopy32(3, sampleCharData, (void *)HW_OBJ_VRAM, sizeof(sampleCharData));
107 MI_DmaCopy32(3, samplePlttData, (void *)HW_OBJ_PLTT, sizeof(samplePlttData));
108
109 //---- drive unused OBJs out of screen
110 MI_DmaFill32(3, oamBak, 0xc0, sizeof(oamBak));
111
112 //---- start displaying
113 OS_WaitVBlankIntr();
114 GX_DispOn();
115 }
116
117 /*---------------------------------------------------------------------------*
118 Name: DisplayCounter
119
120 Description: display counter
121
122 Arguments: objNo :
123 y :
124 count :
125
126 Returns: None
127 *---------------------------------------------------------------------------*/
valToChar(int val)128 inline int valToChar(int val)
129 {
130 return (int)((val < 10) ? '0' + val : 'A' + val - 10);
131 }
132
DisplayCounter(int objNo,int y,u32 count)133 void DisplayCounter( int objNo, int y, u32 count )
134 {
135 SetObj(objNo+0, 50, y, valToChar((int)((count>>28)&0xf)), 2);
136 SetObj(objNo+1, 60, y, valToChar((int)((count>>24)&0xf)), 2);
137 SetObj(objNo+2, 70, y, valToChar((int)((count>>20)&0xf)), 2);
138 SetObj(objNo+3, 80, y, valToChar((int)((count>>16)&0xf)), 2);
139 SetObj(objNo+4, 90, y, valToChar((int)((count>>12)&0xf)), 2);
140 SetObj(objNo+5, 100, y, valToChar((int)((count>> 8)&0xf)), 2);
141 SetObj(objNo+6, 110, y, valToChar((int)((count>> 4)&0xf)), 2);
142 SetObj(objNo+7, 120, y, valToChar((int)((count )&0xf)), 2);
143 }
144
145 /*---------------------------------------------------------------------------*
146 Name: SetObj
147
148 Description: set a object
149
150 Arguments: objNo : object No.
151 x : x
152 y : y
153 charNo : character No.
154 paletteNo : palette No.
155
156 Returns: None
157 *---------------------------------------------------------------------------*/
SetObj(int objNo,int x,int y,int charNo,int paletteNo)158 void SetObj(int objNo, int x, int y, int charNo, int paletteNo)
159 {
160 G2_SetOBJAttr((GXOamAttr *)&oamBak[objNo],
161 x,
162 y,
163 0,
164 GX_OAM_MODE_NORMAL,
165 FALSE,
166 GX_OAM_EFFECT_NONE, GX_OAM_SHAPE_8x8, GX_OAM_COLOR_16, charNo, paletteNo, 0);
167 }
168
169 /*---------------------------------------------------------------------------*
170 Name: myVBlankIntr
171
172 Description: vblank interrupt handler
173
174 Arguments: None
175
176 Returns: None
177 *---------------------------------------------------------------------------*/
myVBlankIntr(void)178 static void myVBlankIntr(void)
179 {
180 DC_FlushRange(oamBak, sizeof(oamBak));
181
182 //---- transfer OAM data
183 MI_DmaCopy32Async(DMA_NO, oamBak, (void *)HW_OAM, sizeof(oamBak), myDmaCallback, (void *)0x1);
184
185 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
186 }
187
188 /*---------------------------------------------------------------------------*
189 Name: myVBlankIntr
190
191 Description: dma callback
192
193 Arguments: arg :
194
195 Returns: None
196 *---------------------------------------------------------------------------*/
myDmaCallback(void * arg)197 void myDmaCallback(void *arg)
198 {
199 dmaCnt = dmaCnt + (u32)arg;
200 }
201
202 /*====== End of main.c ======*/
203