1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - demos - tick-1
3   File:     main.c
4 
5   Copyright 2003-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: 5205 $
15   $Author: yada $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 #include "data.h"
19 
20 static GXOamAttr oamBak[128];
21 
22 void    VBlankIntr(void);
23 void    ObjSet(int objNo, int x, int y, int charNo, int paletteNo);
24 
25 //---- Return 0-9, A-F code for OBJ display
ObjChar(u32 cnt,int shift)26 inline int ObjChar(u32 cnt, int shift)
27 {
28     u32     d = (cnt >> shift) & 0xf;
29     return (int)((d < 10) ? '0' + d : 'A' + d - 10);
30 }
31 
32 //================================================================================
33 /*---------------------------------------------------------------------------*
34   Name:         NitroMain
35 
36   Description:  Main.
37 
38   Arguments:    None.
39 
40   Returns:      None.
41  *---------------------------------------------------------------------------*/
NitroMain()42 void NitroMain()
43 {
44     //================ Initialization
45     OS_Init();
46     OS_InitTick();                     // Initialize system clock
47 
48     GX_Init();
49 
50     //================ Settings
51     //---- All Power ON
52     GX_SetPower(GX_POWER_ALL);
53 
54     //----  Enable V-Blank interrupt
55     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
56     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
57     (void)OS_EnableIrq();
58 
59     //---- V-Blank occurrence settings
60     (void)GX_VBlankIntr(TRUE);
61 
62     //---- Clear VRAM
63     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
64     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
65     (void)GX_DisableBankForLCDC();
66 
67     //---- OAM and palette clear
68     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
69     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
70 
71     //---- Set bank A for OBJ
72     GX_SetBankForOBJ(GX_VRAM_OBJ_128_A);
73 
74     //---- Set to graphics display mode
75     GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
76 
77     //---- Set only OBJ display ON
78     GX_SetVisiblePlane(GX_PLANEMASK_OBJ);
79 
80     //---- Used with 32-KB OBJ in 2D mapping mode
81     GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
82 
83     //---- Data load
84     MI_DmaCopy32(3, sampleCharData, (void *)HW_OBJ_VRAM, sizeof(sampleCharData));
85     MI_DmaCopy32(3, samplePlttData, (void *)HW_OBJ_PLTT, sizeof(samplePlttData));
86 
87     GX_DispOn();
88 
89 
90     //================ Main loop
91     while (1)
92     {
93         u16     keyData;
94         u32     hi, low;
95         OSTick  timerCnt;
96 
97         //---- Wait for V-Blank interrupt completion
98         OS_WaitVBlankIntr();
99 
100         //---- Move hidden OBJ off screen
101         MI_DmaFill32(3, oamBak, 0xc0, sizeof(oamBak));
102 
103         //---- Load pad data
104         keyData = PAD_Read();
105 
106         //---- Get system clock value
107         timerCnt = OS_GetTick();
108 
109         //---- Display system clock value
110         hi = (u32)(timerCnt >> 32);
111         low = (u32)(timerCnt & 0xffffffff);
112 
113         ObjSet(0, 50, 100, ObjChar(hi, 28), 2);
114         ObjSet(1, 60, 100, ObjChar(hi, 24), 2);
115         ObjSet(2, 70, 100, ObjChar(hi, 20), 2);
116         ObjSet(3, 80, 100, ObjChar(hi, 16), 2);
117         ObjSet(4, 90, 100, ObjChar(hi, 12), 2);
118         ObjSet(5, 100, 100, ObjChar(hi, 8), 2);
119         ObjSet(6, 110, 100, ObjChar(hi, 4), 2);
120         ObjSet(7, 120, 100, ObjChar(hi, 0), 2);
121 
122         ObjSet(8, 140, 100, ObjChar(low, 28), 2);
123         ObjSet(9, 150, 100, ObjChar(low, 24), 2);
124         ObjSet(10, 160, 100, ObjChar(low, 20), 2);
125         ObjSet(11, 170, 100, ObjChar(low, 16), 2);
126         ObjSet(12, 180, 100, ObjChar(low, 12), 2);
127         ObjSet(13, 190, 100, ObjChar(low, 8), 2);
128         ObjSet(14, 200, 100, ObjChar(low, 4), 2);
129         ObjSet(15, 210, 100, ObjChar(low, 0), 2);
130 
131         //---- Pressing the A button advances the system clock a little
132         if (keyData & PAD_BUTTON_A)
133         {
134             OS_SetTick(OS_GetTick() + (u64)0x20000000000ULL);
135         }
136     }
137 }
138 
139 //--------------------------------------------------------------------------------
140 //  Set OBJ
141 //
ObjSet(int objNo,int x,int y,int charNo,int paletteNo)142 void ObjSet(int objNo, int x, int y, int charNo, int paletteNo)
143 {
144     G2_SetOBJAttr((GXOamAttr *)&oamBak[objNo],
145                   x,
146                   y,
147                   0,
148                   GX_OAM_MODE_NORMAL,
149                   FALSE,
150                   GX_OAM_EFFECT_NONE, GX_OAM_SHAPE_8x8, GX_OAM_COLOR_16, charNo, paletteNo, 0);
151 }
152 
153 
154 //--------------------------------------------------------------------------------
155 //    V-Blank interrupt process
156 //
VBlankIntr(void)157 void VBlankIntr(void)
158 {
159     //---- OAM updating
160     DC_FlushRange(oamBak, sizeof(oamBak));
161     /* I/O register is accessed using DMA operation, so cache wait is not needed */
162     // DC_WaitWriteBufferEmpty();
163     MI_DmaCopy32(3, oamBak, (void *)HW_OAM, sizeof(oamBak));
164 
165     //---- Interrupt check flag
166     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
167 }
168 
169 /*====== End of main.c ======*/
170