1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/2D_CharBg_2
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-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 //---------------------------------------------------------------------------
19 // A sample that uses BG #2 as affine mode (rotate):
20 //
21 // USAGE:
22 //   UP, DOWN, LEFT, RIGHT      : Change the center of rotation
23 //   L,R:                   Rotate BG #2
24 //
25 // HOWTO:
26 // 1. Set up the character/palette/screen data same as 2D_CharBG_1.
27 // 2. Set up a matrix and points with G2_SetBGxAffine().
28 //---------------------------------------------------------------------------
29 
30 #include <nitro/code32.h>              // avoid byte access problems
31 #ifdef SDK_TWL
32 #include <twl.h>
33 #else
34 #include <nitro.h>
35 #endif
36 #include "DEMO.h"
37 #include "data.h"
38 
39 static u8 sScrnBuf[SCREEN_SIZE];       // Buffer for screen data (BG #2)
40 
41 #ifdef SDK_TWL
TwlMain(void)42 void TwlMain(void)
43 #else
44 void NitroMain(void)
45 #endif
46 {
47     s16     x_0 = 0, y_0 = 0;
48     s16     x_1 = 0, y_1 = 0;
49 
50     u16     rotate = 0;
51 
52     //---------------------------------------------------------------------------
53     // Initialize:
54     // Enables IRQ interrupts, initializes VRAM, and sets BG #2 for affine mode.
55     //---------------------------------------------------------------------------
56     DEMOInitCommon();
57     DEMOInitVRAM();
58     DEMOInitDisplayBG2Only();
59 
60     //---------------------------------------------------------------------------
61     // Transmitting the character data and the palette data
62     //---------------------------------------------------------------------------
63     GX_LoadBG2Char(d_64_256_bg_schDT, 0, sizeof(d_64_256_bg_schDT));
64     GX_LoadBGPltt(d_64_256_bg_sclDT, 0, sizeof(d_64_256_bg_sclDT));
65 
66     {
67         int     i, j;
68         for (i = 0; i < 8; i++)
69         {
70             for (j = 0; j < 8; j++)
71             {
72                 sScrnBuf[(i * 16) + j] = (u8)((i * 0x10) + j);
73             }
74         }
75     }
76     // Store the data in the main memory, and invalidate the cache.
77     DC_FlushRange(sScrnBuf, sizeof(sScrnBuf));
78     /* I/O register is accessed using DMA operation, so cache wait is not needed */
79     // DC_WaitWriteBufferEmpty();
80 
81     // DMA Transfer to BG #2 screen
82     GX_LoadBG2Scr(sScrnBuf, 0, sizeof(sScrnBuf));
83 
84     DEMOStartDisplay();
85     //---------------------------------------------------------------------------
86     // Main Loop
87     //---------------------------------------------------------------------------
88     while (1)
89     {
90         MtxFx22 mtx;
91         fx16    sinVal, cosVal;
92 
93         DEMOReadKey();
94 
95         if (DEMO_IS_PRESS(PAD_KEY_UP))
96             y_0 -= 2;
97         if (DEMO_IS_PRESS(PAD_KEY_DOWN))
98             y_0 += 2;
99         if (DEMO_IS_PRESS(PAD_KEY_RIGHT))
100             x_0 += 2;
101         if (DEMO_IS_PRESS(PAD_KEY_LEFT))
102             x_0 -= 2;
103         if (DEMO_IS_PRESS(PAD_BUTTON_L))
104             rotate -= 256;
105         if (DEMO_IS_PRESS(PAD_BUTTON_R))
106             rotate += 256;
107         if (DEMO_IS_TRIG(PAD_BUTTON_SELECT))
108         {
109             x_0 = 32;
110             y_0 = 32;
111             rotate = 0;
112         }
113 
114         sinVal = FX_SinIdx(rotate);
115         cosVal = FX_CosIdx(rotate);
116 
117         mtx._00 = (fx32)cosVal;
118         mtx._01 = (fx32)sinVal;
119         mtx._10 = -(fx32)sinVal;
120         mtx._11 = (fx32)cosVal;
121 
122 #ifdef SDK_AUTOTEST
123         x_0 = 10;
124         y_0 = 15;
125         rotate = 4096;                 // default params for testing.
126         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
127         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
128         EXT_TestScreenShot(100, 0xD4F05FD3);
129         EXT_TestTickCounter();
130 #endif //SDK_AUTOTEST
131 
132         OS_WaitVBlankIntr();           // Waiting for the end of the VBlank interrupt
133 
134         //---------------------------------------------------------------------------
135         // Set up affine transformation of BG #2
136         //---------------------------------------------------------------------------
137         G2_SetBG2Affine(&mtx,          // a matrix for rotation and scaling
138                         x_0, y_0,      // the center of rotation
139                         x_1, y_1       // the reference point before rotation and scaling applied
140             );
141     }
142 }
143 
144 //---------------------------------------------------------------------------
145 // VBlank interrupt function:
146 //
147 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
148 // OS_EnableIrqMask selects IRQ interrupts to enable, and
149 // OS_EnableIrq enables IRQ interrupts.
150 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
151 //---------------------------------------------------------------------------
VBlankIntr(void)152 void VBlankIntr(void)
153 {
154     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Checking VBlank interrupt
155 }
156