1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - relayStation-2
3   File:     dispfunc.c
4 
5   Copyright 2005-2009 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:: 2007-11-15#$
14   $Rev: 2414 $
15   $Author: hatamoto_minoru $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro.h>
19 #include <string.h>
20 
21 #include "dispfunc.h"
22 
23 
24 /*
25 	=================================================================================
26 
27 	Background character rendering (fixed to BG0)
28 
29 	=================================================================================
30 */
31 
32 
33 #define BG_LINES	(24)
34 #define PROMPT		">"
35 
36 // --- Static value
37 static u16 vscr[32 * 32] ATTRIBUTE_ALIGN(32);   // Virtual screen
38 static u16 vscr_sub[32 * 32] ATTRIBUTE_ALIGN(32);       // Virtual screen (sub)
39 static u16 cur_line = 0;
40 
41 // --- Static functions
42 static void BgiPutChar(int x, int y, u8 palette, char c, u8 lcd);
43 static void BgiPutStringN(int x, int y, u8 palette, const char *text, u32 num, u8 lcd);
44 
45 
BgiPutChar(int x,int y,u8 palette,char c,u8 lcd)46 static void BgiPutChar(int x, int y, u8 palette, char c, u8 lcd)
47 {
48     if (lcd)
49     {
50         vscr[((y * 32) + x) % (32 * 32)] = (u16)((palette << 12) | c);
51     }
52     else
53     {
54         vscr_sub[((y * 32) + x) % (32 * 32)] = (u16)((palette << 12) | c);
55     }
56 }
57 
BgiPutStringN(int x,int y,u8 palette,const char * text,u32 num,u8 lcd)58 static void BgiPutStringN(int x, int y, u8 palette, const char *text, u32 num, u8 lcd)
59 {
60     s32     i;
61 
62     for (i = 0; i < num; i++)
63     {
64         if (text[i] == 0x00)
65         {
66             break;
67         }
68 
69         BgiPutChar((s16)(x + i), y, palette, text[i], lcd);
70     }
71 
72 }
73 
74 /*
75 	--- Initialize
76 */
BgInitForPrintStr(void)77 void BgInitForPrintStr(void)
78 {
79     // Main LCD
80     GX_SetBankForBG(GX_VRAM_BG_64_E);
81     G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, GX_BG_COLORMODE_16, GX_BG_SCRBASE_0xf000,      // SCR base block 31
82                      GX_BG_CHARBASE_0x00000,    // CHR base block 0
83                      GX_BG_EXTPLTT_01);
84     G2_SetBG0Priority(0);
85     G2_BG0Mosaic(FALSE);
86     GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_2D);
87 
88     GX_LoadBG0Char(sampleCharData, 0, sizeof(sampleCharData));
89     GX_LoadBGPltt(samplePlttData, 0, sizeof(samplePlttData));
90 
91     MI_CpuFillFast((void *)vscr, 0, sizeof(vscr));
92     DC_FlushRange(vscr, sizeof(vscr));
93     /* I/O register is accessed using DMA operation, so cache wait is not needed */
94     // DC_WaitWriteBufferEmpty();
95     GX_LoadBG0Scr(vscr, 0, sizeof(vscr));
96 
97     // Sub LCD
98     GX_SetBankForSubBG(GX_VRAM_SUB_BG_128_C);
99     G2S_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, GX_BG_COLORMODE_16, GX_BG_SCRBASE_0xf800,     // SCR base block 31
100                       GX_BG_CHARBASE_0x10000,   // CHR base block 0
101                       GX_BG_EXTPLTT_01);
102     G2S_SetBG0Priority(0);
103     G2S_BG0Mosaic(FALSE);
104     GXS_SetGraphicsMode(GX_BGMODE_0);
105 
106     GXS_LoadBG0Char(sampleCharData, 0, sizeof(sampleCharData));
107     GXS_LoadBGPltt(samplePlttData, 0, sizeof(samplePlttData));
108     MI_CpuFillFast((void *)vscr_sub, 0, sizeof(vscr_sub));
109     DC_FlushRange(vscr_sub, sizeof(vscr_sub));
110     /* I/O register is accessed using DMA operation, so cache wait is not needed */
111     // DC_WaitWriteBufferEmpty();
112     GXS_LoadBG0Scr(vscr_sub, 0, sizeof(vscr_sub));
113 
114     cur_line = 0;
115 }
116 
117 /*
118 	--- For main LCD
119 */
120 
121 // Formatted string output
BgPrintf(int x,int y,u8 palette,const char * text,...)122 void BgPrintf(int x, int y, u8 palette, const char *text, ...)
123 {
124     va_list vlist;
125     char    temp[32 * 2 + 2];
126 
127     MI_CpuClear8(&temp[0], sizeof(temp));
128 
129     va_start(vlist, text);
130     (void)OS_VSNPrintf(temp, 32 * 2 + 2, text, vlist);
131     va_end(vlist);
132 
133     BgPutString(x, y, palette, temp);
134 }
135 
136 
137 // String output
BgPutString(int x,int y,u8 palette,const char * text)138 void BgPutString(int x, int y, u8 palette, const char *text)
139 {
140     BgPutStringN(x, y, palette, text, 32);
141 }
142 
143 // String output with the character number restriction
BgPutStringN(int x,int y,u8 palette,const char * text,u32 num)144 void BgPutStringN(int x, int y, u8 palette, const char *text, u32 num)
145 {
146     BgiPutStringN(x, y, palette, text, num, 1);
147 }
148 
149 
150 // 1 character output
BgPutChar(int x,int y,u8 palette,char c)151 void BgPutChar(int x, int y, u8 palette, char c)
152 {
153     BgiPutChar(x, y, palette, c, 1);
154 }
155 
156 // Clear background
BgClear(void)157 void BgClear(void)
158 {
159     MI_CpuClearFast((void *)vscr, sizeof(vscr));
160 }
161 
162 /*
163 	--- V-Blank process
164 */
165 
BgScrSetVblank(void)166 void BgScrSetVblank(void)
167 {
168     // Reflect virtual screen in VRAM
169     DC_FlushRange(vscr, sizeof(vscr));
170     /* I/O register is accessed using DMA operation, so cache wait is not needed */
171     // DC_WaitWriteBufferEmpty();
172     GX_LoadBG0Scr(vscr, 0, sizeof(vscr));
173     DC_FlushRange(vscr_sub, sizeof(vscr_sub));
174     /* I/O register is accessed using DMA operation, so cache wait is not needed */
175     // DC_WaitWriteBufferEmpty();
176     GXS_LoadBG0Scr(vscr_sub, 0, sizeof(vscr_sub));
177 }
178