spbench.c
9.43 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
/**************************************************************************
* *
* Copyright (C) 1995, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
*************************************************************************/
/*
* spbench.c
*
* Simple sprite benchmarking application.
*
* Written by Kato San, Nintendo Corporation.
*/
#include "spbench.h"
#define ROOT_DL_LEN 32
#define SPRITE_DL_LEN 20000
typedef struct{s16 x,y,dx,dy,n;} SpriteData;
u64 bootStack[BOOT_STACKSIZE/sizeof(u64)];
static u64 idleStack[IDLE_STACKSIZE/sizeof(u64)];
static u64 mainStack[MAIN_STACKSIZE/sizeof(u64)];
static u64 rmonStack[RMON_STACKSIZE/sizeof(u64)];
static OSThread idleThread, mainThread, rmonThread;
static OSMesgQueue fieldMesgQueue,rspMesgQueue,rdpMesgQueue;
static OSMesg fieldMesg[1],rspMesg[1],rdpMesg[1];
static u64 dram_stack[SP_DRAM_STACK_SIZE64];
static Gfx root_dl[2][ROOT_DL_LEN];
static Gfx sprite_dl[2][SPRITE_DL_LEN];
static SpriteData spriteData[MAX_SPRITES];
/*
* Static display list to clear the screen and initialize various modes.
*/
static Gfx setup_dl[]={
gsDPSetCycleType(G_CYC_FILL),
gsDPPipelineMode(G_PM_NPRIMITIVE),
gsDPSetFillColor(0x10001*(u32)GPACK_RGBA5551(0x3f,0x7f,0x7f,1)),
gsDPFillRectangle(0,0,SCREEN_WD-1, SCREEN_HT-1),
gsDPPipeSync(),
gsDPSetTexturePersp(G_TP_NONE),
gsDPSetTextureLOD(G_TL_TILE),
gsDPSetTextureLUT(G_TT_NONE),
gsDPSetTextureFilter(G_TF_POINT),
gsDPSetTextureConvert(G_TC_FILT),
gsDPSetScissor(G_SC_NON_INTERLACE,0,0,SCREEN_WD,SCREEN_HT),
gsDPSetCycleType(G_CYC_1CYCLE),
gsDPSetAlphaCompare(G_AC_THRESHOLD),
gsDPSetBlendColor(0, 0, 0, 0x01),
gsDPSetCombineMode(G_CC_DECALRGBA,G_CC_DECALRGBA),
gsDPSetRenderMode(G_RM_OPA_SURF,G_RM_OPA_SURF2),
gsSPEndDisplayList()
};
/*
* RSP task structure.
*/
static OSTask tl[]={
{{
M_GFXTASK,OS_TASK_DP_WAIT,
(u64*)rspbootTextStart,
0/* size_of_rspboot */,
(u64*)gspFast3DTextStart,SP_UCODE_SIZE,
(u64*)gspFast3DDataStart,SP_UCODE_DATA_SIZE,
(u64*)&(dram_stack[0]),SP_DRAM_STACK_SIZE8,
(u64*)0x0,(unsigned long long *)0x0,
0/* begin_of_dl */,
0/* size_of_dl */,
(u64*)NULL,0xDA0
},},
{{
M_GFXTASK,OS_TASK_DP_WAIT,
(u64*)rspbootTextStart,
0/* size_of_rspboot */,
(u64*)gspFast3DTextStart,SP_UCODE_SIZE,
(u64*)gspFast3DDataStart,SP_UCODE_DATA_SIZE,
(u64*)&(dram_stack[0]),SP_DRAM_STACK_SIZE8,
(u64*)0x0,(unsigned long long *)0x0,
0/* begin_of_dl */,
0/* size_of_dl */,
(u64*)NULL,0xDA0
},}};
/*
* random
*
* Random number generator
*/
static u16 r;
static int
random(void) {
int i;
for(i=0; i != 7; ++i){
if (r&1)
r = r>>1^0x8812;
else
r >>= 1;
}
return(r);
}
/*
* initSpriteData
*
* Generate initial sprite locations and directions based on sprite size
* parameters. A note about the alignment parameter here:
*
* If a span does not cross a 64 byte boundary (in copy mode),
* the access can be done in a single 64 byte rdram request.
* For this app, 64 bytes = 32 pixels = 128 x coordintaes.
* But since we are only applying this alignment to 16 pixel sprites,
* we can avoid crossing a 64 byte grid boundary as long as the starting
* x position and the delta x is a multiple of 64 x coordinates.
*/
static void
initSpriteData(SpriteData *ps)
{
int i;
/* re-initialize seed */
r = 0x1234;
for(i=0; i != MAX_SPRITES; ++i) {
do {
ps->x=random() & 0x7ff & ~(currentParam->alignment-1);
} while (ps->x>= (SCREEN_WD<<2));
do {
ps->y=random()&0x7ff;
} while (ps->y>= (SCREEN_HT<<2));
do {
if (currentParam->alignment == 1)
ps->dx=random() & 0xf;
else
ps->dx=64;
} while(ps->dx==0);
do {
ps->dy=random() & 0xf;
} while(ps->dy==0);
ps->n = (random() & (currentParam->texturesInTmem-1)) *
(currentParam->textureSizeY << 5);
++ps;
}
}
/*
* prepare_dl
*
* Prepare the display list for the given frame. Simply generate textured
* rectangles based on the sprite size and location; then update the sprite
* location for next time.
*/
static void
prepare_dl(int frame, SpriteData *ps)
{
int i;
Gfx *dlp;
dlp=sprite_dl[frame&1];
gDPSetCycleType(dlp++, currentParam->cycleType);
for(i=0; i < currentParam->numSprites; i++) {
if ((i % currentParam->loadFactor) == 0) {
gDPLoadTextureBlock(dlp++, currentParam->textureData,
G_IM_FMT_RGBA, G_IM_SIZ_16b,
currentParam->textureSizeX,
currentParam->textureSizeY*currentParam->texturesInTmem,
0,
G_TX_WRAP|G_TX_NOMIRROR,G_TX_WRAP|G_TX_NOMIRROR,
G_TX_NOMASK,G_TX_NOMASK,G_TX_NOLOD,G_TX_NOLOD);
}
gSPTextureRectangle(dlp++,
ps->x, ps->y,
(ps->x-4)+currentParam->spriteSizeX*4,
(ps->y-4)+currentParam->spriteSizeY*4,
0,
0, ps->n,
currentParam->dsdx, currentParam->dtdy);
ps->x += ps->dx;
if (ps->x<0) {
ps->x = -ps->x;
ps->dx=-ps->dx;
}
if (ps->x >= (SCREEN_WD - currentParam->spriteSizeX)*4) {
ps->x = 2*(SCREEN_WD - currentParam->spriteSizeX)*4 - ps->x;
ps->dx =- ps->dx;
}
ps->y += ps->dy;
if (ps->y < 0) {
ps->y =- ps->y;
ps->dy=-ps->dy;
}
if (ps->y >= (SCREEN_HT - currentParam->spriteSizeY) * 4) {
ps->y = 2*(SCREEN_HT-currentParam->spriteSizeY)*4 - ps->y;
ps->dy =- ps->dy;
}
++ps;
}
gSPEndDisplayList(dlp++);
if ((dlp-sprite_dl[frame&1]) > SPRITE_DL_LEN) {
osSyncPrintf("spbench: sprite display list too long (%d vs %s)\n",
dlp-sprite_dl[frame&1], SPRITE_DL_LEN);
osExit();
for (;;);
}
osWritebackDCache(sprite_dl[frame&1],
(dlp-sprite_dl[frame&1]) * sizeof(Gfx));
dlp=root_dl[frame&1];
gDPSetColorImage(dlp++,G_IM_FMT_RGBA,G_IM_SIZ_16b,SCREEN_WD,
framebuffer[frame&1]);
gSPDisplayList(dlp++,setup_dl);
gSPDisplayList(dlp++,sprite_dl[frame&1]);
graphMeter(dlp++);
gDPFullSync(dlp++);
gSPEndDisplayList(dlp++);
tl[frame&1].t.data_ptr=(u64*)root_dl[frame&1];
tl[frame&1].t.data_size=((int)(dlp-root_dl[frame&1])*sizeof(Gfx));
if ((dlp-root_dl[frame&1]) > ROOT_DL_LEN) {
osSyncPrintf("spbench: root display list too long (%d vs %s)\n",
dlp-root_dl[frame&1], ROOT_DL_LEN);
osExit();
for (;;);
}
osWritebackDCache(root_dl[frame&1],
(dlp-root_dl[frame&1]) * sizeof(Gfx));
}
/*
* idle
*
* This thread fires up the application threads and then lowers its priority
* to become the idle thread.
*/
static void
idle(void *arg)
{
osCreateThread(&rmonThread, (OSId)0, rmonMain, (void*)0,
rmonStack+RMON_STACKSIZE/sizeof(u64), OS_PRIORITY_RMON);
osStartThread(&rmonThread);
osCreateThread(&mainThread,(OSId)3,main,(void*)0,
mainStack+MAIN_STACKSIZE/sizeof(u64),(OSPri)10);
osStartThread(&mainThread);
osSetThreadPri((OSThread*)0,(OSPri)0);
for (;;);
}
/*
* boot
*
* Initial boot code; initialize the OS and fire up the idle thread.
*/
void
boot(void)
{
osInitialize();
osCreateThread(&idleThread,1,idle,(void*)0,
idleStack+IDLE_STACKSIZE/sizeof(u64),(OSPri)10);
osStartThread(&idleThread);
}
/*
* main
*
* Loop, rendering the previous frame's display list while generating the
* current one. Check the controller at every frame to adjust sprite
* parameters.
*/
void
main(void *arg)
{
u32 frame;
int controllerNum;
osCreateViManager(OS_PRIORITY_VIMGR);
osViSetMode(&osViModeTable[OS_VI_NTSC_LAN1]);
osViSetSpecialFeatures(OS_VI_GAMMA_OFF);
osCreateMesgQueue(&fieldMesgQueue,fieldMesg,1);
osViSetEvent(&fieldMesgQueue,(OSMesg)0,1);
osCreateMesgQueue(&rspMesgQueue,rspMesg,1);
osSetEventMesg(OS_EVENT_SP,&rspMesgQueue,(OSMesg)0);
osCreateMesgQueue(&rdpMesgQueue,rdpMesg,1);
osSetEventMesg(OS_EVENT_DP,&rdpMesgQueue,(OSMesg)0);
tl[0].t.ucode_boot_size=((int)rspbootTextEnd-(int)rspbootTextStart);
tl[1].t.ucode_boot_size=((int)rspbootTextEnd-(int)rspbootTextStart);
if ((controllerNum = initController()) == -1) {
osSyncPrintf("spbench: no controller connected\n");
osExit();
for (;;);
}
osSyncPrintf("spbench: using controller %d\n\n", controllerNum);
printCurrentParam();
initialMeter();
initSpriteData(spriteData);
frame=0;
prepare_dl(frame, spriteData);
for (;;) {
if (MQ_IS_FULL(&fieldMesgQueue))
osRecvMesg(&fieldMesgQueue,(OSMesg*)0,OS_MESG_BLOCK);
osRecvMesg(&fieldMesgQueue,(OSMesg*)0,OS_MESG_BLOCK);
timeMeter(0); /* reset meter */
osSpTaskLoad(&tl[frame&1]);
osSpTaskStartGo(&tl[frame&1]);
frame+=1;
if (readController(controllerNum)) {
timeMeter(1); /* mark after controller read */
initSpriteData(spriteData);
} else {
timeMeter(1); /* mark after controller read */
}
prepare_dl(frame, spriteData);
timeMeter(1); /* mark after dl preparation */
osRecvMesg(&rspMesgQueue,(OSMesg*)0,OS_MESG_BLOCK);
osRecvMesg(&rdpMesgQueue,(OSMesg*)0,OS_MESG_BLOCK);
timeMeter(1); /* mark after dl render */
osViSwapBuffer(framebuffer[frame&1^1]);
timeMeter(1); /* final mark at retrace */
}
}