auxdata.c 11.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
#include "ultra64.h"
#include "os_bb.h"
#include "os_bbfs.h"
#include "os_bbexec.h"

#include "auxdata.h"
#include "libfb.h"

extern unsigned char auxdatabuf[AUXDATABUFSIZE];

extern char *strchr(const char *, int);
extern int strcmp(const char *, const char *);
extern int strncmp(const char *, const char *, int);
extern void skExit(void);

s32 binding[] = {0, 1, 2, 3};
static    u8 pattern;
static u16 lastbutton[MAXCONTROLLERS];
static char msg[256];
static u16 cfb[320*240] __attribute__((aligned(64)));
OSContStatus statusData[MAXCONTROLLERS];
OSContPad controllerData[MAXCONTROLLERS];

#define DMA_QUEUE_SIZE	200

/*
 * 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;

static OSMesg           nmiMessages[1];
static OSMesgQueue      nmiMessageQ;

OSPiHandle              *handler;

#define NUM_ACCESS_MESG 1
static OSMesgQueue accessQueue;
static OSMesg accessBuf[NUM_ACCESS_MESG];
static int queueCreated = 0;

static void
createQueue(void)
{
    osCreateMesgQueue(&accessQueue, accessBuf, NUM_ACCESS_MESG);
    (void) osSendMesg(&accessQueue, (OSMesg)0, OS_MESG_NOBLOCK);
    queueCreated = 1;
}

static void
getAccess(void)
{
    OSMesg dummyMesg;

    /* Create access message queue if haven't done so */
    if (!queueCreated) {
        createQueue();
    }

    (void)osRecvMesg(&accessQueue, &dummyMesg, OS_MESG_BLOCK);
}

static void
relAccess(void)
{
    (void) osSendMesg(&accessQueue, (OSMesg)0, OS_MESG_NOBLOCK);
}

/*
 * Local variables and routines
 */

static void idleproc(char *);
static void mainproc(char *);

static u32 entryCode;
void
boot(u32 reg)
{
    entryCode = reg;

    /*
     * Make the video output black
     */
    IO_WRITE(VI_H_START_REG, 0x0);

    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);

    osCreateMesgQueue(&nmiMessageQ, nmiMessages, 1);
    osSetEventMesg(OS_EVENT_PRENMI, &nmiMessageQ, (OSMesg)1);

    /*
     * 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, (OSPri)7);
    osStartThread(&mainThread);

    osSetThreadPri(0, OS_PRIORITY_IDLE);
    for(;;);                                    /* idle thread */
}

static OSMesgQueue	retraceMessageQ;
static OSMesg		dummyMessage, retraceMessageBuf;

static OSBbDirEnt dir[410];
static int num_files = 0;
static int current = 0;

static int top = 0;

static void
drawFile(int i) {
    int x, y;
    u16 color;
    if ((i < top || i > (top+7)) && (i < ((num_files+1)/2+top) || i > ((num_files+1)/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));
    fbPrintStr(color, x, y, dir[i].name);
}

static void
redraw(void)
{
    int i;
    char *title =  "AuxData Test";

    fbClear();
    fbPrintStr(fbMarkColor, 2+16-strlen(title)/2, 2, title);
    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");
    }

    if (*msg) {
        fbPrintStr(fbRed, 4, 13, msg);
    }

    osWritebackDCacheAll();
}

static void
readFiles(void)
{
    int i;

    num_files = osBbFReadDir(dir, sizeof dir/sizeof dir[0]);
    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);
	char *ext = strchr(dir[i].name, '.');
	if (j > 4 && ext != NULL) {
	    ext++;
	    if (!strcmp(ext, "sta"))
		continue;
	    if (!strcmp(ext, "pak"))
		continue;
	    if (!strncmp(ext, "u0", 2))
		continue;
	}
        --num_files;
        for (j = i; j < num_files;++j) {
            dir[j] = dir[j+1];
        }
        --i;
    }
}

static void
initController(void)
{
    osCreateMesgQueue(&contMessageQ, &dummyMessage, 1);
    osSetEventMesg(OS_EVENT_SI, &contMessageQ, (OSMesg) 0);

    osContInit(&contMessageQ, &pattern, &statusData[0]);
}

#define DCACHE_ALIGN	__attribute__ ((aligned (16)))

u8 auxbuf[0x8000] DCACHE_ALIGN;
u8 auxid = 0;

void
strcat(char *t, char *f)
{
    while (*t) t++;
    while ((*t++ = *f++)) ;
}

static void
commitState()
{
    int rv;
    
    rv = osBbGameCommitState();
    if (rv < 0)
        sprintf(msg, "State commit rv %d", rv);
    else
        sprintf(msg, "State committed");
    readFiles(); 
    redraw();
}

static void
displayAuxList()
{
    int rv, i;
    u8 list[16];
    char buf[16];

    memset(list, 0x42, sizeof list);
    if ((rv = osBbAuxDataIds(list)) < 0) {
	sprintf(msg, "GetIds failed (%d)", rv);
    } else if (rv == 0) {
        sprintf(msg, "No Aux Data");
    } else {
        sprintf(msg, "Aux: ");
	for (i = 0; i < rv; i++) {
	    sprintf(buf, "%d ", list[i]);
	    strcat(msg, buf);
	}
    }

    redraw();
}

static void
createAuxData()
{
    int rv;
    int i;

    memset(auxbuf, 0, sizeof auxbuf);
    sprintf(auxbuf, "aux data id %d\n", auxid);
    for (i = 100; i < sizeof auxbuf; i++)
	auxbuf[i] = auxid + i;
    rv = osBbAuxDataSave(auxid, auxbuf, sizeof auxbuf);
    if (rv == sizeof auxbuf)
	sprintf(msg, "Saved Aux %d", auxid);
    else
	sprintf(msg, "Save %d failed (%d)", auxid, rv);
    readFiles(); 
    redraw();
}

static void
loadAuxData()
{
    int rv;
    int i;
    u8 buf[64];

    memset(auxbuf, 0, sizeof auxbuf);
    sprintf(buf, "aux data id %d\n", auxid);
    rv = osBbAuxDataLoad(auxid, auxbuf, sizeof auxbuf);
    if (rv != sizeof auxbuf) {
	sprintf(msg, "Load %d failed (%d)", auxid, rv);
    } else if (strcmp(buf, auxbuf)) {
	sprintf(msg, "Load %d bad str", auxid);
    } else {
        for (i = 100; i < sizeof auxbuf; i++) {
	    if (auxbuf[i] != (u8)(auxid + i))
		break;
	}
	if (i < sizeof auxbuf) {
	    sprintf(msg, "Load %d @(%d) %x sb %x", auxid, i, auxbuf[i], (u8)(auxid + i));
	} else {
	    sprintf(msg, "Load %d ok", auxid);
	}
    }
    redraw();
}

static void
deleteAuxData()
{
    int rv;

    rv = osBbAuxDataDelete(auxid);
    if (rv >= 0)
	sprintf(msg, "Aux %d deleted", auxid);
    else
	sprintf(msg, "Delete Aux %d failed %d", auxid, rv);
    readFiles(); 
    redraw();
}

static void
sizeAuxData()
{
    int rv;

    rv = osBbAuxDataSize(auxid);
    if (rv >= 0)
	sprintf(msg, "Aux %d: %d bytes", auxid, rv);
    else
	sprintf(msg, "Size Aux %d failed %d", auxid, rv);
    redraw();
}

static void
deleteFile(char *name)
{
    int rv;

    if ((rv = osBbFDelete(name)) < 0) {
	sprintf(msg, "Delete %s failed (%d)", name, rv);
    } else {
	if (current > num_files)
	    current = num_files;
        sprintf(msg, "Deleted %s", name);
        readFiles();
    }

    redraw();
}

static void
executeApp(char *name)
{
    int fd;

    fd = osBbFOpen(name, "r");
    if (fd >= 0) {
        OSBbStatBuf sb;
        if (osBbFStat(fd, &sb, NULL, 0) >= 0) {
            sprintf(msg, "Size %s = %ld (%ld blocks)", name, sb.size, sb.size/16384);
	} else {
	    sprintf(msg, "Failed to stat %s", name);
	}
    }

    redraw();
}
    
static void
processButton(u16 button, int lastbutton)
{
    int old = current;

    if (current == -1) {
	redraw();
	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 & A_BUTTON && !(lastbutton & A_BUTTON)) {
        executeApp(dir[current].name);
    }

    if (button & START_BUTTON && !(lastbutton & START_BUTTON)) {
	commitState();
    }

    if (button & B_BUTTON && !(lastbutton & B_BUTTON)) {
        displayAuxList();
    }
    
    if (button & L_TRIG && !(lastbutton & L_TRIG)) {
        deleteAuxData();
    }

    if (button & R_TRIG && !(lastbutton & R_TRIG)) {
        createAuxData();
    }

    if (button & Z_TRIG && !(lastbutton & Z_TRIG)) {
        deleteFile(dir[current].name);
    }

    if (button & U_CBUTTONS && !(lastbutton & U_CBUTTONS)) {
	sprintf(msg, "Auxid %d", ++auxid);
    }

    if (button & D_CBUTTONS && !(lastbutton & D_CBUTTONS)) {
	sprintf(msg, "Auxid %d", --auxid);
    }

    if (button & L_CBUTTONS && !(lastbutton & L_CBUTTONS)) {
	loadAuxData();
    }

    if (button & R_CBUTTONS && !(lastbutton & R_CBUTTONS)) {
	sizeAuxData();
    }

    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();
}

static void
readControllers(void)
{
    int i;
    OSContPad *pad;

    getAccess();
    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;
        }
    }
    relAccess();
}

static void 
mainproc(char *argv)
{
    osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
    osViSetEvent(&retraceMessageQ, dummyMessage, 1);
    osViBlack(1);
    osViSwapBuffer(cfb);
    initController();

    osBbAuxDataInit(auxdatabuf, sizeof(auxdatabuf));
    
    fbInit(FB_LOW_RES);

    readFiles();
    redraw();

#ifdef DEBUG
    gdbInit();
#endif

    osViBlack(0);
    osViSwapBuffer(cfb);

    for(;;) {
        osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
        osContStartReadData(&contMessageQ);
        if (osRecvMesg(&nmiMessageQ, NULL, OS_MESG_NOBLOCK) == 0)
            skExit();
        readControllers();
    }
}