1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/Sub_CharBg_4
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 // A sample that scrolls BG #0 (text mode)
19 //
20 // USAGE:
21 //   UP, DOWN, LEFT, RIGHT: Scrolls a sphere
22 //   SELECT:                Move sphere to the initial position
23 //
24 // HOWTO:
25 // 1. Set up the character/palette/screen data the same as Sub_CharBg_1
26 // 2. Specify an offset with G2S_SetBGxOffset
27 //---------------------------------------------------------------------------
28 
29 #ifdef SDK_TWL
30 #include <twl.h>
31 #else
32 #include <nitro.h>
33 #endif
34 #include "DEMO.h"
35 #include "data.h"
36 
37 static u16 sScrnBuf[SCREEN_SIZE];      // Buffer for screen data (BG #0)
38 
39 #ifdef SDK_TWL
TwlMain(void)40 void TwlMain(void)
41 #else
42 void NitroMain(void)
43 #endif
44 {
45     int     offset_H = 0, offset_V = 0;
46 
47     //---------------------------------------------------------------------------
48     // Initialize:
49     // Enables IRQ interrupts, initializes VRAM, and sets BG #0 for text mode
50     //---------------------------------------------------------------------------
51     DEMOInitCommon();
52     DEMOInitVRAM();
53     DEMOInitDisplaySubBG0Only();
54 
55     //---------------------------------------------------------------------------
56     // Transmitting the character data and the palette data
57     //---------------------------------------------------------------------------
58     GXS_LoadBG0Char(d_64_256_bg_schDT, 0, sizeof(d_64_256_bg_schDT));
59     GXS_LoadBGPltt(d_64_256_bg_sclDT, 0, sizeof(d_64_256_bg_sclDT));
60 
61     {
62         int     i, j;
63         for (i = 0; i < 8; i++)
64         {
65             for (j = 0; j < 8; j++)
66             {
67                 sScrnBuf[(i * 32) + j] = (u16)((i * 0x10) + j);
68             }
69         }
70     }
71 
72     // Store the data in the main memory, and invalidate the cache
73     DC_FlushRange(sScrnBuf, sizeof(sScrnBuf));
74     /* I/O register is accessed using DMA operation, so cache wait is not needed */
75     // DC_WaitWriteBufferEmpty();
76 
77     // DMA Transfer to BG #0 screen
78     GXS_LoadBG0Scr(sScrnBuf, 0, sizeof(sScrnBuf));
79 
80     DEMOStartDisplay();
81     //---------------------------------------------------------------------------
82     // Main Loop
83     //---------------------------------------------------------------------------
84     while (1)
85     {
86         DEMOReadKey();
87 
88         if (DEMO_IS_PRESS(PAD_KEY_UP))
89             offset_V += 2;
90         if (DEMO_IS_PRESS(PAD_KEY_DOWN))
91             offset_V -= 2;
92         if (DEMO_IS_PRESS(PAD_KEY_RIGHT))
93             offset_H -= 2;
94         if (DEMO_IS_PRESS(PAD_KEY_LEFT))
95             offset_H += 2;
96         if (DEMO_IS_TRIG(PAD_BUTTON_SELECT))
97         {
98             offset_H = 0;
99             offset_V = 0;
100         }
101 
102         OS_WaitVBlankIntr();           // Waiting for the end of the V-Blank interrupt
103 
104         // Setting the offset for BG #0 plane
105         G2S_SetBG0Offset(offset_H, offset_V);
106     }
107 }
108 
109 //---------------------------------------------------------------------------
110 // V-Blank interrupt function:
111 //
112 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
113 // OS_EnableIrqMask selects IRQ interrupts to enable, and
114 // OS_EnableIrq enables IRQ interrupts.
115 // Notice that you have to call OS_SetIrqCheckFlag to check a V-Blank interrupt.
116 //---------------------------------------------------------------------------
VBlankIntr(void)117 void VBlankIntr(void)
118 {
119     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Checking V-Blank interrupt
120 }
121