1 /*---------------------------------------------------------------------------*
2   Project:  Simple Color Test
3   File:     color.c
4 
5   Copyright 1998, 1999 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   $Log: color.c,v $
14   Revision 1.3  2008/06/26 02:54:40  urata
15   Removed MPAL defines
16 
17   Revision 1.2  2006/02/20 04:13:13  mitu
18   Changed include path from dolphin/ to revolution/.
19 
20   Revision 1.1  2005/12/16 08:34:26  urata
21   Initial check-in.
22 
23 
24     9     3/01/00 5:41p Hashida
25     Moved NTSC definition from makefile to .c's
26 
27     8     2/18/00 10:46a Hashida
28     Changed the code suitable for demos.
29 
30     7     1/28/00 10:37p Hashida
31     Added VIFlush.
32     Changed to wait for two frames when switching mode.
33 
34     6     1/26/00 3:56p Hashida
35     Changed to use VIConfigure.
36 
37     5     1/21/00 2:15a Hashida
38     Make it work for non-interlace mode
39 
40     4     1/12/00 6:56p Hashida
41     VI_DEBUG is not allowed as an argument for VIConfigureTVScreen
42     (the API decides whether or not debug mode should be used).
43 
44     3     1/11/00 11:06a Hashida
45     It doesn't work temporarily so fixed.
46 
47     2     12/24/99 11:12a Hashida
48     Fixed initial values.
49 
50     1     12/02/99 4:05p Hashida
51     Initial revision
52 
53     2     11/30/99 7:01p Hashida
54     Added a new test for single field frame buffer.
55 
56     2     11/17/99 3:31p Hashida
57     Removed DS mode test when Marlin since Marlin doesn't support DS mode
58     for now.
59 
60 
61     1     11/12/99 5:24p Hashida
62     Initial revision.
63 
64   $NoKeywords: $
65  *---------------------------------------------------------------------------*/
66 
67 #include <revolution.h>
68 
69 #define NTSC      // if you change here, you can see PAL
70 /* #define NON_INTERLACE */   // if you define this, you can see non-interlace
71 
72 #if defined(NTSC)
73 
74 #ifndef NON_INTERLACE
75 GXRenderModeObj* rmode = &GXNtsc480Int;
76 #else
77 GXRenderModeObj* rmode = &GXNtsc240Ds;
78 #endif
79 
80 #elif defined(PAL)
81 
82 #ifndef NON_INTERLACE
83 GXRenderModeObj* rmode = &GXPal528Int;
84 #else
85 GXRenderModeObj* rmode = &GXPal264Ds;
86 #endif
87 
88 #endif
89 
90 // for X frame buffer
91 static u8*  xfb1;
92 static u8*  xfb2;
93 
94 typedef struct
95 {
96     u32         Y;
97     u32         Cb;
98     u32         Cr;
99 
100 } Color_s;
101 
102 Color_s color[] = {
103     { 180, 128, 128 },
104     { 162,  44, 142 },
105     { 131, 156,  44 },
106     { 112,  72,  58 },
107     {  84, 184, 198 },
108     {  65, 100, 212 },
109     {  35, 212, 114 },
110     {  16, 128, 128 },
111 };
112 
113 void allocateFB(u32 fbSize);
114 void fillColor(u32 code, u32 fbSize, u8* xfb);
115 
116 /*---------------------------------------------------------------------------*
117     Name:               allocateFB
118 
119     Description:        Allocates memory for two frame buffers
120 
121     Arguments:          fbSize      frame buffer size
122 
123     Returns:            None.
124  *---------------------------------------------------------------------------*/
allocateFB(u32 fbSize)125 void allocateFB(u32 fbSize)
126 {
127     void*       arenaLo;
128 
129     arenaLo = OSGetArenaLo();
130 
131     // allocate memory for frame buffer here.
132     xfb1 = (u8*)OSRoundUp32B(arenaLo);
133     xfb2 = (u8*)OSRoundUp32B(xfb1 + fbSize);
134 
135     arenaLo = (void*)(xfb1 + 2 * fbSize);
136     OSSetArenaLo(arenaLo);
137 }
138 
139 /*---------------------------------------------------------------------------*
140     Name:               fillColor
141 
142     Description:        fill frame buffer by a single color
143 
144     Arguments:          code        color code
145                         fbSize      size of the frame buffer
146                         xfb         frame buffer address
147 
148     Returns:            None.
149  *---------------------------------------------------------------------------*/
fillColor(u32 code,u32 fbSize,u8 * xfb)150 void fillColor(u32 code, u32 fbSize, u8* xfb)
151 {
152     u32         colorVal;
153     u8*         ptr;
154 
155     colorVal = (color[code].Y << 24)
156         + (color[code].Cb << 16)
157         + (color[code].Y << 8)
158         + color[code].Cr;
159 
160     for (ptr = xfb; ptr < xfb + fbSize; ptr += VI_DISPLAY_PIX_SZ * 2)
161         *(u32*)ptr = colorVal;
162 
163     DCStoreRange((void*)xfb, fbSize);
164 }
165 
main(void)166 void main(void)
167 {
168     u32         frame;
169     u32         code;
170     u32         fbSize;
171     u8*         xfb;
172     u32         first;
173 
174     OSInit();
175     VIInit();
176 
177     // Calculate frame buffer size.
178     // Note that each line width should be a multiple of 16.
179     fbSize = (u32)(VIPadFrameBufferWidth(rmode->fbWidth)
180         * rmode->xfbHeight * VI_DISPLAY_PIX_SZ);
181 
182     allocateFB(fbSize);
183 
184     VIConfigure(rmode);
185 
186     // Need to "flush" so that the VI changes so far takes effect
187     // from the following field
188     VIFlush();
189     VIWaitForRetrace();
190 
191     // Since the TV mode is interlace after VIInit,
192     // we need to wait for one more frame to make sure
193     // that the mode is switched from interlace to non-interlace
194 #ifdef NON_INTERLACE
195     VIWaitForRetrace();
196 #endif
197 
198     first = 1;
199     frame = 0;
200     code = 0;
201 
202     while(1)
203     {
204         xfb = (frame & 0x1)? xfb2 : xfb1;
205 
206         if (frame % 60 == 0)
207         {
208             code = (code + 1) % 8;
209         }
210 
211         fillColor(code, fbSize, xfb);
212 
213         VISetNextFrameBuffer((void*)xfb);
214 
215         if (first == 1)
216         {
217             VISetBlack(FALSE);
218             first = 0;
219         }
220 
221         VIFlush();
222         VIWaitForRetrace();
223 
224         frame++;
225     }
226 }
227