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