videocb.c
8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "ultra64.h"
#include "os_bb.h"
#include "bcp.h"
#include "videocb.h"
#include "graph.h"
// #define PAL 1
#define DMA_QUEUE_SIZE 200
/*
* Thread and stack structures
*/
char bootStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread idleThread;
static char idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread mainThread;
static char mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
/*
* Message queues and message buffers used by this app
*/
static OSMesg PiMessages[DMA_QUEUE_SIZE], dmaMessageBuf;
static OSMesgQueue PiMessageQ, dmaMessageQ;
static OSMesg SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue SiMessageQ, contMessageQ;
static u32 cfb[640*480] __attribute__((aligned(64)));
static OSMesgQueue retraceMessageQ;
static OSMesg dummyMessage, retraceMessageBuf;
static int numTests = 0;
static int current = 0;
static int imageMode = 0;
static char testImage[20][32];
OSContStatus statusData[MAXCONTROLLERS];
OSContPad controllerData[MAXCONTROLLERS];
static int no_controller = 1;
static u16 lastbutton[MAXCONTROLLERS];
static u32 viHighResMode;
static u32 viLowResMode;
/*
* Local variables and routines
*/
static void idleproc(char *);
static void mainproc(char *);
void
boot(void)
{
osInitialize();
osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
idleThreadStack+STACKSIZE, 8);
osStartThread(&idleThread);
}
static void
idleproc(char *argv) /* priority 8 */
{
#ifdef PAL
viHighResMode = OS_VI_PAL_HPF2;
viLowResMode = OS_VI_PAL_LPN1;
__osBbVideoPllInit(OS_TV_PAL);
#else
viHighResMode = OS_VI_NTSC_HPF2;
viLowResMode = OS_VI_NTSC_LPN1;
__osBbVideoPllInit(OS_TV_NTSC);
#endif
osCreateViManager(OS_PRIORITY_VIMGR);
osViSetMode(&osViModeTable[viLowResMode]);
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);
osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);
/*
* The main thread's priority must be the same or lower than the original
* idle's thread priority. This allows the idle thread to change its
* priority to 0 before the main thread starts execution.
*/
osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
mainThreadStack+STACKSIZE/8, (OSPri)7);
osStartThread(&mainThread);
osSetThreadPri(0, OS_PRIORITY_IDLE);
for(;;); /* idle thread */
}
static void drawLine(int i)
{
printstr(i == current ? GPACK_RGBA5551(255,0,255,1): white, i >= numTests/2 ? 20 : 5, 4+(i>=numTests/2?i-numTests/2:i), testImage[i]);
}
static void redraw(void) {
int i;
char *title = "J&B's Video Tests";
// Make sure mode gets reset
osViSetMode(&osViModeTable[viLowResMode]);
gcls();
printstr(markcolor, 4+16-strlen(title)/2, 2, title);
for (i = 0; i < numTests; ++i) {
drawLine(i);
}
osWritebackDCacheAll();
}
static u32 bars[8] = {0xbfbfbf00, // grey
0xbfbf0000, // yellow
0x00bfbf00, // cyan
0x00bf0000, // green
0xbf00bf00, // magenta
0xbf000000, // red
0x0000bf00, // blue
0x00000000 // black
};
static void colorBars()
{
int i, j;
for (i=0; i<640; i++) {
for (j=0; j<480; j++) {
cfb[j*640 + i] = bars[(i*8) / 640];
}
}
}
static void clear32(u32 color)
{
int i, j;
for (i=0; i<640; i++) {
for (j=0; j<480; j++) {
cfb[j*640 + i] = color;
}
}
}
static void resTest(u32 c0, u32 c1)
{
float endWidth = 20.0;
float lineWidth;
u32 lineCount = 0;
int i, j;
for (j=0; j<480; j++) {
i = 0;
while (i<640) {
lineWidth = 1.0 + i*(endWidth-1)/640;
lineCount = 0;
while ((lineCount < (u32) lineWidth) && (i<640)) {
cfb[j*640 + i] = c0;
i++;
lineCount++;
}
lineCount = 0;
while ((lineCount < (u32) lineWidth) && (i<640)) {
cfb[j*640 + i] = c1;
i++;
lineCount++;
}
}
}
}
static void fieldTest(u32 c0, u32 c1)
{
// Alternating c0, c1 vertically
// XXX Need to set the video mode differently for this test
int i, j;
for (j=0; j<480; j++) {
for (i=0; i<640; i++) {
if (j & 1) {
cfb[j*640 + i] = c0;
} else {
cfb[j*640 + i] = c1;
}
}
}
}
static void ramp(u32 c0, u32 c1)
{
int i, j;
u32 r0, g0, b0;
u32 r1, g1, b1;
u32 rgb;
r0 = (c0 >> 24) & 0xff;
g0 = (c0 >> 16) & 0xff;
b0 = (c0 >> 8) & 0xff;
r1 = (c1 >> 24) & 0xff;
g1 = (c1 >> 16) & 0xff;
b1 = (c1 >> 8) & 0xff;
for (j=0; j<480; j++) {
for (i=0; i<640; i++) {
rgb = (r0 + ((r1 - r0)*i) / 640) << 24;
rgb |= ((g0 + ((g1 - g0)*i) / 640) & 0xff) << 16;
rgb |= ((b0 + ((b1 - b0)*i) / 640) & 0xff) << 8;
cfb[j*640 + i] = rgb;
}
}
}
static void drawImage(void)
{
/*
* Switch video mode
*/
osViSetMode(&osViModeTable[viHighResMode]);
switch (current) {
case 0:
// Color bars
colorBars();
break;
case 1:
// White
clear32(0xffffff00);
break;
case 2:
// Black
clear32(0x0);
break;
case 3:
// W&B lines
resTest(0xffffff00, 0x0);
break;
case 4:
// R&B lines
resTest(0xbf000000, 0x0);
break;
case 5:
// W&B fields
fieldTest(0xffffff00, 0x0);
break;
case 6:
// B&W Ramp
ramp(0x0, 0xbfbfbf00);
break;
case 7:
// B&Y Ramp
ramp(0x0, 0xbfbf0000);
break;
case 8:
// 75 % red
clear32(0xbf000000);
break;
default:
break;
}
}
static void initController(void)
{
int i;
u8 pattern;
osCreateMesgQueue(&contMessageQ, &dummyMessage, 1);
osSetEventMesg(OS_EVENT_SI, &contMessageQ, (OSMesg) 0);
osContInit(&contMessageQ, &pattern, &statusData[0]);
for (i = 0; i < MAXCONTROLLERS; i++) {
if ((pattern & (1 << i)) &&
!(statusData[i].errno & CONT_NO_RESPONSE_ERROR)) {
no_controller = 0;
}
}
}
static void processButton(u16 button, int lastbutton)
{
int old = current;;
if (current == -1) return;
// If we are in the high res mode just get out of it and return;
if (imageMode && (lastbutton != button)) {
imageMode = 0;
redraw();
return;
}
if (button & D_JPAD && !(lastbutton & D_JPAD)) {
current = (current + 1) % numTests;
}
if (button & U_JPAD && !(lastbutton & U_JPAD)) {
current = (current - 1) < 0? numTests - 1: current-1;
}
if (button & R_JPAD && !(lastbutton & R_JPAD)) {
if (current + numTests/2 < numTests) {
current += numTests/2;
}
}
if (button & L_JPAD && !(lastbutton & L_JPAD)) {
if (current >= numTests/2) {
current -= numTests/2;
}
}
if ((button & A_BUTTON && !(lastbutton & A_BUTTON)) ||
(button & START_BUTTON && !(lastbutton & START_BUTTON))) {
printstr(red, 4, 13, "Launching: ");
printstr(red, 15, 13, testImage[current]);
drawImage();
imageMode = 1;
osWritebackDCacheAll();
}
if (old != current) {
redraw();
}
}
static void readControllers(void)
{
int i;
OSContPad *pad;
osRecvMesg(&contMessageQ, &dummyMessage, OS_MESG_BLOCK);
osContGetReadData(controllerData);
for (i = 0; i < MAXCONTROLLERS; ++i) {
pad = &controllerData[i];
if (pad->button) {
processButton(pad->button, lastbutton[i]);
}
lastbutton[i] = pad->button;
}
}
static void
mainproc(char *argv)
{
osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
osViSetEvent(&retraceMessageQ, dummyMessage, 1);
initController();
osViBlack(1);
osViSwapBuffer(cfb);
init_graphics(LOW_RES);
strcpy(testImage[numTests++], "Color bars");
strcpy(testImage[numTests++], "White");
strcpy(testImage[numTests++], "Black");
strcpy(testImage[numTests++], "W&B lines");
strcpy(testImage[numTests++], "R&W lines");
strcpy(testImage[numTests++], "W&B fields");
strcpy(testImage[numTests++], "B&W Ramp");
strcpy(testImage[numTests++], "B&Y Ramp");
strcpy(testImage[numTests++], "75% Red");
redraw();
for(;;) {
osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
osContStartReadData(&contMessageQ);
readControllers();
}
}