nnsched.c
10.9 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
/*============================================================================
NINTENDO64 TECHNICAL SUPPORT CENTER
NINTENDO64 SAMPLE PROGRAM 1
Copyright (C) 1997, NINTENDO Co., Ltd.
1997/01/23
Added OS_TASK_LOADABLE flag in order to accommodate the addition of gSPLoadUcode to F3DEX Ver1.20.
1997/02/03
Fixed bug that arose when performance check found value larger than _SC_GTASK_NUM and NN_SC_AUTASK_NUM.
1997/12/10
Corrected display of warning message.
============================================================================*/
#include "nnsched.h"
/* secure stack for thread */
u64 nnScStack[NN_SC_STACKSIZE/8];
u64 nnScAudioStack[NN_SC_STACKSIZE/8];
u64 nnScGraphicsStack[NN_SC_STACKSIZE/8];
u32 framecont = 0;
#ifdef NN_SC_PERF
static u32 nnsc_perf_index = 0; /* location of internally used buffer */
static u32 nnsc_perf_flag = 0;
static NNScPerf nnsc_perf[NN_SC_PERF_NUM]; /* measurement data */
static NNScPerf* nnsc_perf_inptr; /* internally used pointer */
NNScPerf* nnsc_perf_ptr; /* pointer from external reference */
#endif /* NN_SC_PERF */
/************************************************************
nnScCreateScheduler() - Create scheduler
************************************************************/
void nnScCreateScheduler(NNSched *sc, u8 videoMode, u8 numFields)
{
/* initialize variables */
sc->curGraphicsTask = 0;
sc->curAudioTask = 0;
sc->graphicsTaskSuspended = 0;
sc->clientList = 0;
sc->firstTime = 1;
sc->retraceMsg = NN_SC_RETRACE_MSG;
sc->prenmiMsg = NN_SC_PRE_NMI_MSG;
/* create message queue */
osCreateMesgQueue(&sc->retraceMQ, sc->retraceMsgBuf, NN_SC_MAX_MESGS);
osCreateMesgQueue(&sc->rspMQ, sc->rspMsgBuf, NN_SC_MAX_MESGS);
osCreateMesgQueue(&sc->rdpMQ, sc->rdpMsgBuf, NN_SC_MAX_MESGS);
osCreateMesgQueue(&sc->graphicsRequestMQ, sc->graphicsRequestBuf,
NN_SC_MAX_MESGS);
osCreateMesgQueue(&sc->audioRequestMQ, sc->audioRequestBuf,
NN_SC_MAX_MESGS);
osCreateMesgQueue(&sc->waitMQ, sc->waitMsgBuf, NN_SC_MAX_MESGS);
/* video mode settings */
osCreateViManager(OS_PRIORITY_VIMGR);
osViSetMode(&osViModeTable[videoMode]);
osViBlack(TRUE);
/* register event handler */
osViSetEvent(&sc->retraceMQ, (OSMesg)VIDEO_MSG, numFields);
osSetEventMesg(OS_EVENT_SP, &sc->rspMQ, (OSMesg)RSP_DONE_MSG);
osSetEventMesg(OS_EVENT_DP, &sc->rdpMQ, (OSMesg)RDP_DONE_MSG);
osSetEventMesg(OS_EVENT_PRENMI, &sc->retraceMQ, (OSMesg)PRE_NMI_MSG);
/* start scheduler thread */
osCreateThread(&sc->schedulerThread, 19, (void(*)(void*))nnScEventHandler,
(void *)sc, nnScStack+NN_SC_STACKSIZE/sizeof(u64),
NN_SC_PRI);
osStartThread(&sc->schedulerThread);
osCreateThread(&sc->audioThread, 18, (void(*)(void *))nnScExecuteAudio,
(void *)sc, nnScAudioStack+NN_SC_STACKSIZE/sizeof(u64),
NN_SC_AUDIO_PRI);
osStartThread(&sc->audioThread);
osCreateThread(&sc->graphicsThread, 17,(void(*)(void*))nnScExecuteGraphics,
(void *)sc, nnScGraphicsStack+NN_SC_STACKSIZE/sizeof(u64),
NN_SC_GRAPHICS_PRI);
osStartThread(&sc->graphicsThread);
}
/************************************************************
nnScGetAudioMQ() --- Get audio message queue
************************************************************/
OSMesgQueue *nnScGetAudioMQ(NNSched *sc)
{
return( &sc->audioRequestMQ );
}
/************************************************************
nnScGetGfxMQ() --- Get audio message queue
************************************************************/
OSMesgQueue *nnScGetGfxMQ(NNSched *sc)
{
return( &sc->graphicsRequestMQ );
}
/************************************************************
nnScEventHandler() - Process system event
************************************************************/
void nnScEventHandler(NNSched *sc)
{
OSMesg msg = (OSMesg)0;
while(1) {
/* Cx"g̎"3/4 */
osRecvMesg(&sc->retraceMQ, &msg, OS_MESG_BLOCK);
framecont++;
switch ( (int)msg ) {
case VIDEO_MSG: /* process retrace signal */
nnScEventBroadcast( sc, &sc->retraceMsg );
#ifdef NN_SC_PERF
if( nnsc_perf_flag == 0){
/* do not initialize until all graphic tasks are completed */
nnsc_perf_flag++;
/* enable externally derived reference to measured buffer pointer */
nnsc_perf_ptr = &nnsc_perf[nnsc_perf_index];
nnsc_perf_index++; /* swap buffer */
nnsc_perf_index %= NN_SC_PERF_NUM;
nnsc_perf_inptr = &nnsc_perf[nnsc_perf_index];
nnsc_perf_inptr->autask_cnt = 0;
nnsc_perf_inptr->gtask_cnt = 0;
/* get retrace time */
nnsc_perf_inptr->retrace_time = OS_CYCLES_TO_USEC(osGetTime());
}
#endif /* NN_SC_PERF */
break;
case PRE_NMI_MSG: /* process NMI signal */
nnScEventBroadcast( sc, &sc->prenmiMsg );
break;
}
}
}
/************************************************************
nnScAddClient() --- Register client
************************************************************/
void nnScAddClient(NNSched *sc, NNScClient *c, OSMesgQueue *msgQ)
{
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
c->msgQ = msgQ;
c->next = sc->clientList;
sc->clientList = c;
osSetIntMask(mask);
}
/************************************************************
nnScRemoveClient() --- Remove client
************************************************************/
void nnScRemoveClient(NNSched *sc, NNScClient *c)
{
NNScClient *client = sc->clientList;
NNScClient *prev = 0;
OSIntMask mask;
mask = osSetIntMask(OS_IM_NONE);
while (client != 0) {
if (client == c) {
if(prev)
prev->next = c->next;
else
sc->clientList = c->next;
break;
}
prev = client;
client = client->next;
}
osSetIntMask(mask);
}
/************************************************************
nnScEventBroadcast() -- Transmit message to client
************************************************************/
void nnScEventBroadcast(NNSched *sc, NNScMsg *msg)
{
NNScClient *client;
/* inform necessary clients of retrace message */
for (client = sc->clientList; client != 0; client = client->next) {
osSendMesg(client->msgQ, (OSMesg *)msg, OS_MESG_NOBLOCK);
}
}
/************************************************************
nnScExecuteAudio() -- Execute audio task
************************************************************/
void nnScExecuteAudio(NNSched *sc)
{
OSMesg msg = (OSMesg)0;
NNScTask *task = (NNScTask *)0;
NNScTask *gfxTask = (NNScTask *)0;
u32 yieldFlag = 0;
#ifdef NN_SC_PERF
u32 task_cnt;
#endif /* NN_SC_PERF */
while(1) {
/* wait for audio execution request */
osRecvMesg(&sc->audioRequestMQ, (OSMesg *)&task, OS_MESG_BLOCK);
osWritebackDCacheAll(); /* flash cache */
/* Inspect current RSP stack */
yieldFlag = 0;
gfxTask = sc->curGraphicsTask;
if( gfxTask ) {
/* wait for graphic task to end (yield) */
osSpTaskYield(); /* task yield */
osRecvMesg(&sc->rspMQ, &msg, OS_MESG_BLOCK);
/* verify that task has actually yielded */
if (osSpTaskYielded(&gfxTask->list)){
yieldFlag =1;
} else {
yieldFlag =2;
}
}
#ifdef NN_SC_PERF
if(nnsc_perf_inptr->autask_cnt < NN_SC_AUTASK_NUM){
task_cnt = nnsc_perf_inptr->autask_cnt;
nnsc_perf_inptr->autask_stime[task_cnt] =
OS_CYCLES_TO_USEC(osGetTime()) - nnsc_perf_inptr->retrace_time;
}
#endif /* NN_SC_PERF */
sc->curAudioTask = task;
osSpTaskStart(&task->list); /* execute task */
/* wait for end of RSP task */
osRecvMesg(&sc->rspMQ, &msg, OS_MESG_BLOCK);
sc->curAudioTask = (NNScTask *)0;
#ifdef NN_SC_PERF
if(nnsc_perf_inptr->autask_cnt < NN_SC_AUTASK_NUM){
nnsc_perf_inptr->autask_etime[task_cnt] =
OS_CYCLES_TO_USEC(osGetTime()) - nnsc_perf_inptr->retrace_time;
nnsc_perf_inptr->autask_cnt++;
}
#endif /* NN_SC_PERF */
if( sc->graphicsTaskSuspended )
osSendMesg( &sc->waitMQ, &msg, OS_MESG_BLOCK );
/* restart graphic task that yielded */
if( yieldFlag == 1 ) {
osSpTaskStart(&gfxTask->list);
} else if ( yieldFlag == 2 ) {
osSendMesg(&sc->rspMQ, &msg, OS_MESG_BLOCK);
}
/* inform thread started by audio task that task has ended */
osSendMesg(task->msgQ, task->msg, OS_MESG_BLOCK);
}
}
/************************************************************
* nnScExecuteGraphics() -- Execute graphic task
************************************************************/
void nnScExecuteGraphics(NNSched *sc)
{
OSMesg msg = (OSMesg)0;
NNScTask *task;
#ifdef NN_SC_PERF
u32 task_cnt;
#endif /* NN_SC_PERF */
while(1) {
/* wait for graphic task execution request */
osRecvMesg(&sc->graphicsRequestMQ, (OSMesg *)&task, OS_MESG_BLOCK);
/* wait for frame buffer to become available for use */
nnScWaitTaskReady(sc, task);
if( sc->curAudioTask ) {
sc->graphicsTaskSuspended = task;
osRecvMesg( &sc->waitMQ, &msg, OS_MESG_BLOCK );
sc->graphicsTaskSuspended = (NNScTask *)0;
}
#ifdef NN_SC_PERF
if(nnsc_perf_inptr->gtask_cnt < NN_SC_GTASK_NUM){
task_cnt = nnsc_perf_inptr->gtask_cnt;
nnsc_perf_inptr->gtask_stime[task_cnt] =
OS_CYCLES_TO_USEC(osGetTime()) - nnsc_perf_inptr->retrace_time;
}
#endif /* NN_SC_PERF */
sc->curGraphicsTask = task;
osSpTaskStart(&task->list); /* execute task */
/* wait for end of RSP task */
osRecvMesg(&sc->rspMQ, &msg, OS_MESG_BLOCK);
sc->curGraphicsTask = (NNScTask *)0;
#ifdef NN_SC_PERF
if(nnsc_perf_inptr->gtask_cnt < NN_SC_GTASK_NUM){
nnsc_perf_inptr->rsp_etime[task_cnt] =
OS_CYCLES_TO_USEC(osGetTime()) - nnsc_perf_inptr->retrace_time;
}
#endif /* NN_SC_PERF */
/* wait for end of RDP task */
osRecvMesg(&sc->rdpMQ, &msg, OS_MESG_BLOCK);
#ifdef NN_SC_PERF
if(nnsc_perf_inptr->gtask_cnt < NN_SC_GTASK_NUM){
nnsc_perf_inptr->rdp_etime[task_cnt] =
OS_CYCLES_TO_USEC(osGetTime()) - nnsc_perf_inptr->retrace_time;
nnsc_perf_inptr->gtask_cnt++;
}
#endif /* NN_SC_PERF */
/* invalidate video blackout first time only */
if (sc->firstTime) {
osViBlack(FALSE);
sc->firstTime = 0;
}
/* specify next frame buffer to display */
if ( task->flags & NN_SC_SWAPBUFFER ){
osViSwapBuffer(task->framebuffer);
#ifdef NN_SC_PERF
nnsc_perf_flag = 0;
#endif /* NN_SC_PERF */
}
/* inform thread started by graphic task that task has ended */
osSendMesg(task->msgQ, task->msg, OS_MESG_BLOCK);
}
}
/************************************************************
nnScWaitTaskReady() - Wait for frame buffer to become available for use
************************************************************/
void nnScWaitTaskReady(NNSched *sc, NNScTask *task)
{
OSMesg msg = (OSMesg)0;
NNScClient client;
void *fb = task->framebuffer;
/* wait for next retrace if frame buffer is not empty */
while( osViGetCurrentFramebuffer() == fb || osViGetNextFramebuffer() ==
fb ) {
nnScAddClient( sc, &client, &sc->waitMQ );
osRecvMesg( &sc->waitMQ, &msg, OS_MESG_BLOCK );
nnScRemoveClient( sc, &client );
}
}