trivial.c
13.4 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*---------------------------------------------------------------------*
Copyright (C) 1998 Nintendo. (Originated by SGI)
$RCSfile: trivial.c,v $
$Revision: 1.1.1.1 $
$Date: 2002/05/02 03:27:20 $
*---------------------------------------------------------------------*/
/*
* Trivial (at least compared to simple) Ultra-64 program
*
* Uses a Z-buffer, and does double-buffering of the color buffers, to
* allow overlap of CPU work with rendering.
*
* Bill Mark, June 1995
*/
#include <ultra64.h>
#include "trivial.h"
#include "walk_around.h"
/*
* Bar graph start position
*/
#define BARG_START_X 50
#define BARG_START_Y 200
extern u32 MyMode;
extern u32 RotVal;
extern u32 nDL;
/*
* Are we debugging?
*/
#define DEBUG_FLAG 0
/*
* Threads
*/
static OSThread mainThread;
static OSThread gameThread;
static OSThread rmonThread;
/*
* Thread priorities
*/
#define MAIN_PRIORITY 10
#define GAME_PRIORITY 10
/*
* Thread ID's -- Used only with debugger and for fault messages --
* * they're arbitrary
*/
#define MAIN_ID 3
#define GAME_ID 4
u64 bootStack[STACKSIZE / 8];
u64 mainStack[STACKSIZE / 8];
u64 gameStack[STACKSIZE / 8];
u64 rmonStack[STACKSIZE / 8];
/*
* Message queue lengths
*/
#define PI_MSGQUEUE_SIZE 8
#define DMA_MSGQUEUE_SIZE 1
#define RDPDONE_MSGQUEUE_SIZE 1
#define RETRACE_MSGQUEUE_SIZE 1
/*
* Message queues
*/
static OSMesg PiMessages[PI_MSGQUEUE_SIZE];
static OSMesgQueue PiMessageQ;
static OSMesg DMAMessages[DMA_MSGQUEUE_SIZE];
static OSMesgQueue DMAMessageQ;
static OSMesg RDPDoneMessages[RDPDONE_MSGQUEUE_SIZE];
static OSMesgQueue RDPDoneMessageQ;
static OSMesg RetraceMessages[RETRACE_MSGQUEUE_SIZE];
static OSMesgQueue RetraceMessageQ;
/*
* Dummy message
*/
static OSMesg dummyMsg;
/*
* Symbols generated by "makerom" to tell us where various segments are
* in cartridge ROM, and in RDRAM.
*/
extern char _rsp_staticSegmentRomStart[],
_rsp_staticSegmentRomEnd[];
extern char _cfbSegmentStart[];
extern char _codeSegmentEnd[];
/*
* Display lists defined in static.c
*/
extern Gfx rspinit_dl[];
extern Gfx rdpinit_dl[];
extern Gfx scrnclr_dl[];
extern Gfx shadeoverlaptri_dl[];
extern Gfx texrect_dl[];
/*
* Z-Buffer from zbuf.c; Color Frame Bufs from cfb.c
*/
extern unsigned short zbuffer[];
extern unsigned short cfb_A[];
extern unsigned short cfb_B[];
/*
* Double-buffered data structures
* Must be declared global (rather than local to a routine) so that they
* don't disappear while they're in use when routine exits.
*/
Dynamic dynamic[2];
OSTask task[2];
Gfx *glistp;
/*
* Bar Graph array
*/
#define MAX_BARS 100
/* point to current entry */
static u32 CurBar = 0;
struct {
u8 c[4];
u8 cid;
} BarGraph[MAX_BARS];
static u32 PerfCounters[4];
OSPiHandle *handler;
/*
* Protypes for routines in this file
*/
void boot(void *arg);
static void mainproc(void *arg);
static void gameproc(void *arg);
static void gameloop(char *rsp_static_addr);
/*
* Simple pseudo-random number generator
*/
#define DEFAULT_SEED 12344
static int RandNum = DEFAULT_SEED & (~1);
void
set_random_seed( int seed )
{
RandNum = seed & (~1);
}
int
get_random( void )
{
RandNum *= (RandNum - 1);
return(RandNum);
}
/*
* boot -- first routine called
*/
/*
* Note: see boot() in simple.c to see how to parse args from "gload"
*/
void
boot(void *arg)
{
osInitialize();
handler = osCartRomInit();
osCreateThread(&mainThread, MAIN_ID, (void (*)(void *)) mainproc, arg,
(void *) (mainStack + STACKSIZE / 8), (OSPri) MAIN_PRIORITY);
osStartThread(&mainThread);
}
/*
* mainproc -- invoked by boot. Sets up, then becomes idle thread.
*/
static void
mainproc(void *arg)
{
/*
* Note: Not using TLB -- all our R4300 address are KSEG0 (physical)
*/
osCreateThread(&gameThread, GAME_ID, gameproc, arg,
(void *) (gameStack + STACKSIZE / 8), GAME_PRIORITY);
/*
* Start VI Mgr (Video)
*/
osCreateViManager(OS_PRIORITY_VIMGR);
/*
* Set video mode to Low Res; Anti-Aliased; Non-interlaced; 16 bits/pixel
* * Related values: 1) #define's for SCREEN_WD and HT in trivial_scrnsize.h
* * 2) Element sizes in cfb.c
* * 3) Address for cfb in 'spec'
* * 4) gsDP clear instructions in static.c
*/
osViSetMode(&osViModeTable[OS_VI_NTSC_LAN1]);
/*
* Start PI Mgr for access to cartridge
* * This call creates the message queue, so we don't have to.
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
PI_MSGQUEUE_SIZE);
/*
* Create and start rmon thread
*/
osCreateThread(&rmonThread, 0, rmonMain, (void *) 0,
(void *) (rmonStack + STACKSIZE / 8), (OSPri) OS_PRIORITY_RMON);
osStartThread(&rmonThread);
if (!DEBUG_FLAG)
osStartThread(&gameThread);
/*
* Become the idle thread and reqlinquish CPU
*/
osSetThreadPri(NULL, 0);
for (;;) ;
}
/*
* gameproc -- entry point for permanently running primary game thread
*/
static void
gameproc(void *arg)
{
char *rsp_static_addr;
char *rsp_static_end;
int rsp_static_len;
OSIoMesg dmaMb;
/*
* Message Queue for OS messages indicating DMA completions
*/
osCreateMesgQueue(&DMAMessageQ, DMAMessages, DMA_MSGQUEUE_SIZE);
/*
* Message Queue for OS messages indicating RDP done
*/
osCreateMesgQueue(&RDPDoneMessageQ, RDPDoneMessages, RDPDONE_MSGQUEUE_SIZE);
osSetEventMesg(OS_EVENT_DP, &RDPDoneMessageQ, dummyMsg);
/*
* Message Queue for OS messages indicating vertical retrace
*/
osCreateMesgQueue(&RetraceMessageQ, RetraceMessages, RETRACE_MSGQUEUE_SIZE);
osViSetEvent(&RetraceMessageQ, dummyMsg, 1);
/*
* Load the 'rsp_static' segment from the cartridge, and place it
* right after the code/data segment, checking first to make sure that
* this won't overwrite the CFB's.
*/
rsp_static_len = _rsp_staticSegmentRomEnd - _rsp_staticSegmentRomStart;
rsp_static_addr = _codeSegmentEnd;
rsp_static_end = _codeSegmentEnd + rsp_static_len;
/*
* Note: dmaMb is working space used by osPiStartDma
*/
dmaMb.hdr.pri = OS_MESG_PRI_NORMAL;
dmaMb.hdr.retQueue = &DMAMessageQ;
dmaMb.dramAddr = rsp_static_addr;
dmaMb.devAddr = (u32)_rsp_staticSegmentRomStart;
dmaMb.size = rsp_static_len;
osEPiStartDma(handler, &dmaMb, OS_READ);
osRecvMesg(&DMAMessageQ, NULL, OS_MESG_BLOCK);
/*
* Run the main loop of the game
*/
gameloop(rsp_static_addr);
}
/*
* Display running perf meter bar graph
*/
static void
draw_bar_graph( void )
{
u32 cnt, x0, x1, y0, y1;
int i, j, cid;
cnt = 0;
i = CurBar;
for(j = 0; j < 3; j++) {
/* rmonPrintf("count[%d] = %08x ", j, PerfCounters[j]); */
BarGraph[CurBar].c[j] = MAX(1, MIN((PerfCounters[j] >> 14), 0x3f));
}
BarGraph[CurBar].c[3] = MAX(1, MIN((PerfCounters[3] >> 6), 0x3f));
/* rmonPrintf("\n"); */
if(MyMode < 4)
BarGraph[CurBar].cid = MyMode;
/* set bar graph color, modes */
gDPPipeSync(glistp++);
gDPSetCombineMode(glistp++, G_CC_PRIMITIVE, G_CC_PRIMITIVE);
CurBar++;
if(CurBar == MAX_BARS)
CurBar = 0;
if(MyMode < 4) {
while(cnt < MAX_BARS) {
cid = BarGraph[i].cid;
x0 = BARG_START_X + cnt*2;
y0 = BARG_START_Y - BarGraph[i].c[cid];
x1 = x0 + 2;
y1 = BARG_START_Y;
gDPSetPrimColor(glistp++, 0, 0, 255, 255, 255, 255);
gDPFillRectangle(glistp++,
BARG_START_X,
BARG_START_Y - 64,
BARG_START_X + MAX_BARS*2,
BARG_START_Y - 63);
switch(cid) {
case 0:
gDPSetPrimColor(glistp++, 0, 0, 255, 0, 255, 255);
break;
case 1:
gDPSetPrimColor(glistp++, 0, 0, 255, 255, 0, 255);
break;
case 2:
gDPSetPrimColor(glistp++, 0, 0, 0, 0, 255, 255);
break;
case 3:
gDPSetPrimColor(glistp++, 0, 0, 255, 0, 0, 255);
break;
}
gDPFillRectangle(glistp++, x0, y0, x1, y1);
i--;
if(i < 0) {
i = MAX_BARS - 1;
}
cnt++;
}
} else {
/* dynamic bar graph mode */
x0 = BARG_START_X;
y0 = BARG_START_Y;
x1 = x0 + BarGraph[i].c[0];
y1 = BARG_START_Y + 20;
gDPSetPrimColor(glistp++, 0, 0, 255, 0, 255, 255);
gDPFillRectangle(glistp++, x0, y0, x1, y1);
x0 = x1;
x1 = x0 + BarGraph[i].c[1];
gDPSetPrimColor(glistp++, 0, 0, 255, 255, 0, 255);
gDPFillRectangle(glistp++, x0, y0, x1, y1);
x0 = x1;
x1 = x0 + BarGraph[i].c[2];
gDPSetPrimColor(glistp++, 0, 0, 0, 0, 255, 255);
gDPFillRectangle(glistp++, x0, y0, x1, y1);
x0 = x1;
x1 = x0 + BarGraph[i].c[3];
gDPSetPrimColor(glistp++, 0, 0, 255, 0, 0, 255);
gDPFillRectangle(glistp++, x0, y0, x1, y1);
}
}
/*
* gameloop -- main loop of game
* Called by gameproc; runs in gameThread.
* Parameter: rsp_static_addr, the R4300 address where the
* rsp_static segment was loaded
*/
static void
gameloop(char *rsp_static_addr)
{
int oddframe = 0, i;
Dynamic *generate;
Gfx *glistp0;
OSTask *gentask;
float rotval = 0.0;
OSTime mystart,
myend,
diff;
walkAroundInit(0, 0, 0);
while (1) {
/*
* Set up pointers to dynamic stuff. We're always generating one set
* of dynamic data and drawing another.
*/
generate = &(dynamic[(oddframe ? 0 : 1)]);
walkAround(generate);
/*
* glist portion of generated dynamic data
*/
glistp0 = &(generate->glist[0]);
glistp = glistp0; /*
* This one will be incremented as list is built
*/
/*
* Tell RSP where each segment is
*/
gSPSegment(glistp++, 0, 0x0); /*
* Physical address segment
*/
/*
* Static segment (mapping never changes)
*/
gSPSegment(glistp++, STATIC_SEG, OS_K0_TO_PHYSICAL(rsp_static_addr));
/*
* Dynamic segment (mapping changes every frame)
*/
gSPSegment(glistp++, DYNAMIC_SEG, OS_K0_TO_PHYSICAL(generate));
/*
* RSP and RDP setup, and screen clear
*/
gSPDisplayList(glistp++, rspinit_dl);
gSPDisplayList(glistp++, rdpinit_dl);
gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD,
OS_K0_TO_PHYSICAL(oddframe ? cfb_A : cfb_B));
gSPDisplayList(glistp++, scrnclr_dl);
/*
* Must set color image again after zbuffer clear
*/
gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD,
OS_K0_TO_PHYSICAL(oddframe ? cfb_A : cfb_B));
/*
* Set Z-buffer
*/
gDPSetDepthImage(glistp++, OS_K0_TO_PHYSICAL(zbuffer));
/*
* Set up matricies, then draw the triangle display list
*/
guOrtho(&(generate->projection),
-SCREEN_WD / 2.0, SCREEN_WD / 2.0,
-SCREEN_HT / 2.0, SCREEN_HT / 2.0, 1, 10, 1.0);
guRotate(&(generate->modeling), rotval, 0.0, 0.0, 1.0);
rotval += RotVal;
if (rotval >= 360.0)
rotval = 0.0;
gSPMatrix(glistp++, OS_K0_TO_PHYSICAL(&(generate->projection)),
G_MTX_PROJECTION | G_MTX_LOAD | G_MTX_NOPUSH);
gSPMatrix(glistp++, OS_K0_TO_PHYSICAL(&(generate->modeling)),
G_MTX_MODELVIEW | G_MTX_LOAD | G_MTX_NOPUSH);
guRotate(&(generate->increment), 9.0, 0.0, 0.0, 1.0);
for (i = 0; i < nDL; i++) {
gSPDisplayList(glistp++, texrect_dl);
gSPMatrix(glistp++, OS_K0_TO_PHYSICAL(&(generate->increment)),
G_MTX_MODELVIEW | G_MTX_MUL | G_MTX_NOPUSH);
}
/*
* Draw bar graph of perf counter values
*/
draw_bar_graph();
/*
* Put an END on the top-level display list, and check that we
* haven't overflowed the buffer.
*/
gDPFullSync(glistp++);
gSPEndDisplayList(glistp++);
if ((int) (glistp - glistp0) > GLIST_SIZE) {
rmonPrintf("*** GLIST OVERFLOW ***\n");
/*
* Could quit here or something
*/
}
/*
* Build graphics task
* Note that all addresses are KSEG0, even if used by the RSP.
* Conversion is done by the task routines.
*/
gentask = &(task[oddframe ? 0 : 1]);
gentask->t.type = M_GFXTASK;
gentask->t.flags = 0x0;
gentask->t.ucode_boot = (u64 *) rspbootTextStart;
gentask->t.ucode_boot_size = ((int) rspbootTextEnd - (int) rspbootTextStart);
gentask->t.ucode = (u64 *) gspFast3DTextStart; /*
* use XBUS
*/
gentask->t.ucode_data = (u64 *) gspFast3DDataStart;
gentask->t.ucode_size = 4096;
gentask->t.ucode_data_size = 2048;
gentask->t.dram_stack = (u64 *) rspdrambuf1;
gentask->t.dram_stack_size = 1024;
gentask->t.output_buff = (u64 *) NULL;
gentask->t.output_buff_size = (u64 *) NULL;
gentask->t.yield_data_ptr = (u64 *) NULL; /*
* Graphics only - no yielding
*/
gentask->t.yield_data_size = 0x0;
gentask->t.data_ptr = (u64 *) glistp0;
gentask->t.data_size = ((int) glistp - (int) glistp0);
/*
* Flush the whole cache. Should just do parts of it.
*/
osWritebackDCacheAll();
/*
* Reset perf counters just before starting rsp task
*/
osDpSetStatus(DPC_CLR_CLOCK_CTR | DPC_CLR_CMD_CTR |
DPC_CLR_PIPE_CTR | DPC_CLR_TMEM_CTR);
/*
* Start task on RSP
*/
osSpTaskStart(gentask);
/*
* Wait for task completion (message from RDP)
*/
osRecvMesg(&RDPDoneMessageQ, NULL, OS_MESG_BLOCK);
osDpGetCounters(PerfCounters);
#if 0
myend = osGetTime();
diff = myend - mystart;
rmonPrintf("RSP+RDP = %8.2f msec (%lli cycles)\n",
((float) (CYCLES_TO_NSEC(diff))) / 1000000, diff);
#endif
osViSwapBuffer(oddframe ? cfb_A : cfb_B);
/*
* Wait for the retrace before rendering next frame
*/
if (MQ_IS_FULL(&RetraceMessageQ))
osRecvMesg(&RetraceMessageQ, NULL, OS_MESG_BLOCK);
osRecvMesg(&RetraceMessageQ, NULL, OS_MESG_BLOCK);
/*
* Switch to our other set of variables
*/
oddframe ^= 1;
}
}