viewer.c
19.7 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include <ultra64.h>
#include <sched.h>
#include <PR/bcp.h>
#include <PR/bbnand.h>
#include <PR/os_bb.h>
#include "bbtypes.h"
#include "bbskapi.h"
#include "viewer.h"
#include "status.h"
#include "uilogic.h"
#include "audrenderer.h"
#include "pagetop.h"
#include "pagegame.h"
#include "pagegpak.h"
#include "pagecpak.h"
#include "vidreset.h"
#include "gzip.h"
#include "font.h"
#ifndef _PARTNER
extern void __osBbVideoPllInit(s32 tvType);
#endif
/* On card removal/insertion, force the osBbCardProbe() function
* to pass at least this many times, each time separated by at
* least a field-time.
* To use only a single osBbCardProbe() this define may be set
* to 0 (BUT this CAN result in a hang that remains
* until the card is removed and inserted a second time).
*/
#define CARD_PROBE_SUCCESS_COUNT 5
/* for debounce of button */
#define DEBOUNCE_ON_FRAME_WAIT 15
#define DEBOUNCE_OFF_FRAME_WAIT 10
#define BUT_RELEASED (!((IO_READ(MI_EINTR_REG)&MI_EINTR_BUTTON_PRESSED)))
#define BUT_STATE_CHANGED \
(IO_READ(MI_EINTR_REG)&MI_INTR_BUT)
#define BUT_CLEAR_STATE_CHANGED \
IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_CLR_BUT)
#ifdef SHOW_BBID
int gFlashErr = 0;
u32 gFlashErrAddr = 0;
#endif
BbId gBBID;
u32 gCardID;
u32 gEntryCode;
int gStatus;
int gTicketStatus;
u32 gCid = 0;
u16 gTid = 0;
u16 gTidx = 0;
char gWarningFile[9] = "warn.tmp";
int gWarn = 0;
OSSched gSc;
OSMesgQueue *gpSchedCmdQ;
u64 gpSchedStack[OS_SC_STACKSIZE/8];
OSScClient gGfxClient;
OSMesgQueue gGfxFrameMsgQ;
OSMesg gpGfxFrameMsgBuf[MAX_MESGS];
Gfx *gpGfxList;
GFXInfo gpGfxInfo[2];
u8 gpDate[7];
u32 gPageNo;
int gCardInit;
u8 gHotSpot;
OSMesgQueue gControllerMsgQ;
OSMesg gControllerMsgBuf;
static OSMesg PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue PiMessageQ;
u64 bootStack[STACKSIZEBYTES/sizeof(u64)];
static u64 gameThreadStack[STACKSIZEBYTES/sizeof(u64)];
static u64 initThreadStack[STACKSIZEBYTES/sizeof(u64)];
static OSThread gameThread;
static OSThread initThread;
static void gameproc(void *);
static void initproc(char *);
static void initGame(void);
static void checkCard(void);
static int checkPowerWarning(void);
extern s32 __osShutdown;
#ifdef PAK_COPY
extern SaGpakCopy gGpak;
extern SaCpakCopy gCpak;
#endif
void resetViewer(void);
#ifndef _FINALROM
OSTime lastTime;
#endif
void boot(u32 reg)
{
gEntryCode = reg;
#ifndef _PARTNER
/*
* Turn off the button interrupt
*/
IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_CLR_BUT);
#ifndef SA2 /* Only need to do this if we are the main sysapp (not SA2) */
/*
* Init Pll. No need to do all this if it already happened
*/
if ((gEntryCode & (MI_SEC_MODE_BUT_TRAP | MI_SEC_MODE_TIMER | MI_SEC_MODE_APP)) == 0) {
__osBbVideoPllInit(OS_TV_NTSC);
vidReset(&cfb_16_a[0]);
} else {
/*
* Make the video output black
*/
IO_WRITE(VI_H_START_REG, 0x0);
}
#endif
#endif
osInitialize();
osCreateThread(&initThread, 1, (void(*)(void *))initproc, (void *)0,
(void *)(initThreadStack+(STACKSIZEBYTES/sizeof(u64))),
(OSPri)INIT_PRIORITY);
osStartThread(&initThread);
}
static void initproc(char *argv)
{
#ifndef _PARTNER
#ifndef _FINALROM
int i;
osBbUsbInit();
for (i=0; i<10000000; i++);
#endif
#endif
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
osCreateThread(&gameThread, 6, gameproc, argv, gameThreadStack +
(STACKSIZEBYTES/sizeof(u64)), (OSPri)GAME_PRIORITY);
osStartThread(&gameThread);
osSetThreadPri(0, 0);
for(;;);
}
/**********************************************************************
*
* A continual loop, primarily used for servicing the starts of graphic
* tasks and controller reads. Audio is serviced by the audio thread.
* You receive several message on the same queue, they are:
* OS_SC_RETRACE_MSG: this comes from the scheduler every retrace.
* If you don't already have 2 graphics tasks either pending or
* processing, start creating another.
* OS_SC_DONE_MSG: sent by the scheduler when a graphics task has
* completed.
* OS_SC_PRE_NMI_MSG: sent to indicate the reset button has been
* pressed, and that the game will reboot momentarily. Might
* want to fade or say goodbye.
*
**********************************************************************/
static void gameproc(void *argv)
{
u32 drawbuffer = 0;
u32 pendingGFX = 0;
int powerOff = 0;
GFXMsg *msg = NULL;
/*
* Used for debounce
*/
int waitFrame = DEBOUNCE_ON_FRAME_WAIT;
int ready = 0;
u32 mode;
int rcount = 0;
#ifndef _FINALROM
u32 doneTask = 0;
#endif
initGame();
/*
* Startup sound - wait till it starts before going on
*/
if (gEntryCode & MI_SEC_MODE_TIMER) {
ready = 1;
IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_BUT);
} else {
if (gEntryCode & MI_SEC_MODE_BUT_TRAP ||
gEntryCode & MI_SEC_MODE_APP ) {
playSound(SOUND_MENU_DOWN, AUDIO_NOWAIT);
} else {
playSound(SOUND_STARTUP, AUDIO_NOWAIT);
}
}
while (1)
{
checkCard();
/*
* This is for startup and debounce. Only start listening to
* buttons once the startup sound is done and DEBOUNCE_ON_FRAME_WAIT
* frames have passed by
*/
if (ready) {
osContStartReadData(&gControllerMsgQ);
updateController();
} else {
#ifndef PARTNER
if (BUT_STATE_CHANGED) {
BUT_CLEAR_STATE_CHANGED;
waitFrame = DEBOUNCE_ON_FRAME_WAIT;
}
if (BUT_RELEASED) {
waitFrame--;
}
if (waitFrame < 0) {
IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_BUT);
ready = 1;
}
#else
ready = 1;
#endif
}
mode = osViGetCurrentMode();
if ( gStatus== VW_GAME_PRE_LAUNCH1 ||
gStatus== VW_GAME_PRE_LAUNCH2 ||
gStatus == VW_GAME_PRE_LAUNCH3 ||
gStatus == VW_GAME_PRE_LAUNCH ||
gStatus == VW_GAME_PERSONALIZE ||
gStatus == VW_GAME_LAUNCH1 ||
gStatus == VW_GAME_LAUNCH2 ||
gStatus == VW_GAME_LAUNCH ) {
if (mode != OS_VI_NTSC_LAN1) {
osViSetMode(&osViModeNtscLan1);
osViBlack(TRUE);
rcount = 3;
}
} else {
if (mode != OS_VI_NTSC_HAF1) {
osViSetMode(&osViModeNtscHaf1);
osViBlack(TRUE);
rcount = 3;
}
}
(void) osRecvMesg(&gGfxFrameMsgQ, (OSMesg *)&msg, OS_MESG_BLOCK);
switch (msg->gen.type)
{
case (OS_SC_RETRACE_MSG):
if (powerOff) {
PRINTF("waitFrame = %d\n",waitFrame);
if (BUT_STATE_CHANGED) {
BUT_CLEAR_STATE_CHANGED;
waitFrame = DEBOUNCE_OFF_FRAME_WAIT;
}
if (BUT_RELEASED) {
waitFrame--;
if (waitFrame-- < 0) {
osBbFDelete( gWarningFile );
osBbPowerOff();
for (;;) ;
}
}
} else {
/**** Create a new gfx task unless we already have 2 ****/
if (pendingGFX < 2)
{
createGfxTask(&gpGfxInfo[drawbuffer]);
pendingGFX++;
drawbuffer ^= 1; /* switch the drawbuffer */
}
}
break;
case (OS_SC_DONE_MSG):
#ifndef _FINALROM
lastTime = gpGfxInfo[doneTask].task.totalTime;
doneTask ^= 1;
#endif
pendingGFX--; /* decrement number of pending tasks */
if (rcount && --rcount == 0)
osViBlack(FALSE);
break;
case (OS_SC_PRE_NMI_MSG): /* stop creation of graphics tasks */
pendingGFX += 2;
#ifndef _PARTNER
/*
* TODO: Need to clean up before turning power off!
*/
PRINTF("Power Off\n");
waitFrame = DEBOUNCE_OFF_FRAME_WAIT;
powerOff = 1;
break;
#endif
}
}
}
/**********************************************************************
*
* initGame sets up the message queues used, and starts the scheduler.
* After that call routines to init the graphics, init the controllers,
* and init the audio.
*
*********************************************************************/
static void initGame(void)
{
/**** set up a needed message q's ****/
osCreateMesgQueue(&gControllerMsgQ, &gControllerMsgBuf, 1);
osCreateMesgQueue(&gGfxFrameMsgQ, gpGfxFrameMsgBuf, MAX_MESGS);
/**** Initialize the RCP task scheduler ****/
#ifdef HI_RES
osCreateScheduler(&gSc, (void *)(gpSchedStack + OS_SC_STACKSIZE/8),
SCHEDULER_PRIORITY, OS_VI_NTSC_HAF1, NUM_FIELDS);
#else
osCreateScheduler(&gSc, (void *)(gpSchedStack + OS_SC_STACKSIZE/8),
SCHEDULER_PRIORITY, OS_VI_NTSC_LAN1, NUM_FIELDS);
#endif
/**** Add ourselves to the scheduler to receive retrace messages ****/
osScAddClient(&gSc, &gGfxClient, &gGfxFrameMsgQ);
gpSchedCmdQ = osScGetCmdQ(&gSc);
initGFX();
initCntrl();
initAudio( &gSc );
osBbRtcInit();
resetViewer();
if ( (gEntryCode & MI_SEC_MODE_APP) ||
(gEntryCode & MI_SEC_MODE_BUT_TRAP) ||
(gEntryCode & MI_SEC_MODE_TIMER) )
gWarn = 0;
else {
gWarn = checkPowerWarning();
}
}
void resetViewer(void)
{
int ret = VW_OK;
gCardInit = 0;
gTicketStatus = VW_OK;
#ifdef PAK_COPY
gPageGpakInit = 0;
#endif
if ((gStatus=uiBbidCheck()) == VW_OK) {
if (gEntryCode & MI_SEC_MODE_TIMER) { // return from LP or PC expire
char stateName[16]; /* 8.3 file <cid>.sta */
BbTicketId tid;
if(0==osBbSaveState(stateName, &tid)){
PRINTF("Calling sberrCorrect()\n");
sberrCorrect(stateName);
}
gCardInit = 0;
initPageTop(PAGE_GAME);
gLogoCnt = MAX_LOGO_COUNT;
gStatus = uiCheckExpiration(tid);
ret = uiUpdateTimer(tid, 0);
if (ret==VW_ERR_FS)
gStatus = ret;
initPageGame(0, 0);
playSound(SOUND_LPLAY_EXIT, AUDIO_NOWAIT);
return;
} else {
initPageTop(PAGE_GAME);
if (gEntryCode & MI_SEC_MODE_APP) { // "B" return from club
gLogoCnt = MAX_LOGO_COUNT;
} else if (gEntryCode & MI_SEC_MODE_BUT_TRAP) { // return from games
char stateName[16]; /* 8.3 file <cid>.sta */
BbTicketId tid;
if(0==osBbSaveState(stateName, &tid)){
PRINTF("Calling sberrCorrect()\n");
sberrCorrect(stateName);
}
gLogoCnt = MAX_LOGO_COUNT;
uiUpdateTimer(tid, 1);
initPageGame(0, 0);
} else { // power on
uiUpdateTimer(0, 1); // in case powered off w/o power button
}
}
//gGpakIdxMax = uiGetGpaks(gGpakIds);
#ifdef SHOW_BBID
if (IO_READ(PI_ERROR_REG) & PI_ERROR_CORRECTABLE_ECC) {
gFlashErr = 1;
gFlashErrAddr = IO_READ(PI_ERROR_REG)&PI_ERROR_ADDR_MASK;
}
if (IO_READ(PI_ERROR_REG) & PI_ERROR_UNCORRECTABLE_ECC) {
gFlashErr = 2;
gFlashErrAddr = IO_READ(PI_ERROR_REG)&PI_ERROR_ADDR_MASK;
}
#endif
} else
initPageTop(PAGE_GAME);
}
static int checkPowerWarning()
{
s32 ifd;
if ( (ifd=osBbFOpen(gWarningFile, "r"))==BBFS_ERR_ENTRY ) {
osBbFCreate( gWarningFile, 1, BB_FL_BLOCK_SIZE);
return 0;
} else {
osBbFClose( ifd );
return 1;
}
}
static void checkCard(void)
{
static int cardProbePassCount = 0;
static int cardProbePassLimit = CARD_PROBE_SUCCESS_COUNT;
s32 ret = 0;
if(cardProbePassCount==0)
ret = osBbCardUnhappy();
else{
ret = BBCARD_ERR_CHANGED;
}
switch (gStatus) {
#ifdef PAK_COPY
case VW_GP_CPY_SRC_CARD: // waiting for src card
case VW_ERR_GP_CPY_SRC_NO_GPAK: // src card no gpak
case VW_ERR_GP_CPY_SRC_NO_TICKET: // src card no ticket.sys
if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
gStatus = uiGpakSrcCardOkay();
}
}
}
break;
case VW_GP_CPY_DST_CARD: // waiting for dst. card
case VW_ERR_GP_CPY_WRONG_DST_CARD: // incorrect dst. card
case VW_ERR_GP_CPY_DST_NO_SPACE: // dst. card doesn't have space left
case VW_ERR_GP_CPY_DST_NO_TICKET: // dst. card doesn't have ticket
if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
gStatus = uiGpakDstCardOkay(&gGpak);
}
}
}
break;
case VW_GP_CPY_CANCEL: // copying cancelled
if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
resetViewer();
if (gStatus!=VW_ERR_ID_MISMATCH && gStatus!=VW_ERR_FS)
{
initPageGpak(0, 0);
}
}
}
}
break;
case VW_CP_CPY_SRC_CARD: // waiting for src card
case VW_ERR_CP_CPY_SRC_NO_CPAK: // src card no cpak
case VW_ERR_CP_CPY_SRC_NO_TICKET: // src card no ticket.sys
if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
gStatus = uiCpakSrcCardOkay();
}
}
}
break;
case VW_CP_CPY_DST_CARD: // waiting for dst. card
case VW_ERR_CP_CPY_WRONG_DST_CARD: // incorrect dst. card
case VW_ERR_CP_CPY_DST_NO_TICKET: // dst. card doesn't have ticket
if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
gStatus = uiCpakDstCardOkay(&gCpak);
}
}
}
break;
case VW_CP_CPY_CANCEL: // copying cancelled
if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
resetViewer();
if (gStatus!=VW_ERR_ID_MISMATCH && gStatus!=VW_ERR_FS)
{
initPageCpak(0);
}
}
}
}
break;
#endif
default:
if ( ret == BBCARD_ERR_NO_CARD ) {
gCardInit = 0;
initPageTop(PAGE_GAME);
gStatus = VW_ERR_NO_CARD;
} else if ( ret == BBCARD_ERR_CHANGED) {
if (osBbCardProbe(0) == 0){
if(cardProbePassCount < cardProbePassLimit){
if(cardProbePassCount==0){
cardProbePassLimit = gGfxFrameMsgQ.validCount +
CARD_PROBE_SUCCESS_COUNT;
PRINTF("Setting cardProbe limit = %d\n",
cardProbePassLimit);
}
PRINTF("Incrementing cardProbe count\n");
cardProbePassCount++;
}
else{
PRINTF("Zeroing cardProbe count\n");
cardProbePassCount=0;
resetViewer();
}
}
}
break;
}
}