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