launcher.c
16.3 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
#include "ultra64.h"
#ifdef IS_BB
#include "os_bb.h"
#include "bcp.h"
#include "bbvirage.h"
#endif
#include "launcher.h"
#include "libfb.h"
extern u32 __osBbHackFlags;
extern u32 __osBbNoSk;
u32 hackflags;
u32 tvtype;
u32 tvtypeset = 0;
s32 binding[] = {0, 1, 2, 3};
int currentCP = 0;
static u8 pattern;
static u16 lastbutton[MAXCONTROLLERS];
#define DMA_QUEUE_SIZE 200
#define PRINTF osSyncPrintf
//#define IDE_PRINT
/*
* 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];
static OSMesgQueue PiMessageQ;
static OSMesg SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue SiMessageQ, contMessageQ;
OSPiHandle *handler;
/*
* 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 */
{
osCreateViManager(OS_PRIORITY_VIMGR);
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
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 OSMesgQueue retraceMessageQ;
static OSMesg dummyMessage, retraceMessageBuf;
#ifdef IS_BB
static u16 blockList[8*1024];
static OSBbFs fs;
static void draw_filemap(BbFat16* fat, u16 nblocks, u16 bias);
#else
typedef struct {
char name[256];
u8 type;
int size;
} OSBbDirEnt;
#endif
static OSBbDirEnt dir[400];
static int num_files = 0;
static int current = 0;
static int bias;
#ifdef IS_BB
static char *cause = (char *)(0x80500000-4096);
#else
static char *cause = (char *)(0x80300000-4096);
#endif
static int top = 0;
static void
drawFile(int i) {
int x, y;
u16 color;
if ((i < top || i > (top+7)) && (i < (num_files/2+top) || i > (num_files/2+7+top))) {
return;
}
color = i == current ? GPACK_RGBA5551(255,0,255,1): fbWhite;
x = (i >= (num_files+1)/2) ? 20 : 5;
y = 4-top+(i % ((num_files+1)/2));
if (dir[i].type == 0xff) {
fbPrintStr(color, x-1, y, "*");
}
fbPrintStr(color, x, y, dir[i].name);
}
static void redraw(int mode) {
int i;
char *title = "Card Me";
char buf[32];
char *types[] = {"PAL", "NTSC", "MPAL"};
#if IS_BB
BbVirage2* v2 = (BbVirage2*)VIRAGE2_RAM_START;
#endif
fbClear();
#if IS_BB
if (mode) {
extern BbFat16* __osBbFat;
draw_filemap(__osBbFat, osBbCardBlocks(0), bias);
} else
#endif
{
fbPrintStr(fbMarkColor, 2+16-strlen(title)/2, 2, title);
#if IS_BB
fbPrintf(fbWhite, 28, 2, "%08x", IO_READ(&v2->bbId));
fbPrintf(fbWhite, 3, 2, "%04x", IO_READ(PI_GPIO_REG)>>16);
#endif
if (num_files == 0) {
current = -1;
fbPrintStr(fbRed, 6, 4, "No files found!");
}
for (i = 0; i < num_files; ++i) {
drawFile(i);
}
if (top) {
fbPrintStr(fbYellow, 17, 4, "^^^");
}
if (top < num_files/2-8) {
fbPrintStr(fbYellow, 17, 12, "vvv");
}
sprintf(buf, "CP0:%ld CP1:%ld CP2:%ld CP3:%ld", binding[0], binding[1], binding[2], binding[3]);
fbPrintStr(fbWhite, 5, 14, buf);
sprintf(buf, "CTRL:%ld [%02x]", hackflags, pattern);
fbPrintStr(fbWhite, 5, 12, buf);
sprintf(buf, "TV:%s", types[tvtype]);
fbPrintStr(fbWhite, 28, 13, buf);
}
osWritebackDCacheAll();
}
static void
launcher(void) {
int i;
#ifdef IS_BB
hackflags = __osBbHackFlags;
tvtype = osTvType;
num_files = osBbFReadDir(dir, sizeof dir);
#else
strcpy(dir[num_files++].name, "sf64.aes");
strcpy(dir[num_files++].name, "dm64.aes");
strcpy(dir[num_files++].name, "onetri.aes");
strcpy(dir[num_files++].name, "bmonkey.aes");
strcpy(dir[num_files++].name, "wr64.aes");
strcpy(dir[num_files++].name, "mk64.aes");
strcpy(dir[num_files++].name, "base.aes");
strcpy(dir[num_files++].name, "pidma.aes");
strcpy(dir[num_files++].name, "ddr.aes");
strcpy(dir[num_files++].name, "rogue.aes");
strcpy(dir[num_files++].name, "naboo.aes");
strcpy(dir[num_files++].name, "zelda.aes");
strcpy(dir[num_files++].name, "foo.aes");
strcpy(dir[num_files++].name, "bar.aes");
strcpy(dir[num_files++].name, "baz.aes");
strcpy(dir[num_files++].name, "zom.aes");
strcpy(dir[num_files++].name, "aom.aes");
strcpy(dir[num_files++].name, "bom.aes");
strcpy(dir[num_files++].name, "com.aes");
strcpy(dir[num_files++].name, "excite.aes");
strcpy(dir[num_files++].name, "zom.sta");
strcpy(dir[num_files++].name, "naboo.sta");
strcpy(dir[num_files++].name, "mk64.sta");
strcpy(dir[num_files++].name, "aom.sta");
#endif
for (i = 0; i < num_files-1; ++i) {
int j, k;
for (j = i+1; j < num_files; ++j) {
int l1 = strlen(dir[i].name);
int l2 = strlen(dir[j].name);
for (k = 0; k < l1 < l2 ? l1 : l2; ++k) {
if (dir[i].name[k] > dir[j].name[k]) {
OSBbDirEnt tmp = dir[i];
dir[i] = dir[j];
dir[j] = tmp;
break;
} else if (dir[i].name[k] < dir[j].name[k]) {
break;
}
}
}
}
for (i = 0; i < num_files; ++i) {
int j = strlen(dir[i].name);
if ((j > 3 && dir[i].name[j-4] == '.' && dir[i].name[j-3] == 's' &&
dir[i].name[j-2] == 't' && dir[i].name[j-1] == 'a') ||
(j > 3 && dir[i].name[j-4] == '.' && dir[i].name[j-3] == 'p' &&
dir[i].name[j-2] == 'a' && dir[i].name[j-1] == 'k') ||
!bcmp("launch.aes", dir[i].name, sizeof("launch.aes"))) {
if (i && !bcmp(dir[i].name, dir[i-1].name, j-3)) {
dir[i-1].type = 0xff;
}
--num_files;
for (j = i; j < num_files;++j) {
dir[j] = dir[j+1];
}
--i;
}
}
redraw(0);
}
static void
bsod(void) {
int sx = 6;
int x = sx, y = 5;
char *title = "All your base are belong to us!";
char *p = cause;
fbSetBg(0x1e);
fbClear();
fbPrintStr(fbWhite, 40-strlen(title)/2, 2, title);
while (*p) {
switch (*p) {
case '\n':
++y;
x = sx;
break;
case '\t':
x += 8 - (x-sx)%8;
break;
default:
fbPutChar(fbWhite, x, y, *p);
++x;
if (x-sx > 68) {
++y;
x = sx;
}
break;
}
++p;
}
}
static u16 cfb[640*480] __attribute__((aligned(64)));
OSContStatus statusData[MAXCONTROLLERS];
OSContPad controllerData[MAXCONTROLLERS];
static int no_controller = 1;
static void
initController(void)
{
int i, c = 0;
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)) {
++c;
no_controller = 0;
}
}
if (c == 1) __osBbHackFlags = 0;
}
#ifdef IS_BB
#define SHUFFLE
#ifdef SHUFFLE
s32
reencryptFile(const char* file, u8 buffer[], u32 buflen) {
OSBbStatBuf sb;
int len, i, prevlen = 0;
s32 sfd, dfd, rv;
char tmp[BB_INODE16_NAMELEN+2];
for(i = 0; i < BB_INODE16_NAMELEN-3 && file[i] && file[i] != '.'; i++)
tmp[i] = file[i];
tmp[i+0] = '.';
tmp[i+1] = 'r';
tmp[i+2] = 'c';
tmp[i+3] = 'r';
tmp[i+4] = '\0';
if ((sfd = osBbFOpen(file, "r")) < 0) return sfd;
if ((dfd = osBbFCreate(tmp, 1, 0)) < 0) {
if (dfd == BBFS_ERR_EXISTS) {
/* assume operation was interrupted, continue where we left off */
if ((dfd = osBbFOpen(tmp, "w")) >= 0) {
if ((rv = osBbFStat(dfd, &sb, NULL, 0)) == 0) {
/* if a fragment is left, then we are done */
if (sb.size <= buflen) goto out;
/* if zero size, then not started yet */
prevlen = sb.size ? buflen : 0;
goto resume;
}
dfd = rv;
}
}
osBbFClose(sfd);
return dfd;
}
resume:
if ((rv = osBbFStat(sfd, &sb, NULL, 0)) < 0) goto out;
for(i = prevlen; i < sb.size; i += len) {
len = buflen;
if (sb.size-i < len) len = sb.size-i;
if ((rv = osBbFRead(sfd, prevlen, buffer, len)) < 0) break;
if ((rv = osBbFShuffle(sfd, dfd, i!=0, buffer, len)) < 0) break;
prevlen = len;
}
out:
osBbFClose(sfd);
osBbFClose(dfd);
if (rv == 0)
rv = osBbFRename(tmp, file);
return rv;
}
#endif
#define fbhBlue GPACK_RGBA5551(0,0,80,1)
#define fbhGreen GPACK_RGBA5551(0,80,0,1)
#define fbhRed GPACK_RGBA5551(80,0,0,1)
#define fbhYellow GPACK_RGBA5551(80,80,0,1)
#define fbhMagenta GPACK_RGBA5551(80,0,80,1)
#define fbhCyan GPACK_RGBA5551(0,80,80,1)
#define fbLemonChiffon GPACK_RGBA5551(255,250,205,1)
#define fbLightSlateBlue GPACK_RGBA5551(132,112,255,1)
#define fbLightPink1 GPACK_RGBA5551(255,174,185,1)
#define fbThistle2 GPACK_RGBA5551(238,210,238,1)
#define fbSeaGreen4 GPACK_RGBA5551(105,139,105,1)
#define fbTomato1 GPACK_RGBA5551(255,99,71,1)
#define fbTurquoise2 GPACK_RGBA5551(0,229,238,1)
#define fbSienna4 GPACK_RGBA5551(139,71,38,1)
const u16 rainbow[] = {
fbLemonChiffon,
fbLightSlateBlue,
fbLightPink1,
fbSeaGreen4,
fbTomato1,
fbGray,
fbTurquoise2,
fbSienna4
};
static void
draw_filemap(BbFat16* fat, u16 nblocks, u16 bias) {
int x, y, i;
int bad = 0;
int used = 0;
u16 col;
fbRect(64, 16, 64+191, 16+191, fbhGreen);
for(y = 0; y < 4096/64; y++) {
for(x = 0; x < 64; x++) {
u16 e = BB_FAT16_NEXT(fat, bias+y*64+x);
if (e == BB_FAT_RESERVED) col = fbhYellow;
else if (e == BB_FAT_BAD) col = fbhRed;
else continue;
fbRect(64+3*x, 16+3*y, 64+3*x+2, 16+3*y+2, col);
}
}
for(i = 0; i < BB_INODE16_ENTRIES; i++) {
u16 b = fat->inode[i].block;
if (!fat->inode[i].type) continue;
col = rainbow[i&7];
while(b != BB_FAT_LAST) {
if (b >= bias && b < bias+4096) {
x = (b-bias) & 63;
y = (b-bias) >> 6;
fbRect(64+3*x, 16+3*y, 64+3*x+2, 16+3*y+2, col);
}
b = BB_FAT16_NEXT(fat, b);
}
}
for(i = 0; i < nblocks; i++) {
u16 e = BB_FAT16_NEXT(fat, i);
if (e == BB_FAT_BAD) bad++;
else if (e != BB_FAT_AVAIL) used++;
}
fbPrintf(fbWhite, 8, 14, "bad %d used %.1f free %.1f", bad, used*16.f/1024, (nblocks-used)*16.f/1024);
}
#endif
static void
processButton(u16 button, int lastbutton)
{
int old = current, mode;
if ((button & L_TRIG) && (button & R_TRIG) && (button & B_BUTTON))
osBbPowerOff();
if ((button & L_TRIG) && !(lastbutton & L_TRIG)) {
if (bias > 0)
bias -= 4096;
}
if ((button & R_TRIG) && !(lastbutton & R_TRIG)) {
if (bias < osBbCardBlocks(0)-4096)
bias += 4096;
}
mode = (button&Z_TRIG) != 0;
if (current == -1) {
redraw(mode);
return;
}
if (button & D_JPAD && !(lastbutton & D_JPAD)) {
current = (current + 1) % num_files;
}
if (button & U_JPAD && !(lastbutton & U_JPAD)) {
current = (current - 1) < 0? num_files - 1: current-1;
}
if (button & R_JPAD && !(lastbutton & R_JPAD)) {
if (current + (num_files+1)/2 < num_files) {
current += (num_files+1)/2;
}
}
if (button & L_JPAD && !(lastbutton & L_JPAD)) {
if (current >= (num_files+1)/2) {
current -= (num_files+1)/2;
}
}
if (button & B_BUTTON && !(lastbutton & B_BUTTON)) {
tvtype = (tvtype + 1) % 3;
tvtypeset = 1;
}
if (button & L_CBUTTONS && !(lastbutton & L_CBUTTONS)) {
hackflags = (hackflags + 1) % MAXCONTROLLERS;
}
if (button & R_CBUTTONS && !(lastbutton & R_CBUTTONS)) {
currentCP = (currentCP + 1) % MAXCONTROLLERS;
}
if (button & U_CBUTTONS && !(lastbutton & U_CBUTTONS)) {
binding[currentCP] = (binding[currentCP]+1) % 12;
}
if (button & D_CBUTTONS && !(lastbutton & D_CBUTTONS)) {
binding[currentCP] = (binding[currentCP]-1);
if (binding[currentCP] < 0)
binding[currentCP] = 11;
}
if ((button & A_BUTTON && !(lastbutton & A_BUTTON)) ||
(button & START_BUTTON && !(lastbutton & START_BUTTON))) {
if (*cause) {
#ifdef IS_BB
IO_WRITE(MI_CTRL_REG, MI_CTRL_HARD_RESET|MI_CTRL_DIV_MODE_1_5);
#endif
} else {
int fd;
#ifdef SHUFFLE
if (button&START_BUTTON){
//static u8 rb[32*BB_FL_BLOCK_SIZE] __attribute__((aligned(16)));
fbPrintStr(fbRed, 4, 13, "Shuffle: ");
fbPrintStr(fbRed, 15, 13, dir[current].name);
reencryptFile(dir[current].name, (u8*)cfb, 32*BB_FL_BLOCK_SIZE);
}
#endif
fbPrintStr(fbRed, 4, 13, "Launching: ");
fbPrintStr(fbRed, 15, 13, dir[current].name);
osWritebackDCacheAll();
#ifdef IS_BB
__osBbHackFlags = hackflags;
fd = osBbFOpen(dir[current].name, "r");
if (fd >= 0) {
OSBbStatBuf sb;
if (osBbFStat(fd, &sb, blockList, sizeof(blockList)/sizeof(blockList[0])) >= 0) {
OSBbLaunchMetaData md;
u32 addr;
s32 listSize = sb.size / BB_FL_BLOCK_SIZE;
bzero(&md, sizeof(OSBbLaunchMetaData));
md.memSize = 0x400000;
md.tvType = OS_TV_NTSC;
md.romBase = PI_DOM1_ADDR2;
#if 0
if (osBbGetMetaData(&md, blockList, listSize) >= 0) {
osBbLoadState(dir[current].name, &md.state, binding);
}
#endif
if ((addr = osBbLoadApp(&md, blockList, listSize, 0)) != NULL) {
if (tvtypeset) osTvType = tvtype;
osBbExecApp(addr);
}
}
}
#endif
}
}
if (old != current) {
if (current < (num_files+1)/2) {
if (current < top) {
top = current;
} else if (current > top+7) {
top = current - 7;
}
} else {
if (current-(num_files+1)/2 < top) {
top = current-(num_files+1)/2;
} else if (current-(num_files+1)/2 > top+7) {
top = current-(num_files+1)/2 - 7;
}
}
}
redraw(mode);
}
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->errno && pad->button != lastbutton[i]) {
processButton(pad->button, lastbutton[i]);
lastbutton[i] = pad->button;
}
}
}
static void
mainproc(char *argv) {
char tmp[16];
BbTicketId tid;
__osBbNoSk = 1;
osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
osViSetEvent(&retraceMessageQ, dummyMessage, 1);
osViBlack(1);
osViSwapBuffer(cfb);
initController();
#ifndef IS_BB
*cause = 0;
#endif
#ifdef IS_BB
osBbFInit(&fs);
osBbSaveState(tmp, &tid);
#endif
fbInit(*cause ? FB_HIGH_RES : FB_LOW_RES);
if (*cause) {
bsod();
} else {
launcher();
}
osViBlack(0);
osWritebackDCacheAll();
osViSwapBuffer(cfb);
for(;;) {
osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
osContStartReadData(&contMessageQ);
readControllers();
}
}