1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MB - demos - fake_child
3   File:     disp.c
4 
5   Copyright 2006-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 // Basic string display functionality
20 //----------------------------------------------------------------------
21 
22 #ifdef SDK_TWL
23 #include	<twl.h>
24 #else
25 #include	<nitro.h>
26 #endif
27 #include "disp.h"
28 
29 
30 //============================================================================
31 //  Prototype Declarations
32 //============================================================================
33 static void VramClear(void);
34 static void ObjInitForPrintStr(void);
35 static void BgInitForPrintStr(void);
36 
37 
38 //============================================================================
39 //  Constant Definitions
40 //============================================================================
41 
42 #define BGSTR_MAX_LENGTH        32
43 
44 //============================================================================
45 //  Variable Definitions
46 //============================================================================
47 
48 /* Virtual screen */
49 static u16 vscr[32 * 32];
50 
51 /* Temporary OAM for V-Blank transfer */
52 static GXOamAttr oamBak[128];
53 
54 
55 /* For various rendering usages */
56 extern const u32 sampleFontCharData[8 * 0xe0];
57 extern const u16 samplePlttData[16][16];
58 
59 
60 //============================================================================
61 //  Function Definitions
62 //============================================================================
63 
64 /*---------------------------------------------------------------------------*
65   Name:         BgInitForPrintStr
66 
67   Description:  Initializes BG character drawing (Fixed to BG0, VRAM-C, BG mode 0).
68 
69   Arguments:    None.
70 
71   Returns:      None.
72  *---------------------------------------------------------------------------*/
BgInitForPrintStr(void)73 static void BgInitForPrintStr(void)
74 {
75     GX_SetBankForBG(GX_VRAM_BG_128_C);
76     G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, GX_BG_COLORMODE_16, GX_BG_SCRBASE_0xf800,      /* SCR base block 31 */
77                      GX_BG_CHARBASE_0x00000,    /* CHR base block 0 */
78                      GX_BG_EXTPLTT_01);
79     G2_SetBG0Priority(0);
80     G2_BG0Mosaic(FALSE);
81     GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
82     G2_SetBG0Offset(0, 0);
83 
84     GX_LoadBG0Char(sampleFontCharData, 0, sizeof(sampleFontCharData));
85     GX_LoadBGPltt(samplePlttData, 0, sizeof(samplePlttData));
86 
87     MI_CpuFillFast((void *)vscr, 0, sizeof(vscr));
88     DC_FlushRange(vscr, sizeof(vscr));
89     /* I/O register is accessed using DMA operation, so cache wait is not needed */
90     // DC_WaitWriteBufferEmpty();
91     GX_LoadBG0Scr(vscr, 0, sizeof(vscr));
92 }
93 
94 /*---------------------------------------------------------------------------*
95   Name:         ObjInitForPrintStr
96 
97   Description:  OBJ initialization (VRAM-B, 2D mode).
98 
99   Arguments:    None.
100 
101   Returns:      None.
102  *---------------------------------------------------------------------------*/
ObjInitForPrintStr(void)103 static void ObjInitForPrintStr(void)
104 {
105     GX_SetBankForOBJ(GX_VRAM_OBJ_128_B);
106     GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D);
107     MI_DmaFill32(3, oamBak, 0xc0, sizeof(oamBak));
108 
109     GX_LoadOBJ(sampleFontCharData, 0, sizeof(sampleFontCharData));
110     GX_LoadOBJPltt(samplePlttData, 0, sizeof(samplePlttData));
111 
112 }
113 
114 /*---------------------------------------------------------------------------*
115   Name:         VramClear
116 
117   Description:  Clear VRAM.
118 
119   Arguments:    None.
120 
121   Returns:      None.
122  *---------------------------------------------------------------------------*/
VramClear(void)123 static void VramClear(void)
124 {
125     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
126     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
127     (void)GX_DisableBankForLCDC();
128     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
129     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
130     MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);
131     MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);
132 
133 }
134 
135 /*---------------------------------------------------------------------------*
136   Name:         DispInit
137 
138   Description:  Initializes drawing.
139 
140   Arguments:    None.
141 
142   Returns:      None.
143  *---------------------------------------------------------------------------*/
DispInit(void)144 void DispInit(void)
145 {
146     /* Initialize drawing setting */
147     VramClear();
148     // Initialize OBJs
149     ObjInitForPrintStr();
150     // Initialize BG
151     BgInitForPrintStr();
152 
153     GX_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ);
154 
155 }
156 
157 
158 /*---------------------------------------------------------------------------*
159   Name:         DispOn
160 
161   Description:  Displays screen.
162 
163   Arguments:    None.
164 
165   Returns:      None.
166  *---------------------------------------------------------------------------*/
DispOn(void)167 void DispOn(void)
168 {
169     /* Start display */
170     GX_DispOn();
171     GXS_DispOn();
172 }
173 
174 /*---------------------------------------------------------------------------*
175   Name:         DispOff
176 
177   Description:  Hides screen.
178 
179   Arguments:    None.
180 
181   Returns:      None.
182  *---------------------------------------------------------------------------*/
DispOff(void)183 void DispOff(void)
184 {
185     /* Start display */
186     GX_DispOff();
187     GXS_DispOff();
188 }
189 
190 
191 
192 
193 /*---------------------------------------------------------------------------*
194   Name:         DispVBlankFunc
195 
196   Description:  Drawing V-Blank processing.
197 
198   Arguments:    None.
199 
200   Returns:      None.
201  *---------------------------------------------------------------------------*/
DispVBlankFunc(void)202 void DispVBlankFunc(void)
203 {
204     //---- OAM updating
205     DC_FlushRange(oamBak, sizeof(oamBak));
206     /* I/O register is accessed using DMA operation, so cache wait is not needed */
207     // DC_WaitWriteBufferEmpty();
208     MI_DmaCopy32(3, oamBak, (void *)HW_OAM, sizeof(oamBak));
209 
210     //---- BG screen update
211     DC_FlushRange(vscr, sizeof(vscr));
212     /* I/O register is accessed using DMA operation, so cache wait is not needed */
213     // DC_WaitWriteBufferEmpty();
214     GX_LoadBG0Scr(vscr, 0, sizeof(vscr));
215 }
216 
217 
218 
219 
220 /*---------------------------------------------------------------------------*
221   Name:         ObjSet
222 
223   Description:  Configures OBJ drawing settings.
224 
225   Arguments:    None.
226 
227   Returns:      None.
228  *---------------------------------------------------------------------------*/
ObjSet(s32 objNo,s32 x,s32 y,s32 charNo,s32 paletteNo)229 void ObjSet(s32 objNo, s32 x, s32 y, s32 charNo, s32 paletteNo)
230 {
231     G2_SetOBJAttr((GXOamAttr *)&oamBak[objNo],
232                   x,
233                   y,
234                   0,
235                   GX_OAM_MODE_NORMAL,
236                   FALSE,
237                   GX_OAM_EFFECT_NONE, GX_OAM_SHAPE_8x8, GX_OAM_COLOR_16, charNo, paletteNo, 0);
238 }
239 
240 /*---------------------------------------------------------------------------*
241   Name:         ObjSetString
242 
243   Description:  Draw characters with OBJ.
244 
245   Arguments:    None.
246 
247   Returns:      None.
248  *---------------------------------------------------------------------------*/
ObjSetString(s32 startobjNo,s32 x,s32 y,const char * string,s32 paletteNo)249 void ObjSetString(s32 startobjNo, s32 x, s32 y, const char *string, s32 paletteNo)
250 {
251     s32     i;
252     for (i = 0; (string[i] != '\0') && (i < 24); i++)
253     {
254         ObjSet(startobjNo + i, x + i * 8, y, (s32)string[i], paletteNo);
255     }
256 }
257 
258 /*---------------------------------------------------------------------------*
259   Name:         ObjClear
260 
261   Description:  Clears OBJ.
262 
263   Arguments:    None.
264 
265   Returns:      None.
266  *---------------------------------------------------------------------------*/
ObjClear(s32 objNo)267 void ObjClear(s32 objNo)
268 {
269     ObjSet(objNo, 256 * 8, 192 * 8, 0, 0);
270 }
271 
272 /*---------------------------------------------------------------------------*
273   Name:         ObjClearRange
274 
275   Description:  Clears OBJ (specified range).
276 
277   Arguments:    None.
278 
279   Returns:      None.
280  *---------------------------------------------------------------------------*/
ObjClearRange(s32 start,s32 end)281 void ObjClearRange(s32 start, s32 end)
282 {
283     s32     i;
284     for (i = start; i <= end; i++)
285     {
286         ObjClear(i);
287     }
288 }
289 
290 /*---------------------------------------------------------------------------*
291   Name:         ObjSetMessage
292 
293   Description:  Sets message (Displays 24 characters in the center bottom of screen).
294 
295   Arguments:    None.
296 
297   Returns:      None.
298  *---------------------------------------------------------------------------*/
ObjSetMessage(const char * message)299 void ObjSetMessage(const char *message)
300 {
301     ObjSetString(MESSAGE_OBJNO, 4 * 8, 22 * 8, message, 7);
302 }
303 
304 
305 /*---------------------------------------------------------------------------*
306   Name:         BgPutStringN
307 
308   Description:  Outputs N characters in BG.
309 
310   Arguments:    None.
311 
312   Returns:      None.
313  *---------------------------------------------------------------------------*/
BgPutStringN(s32 x,s32 y,s32 palette,const char * text,s32 num)314 void BgPutStringN(s32 x, s32 y, s32 palette, const char *text, s32 num)
315 {
316     s32     i;
317     if (num > BGSTR_MAX_LENGTH)
318     {
319         num = BGSTR_MAX_LENGTH;
320     }
321 
322     for (i = 0; i < num; i++)
323     {
324         if (text[i] == '\0')
325         {
326             break;
327         }
328         BgPutChar(x + i, y, palette, text[i]);
329     }
330 }
331 
332 /*---------------------------------------------------------------------------*
333   Name:         BgPutChar
334 
335   Description:  Outputs 1 BG character.
336 
337   Arguments:    None.
338 
339   Returns:      None.
340  *---------------------------------------------------------------------------*/
BgPutChar(s32 x,s32 y,s32 palette,s8 c)341 void BgPutChar(s32 x, s32 y, s32 palette, s8 c)
342 {
343     vscr[((y * 32) + x) % (32 * 32)] = (u16)((palette << 12) | c);
344 }
345 
346 /*---------------------------------------------------------------------------*
347   Name:         BgPutString
348 
349   Description:  Outputs 32 BG characters.
350 
351   Arguments:    None.
352 
353   Returns:      None.
354  *---------------------------------------------------------------------------*/
BgPutString(s32 x,s32 y,s32 palette,const char * text)355 void BgPutString(s32 x, s32 y, s32 palette, const char *text)
356 {
357     BgPutStringN(x, y, palette, text, BGSTR_MAX_LENGTH);
358 }
359 
360 /*---------------------------------------------------------------------------*
361   Name:         BgPrintStr
362 
363   Description:  BG formatted output.
364 
365   Arguments:    None.
366 
367   Returns:      None.
368  *---------------------------------------------------------------------------*/
BgPrintStr(s32 x,s32 y,s32 palette,const char * text,...)369 void BgPrintStr(s32 x, s32 y, s32 palette, const char *text, ...)
370 {
371     char    temp[(BGSTR_MAX_LENGTH + 1) * 2];
372     va_list vlist;
373 
374     MI_CpuClear8(temp, sizeof(temp));
375     va_start(vlist, text);
376     (void)vsnprintf(temp, sizeof(temp) - 1, text, vlist);
377     va_end(vlist);
378     BgPutString(x, y, palette, temp);
379 }
380 
381 /*---------------------------------------------------------------------------*
382   Name:         BgSetMessage
383 
384   Description:  Sets BG message.
385 
386   Arguments:    None.
387 
388   Returns:      None.
389  *---------------------------------------------------------------------------*/
BgSetMessage(s32 palette,const char * text,...)390 void BgSetMessage(s32 palette, const char *text, ...)
391 {
392     char    temp[(BGSTR_MAX_LENGTH + 1) * 2];
393     va_list vlist;
394 
395     MI_CpuClear8(temp, sizeof(temp));
396     va_start(vlist, text);
397     (void)vsnprintf(temp, sizeof(temp) - 1, text, vlist);
398     va_end(vlist);
399     BgPutString(4, 22, palette, temp);
400 }
401 
402 /*---------------------------------------------------------------------------*
403   Name:         BgClear
404 
405   Description:  Clears BG.
406 
407   Arguments:    None.
408 
409   Returns:      None.
410  *---------------------------------------------------------------------------*/
BgClear(void)411 void BgClear(void)
412 {
413     MI_CpuClearFast(vscr, sizeof(vscr));
414 }
415 
416 
417 /*---------------------------------------------------------------------------*
418   Name:         BgColorString
419 
420   Description:  Changes the color of character strings printed on the virtual screen.
421 
422   Arguments:    x: X-coordinate (x 8 dots ) from which to start color change
423                 y: Y-coordinate (x 8 dots ) from which to start color change
424                 length: Number of characters to continue the color change for
425                 palette: Specify text color by palette number
426 
427   Returns:      None.
428  *---------------------------------------------------------------------------*/
BgColorString(s16 x,s16 y,s16 length,u8 palette)429 void BgColorString(s16 x, s16 y, s16 length, u8 palette)
430 {
431     s32     i;
432     u16     temp;
433     s32     index;
434 
435     if (length < 0)
436     {
437         return;
438     }
439 
440     for (i = 0; i < length; i++)
441     {
442         index = ((y * 32) + x + i) % (32 * 32);
443         temp = vscr[index];
444         temp &= 0x0fff;
445         temp |= (palette << 12);
446         vscr[index] = temp;
447     }
448 }
449