bb_test.c 28 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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

#include <ultra64.h>
#include "gng.h"
#include "os_bbcard.h"
#include <PR/os_bb.h>
#include <PR/bcp.h>
#include <PR/bbnand.h>
#include "testDriver.h"

#define JCTRL1_ERR 0x1
#define JCTRL2_ERR 0x10
#define JCTRL3_ERR 0x100

int my_strcat(char *dst, const char *append);
#define NDIR 100
#define ONE_SEC      46882325
static OSBbDirEnt dir[NDIR];
static u8 blocks[BB_FL_BLOCK_SIZE];
static OSBbFs fs;
static OSBbStatFs fsb;

int my_poweroff()
{
    osBbPowerOff();

    /* Busy loop to let CPU spin */
    for (; ;);
}

#define  PAUSE_BUT       CONT_B
#define  CONTINUE_BUT    CONT_A
#define  POWEROFF_BUT    (CONT_A | CONT_B | CONT_L | CONT_R)
int pause_start(int which)
{
    int but;

    but = get_button_status(which);
    if ((but & POWEROFF_BUT) == POWEROFF_BUT) 
        my_poweroff();

    if ((but & PAUSE_BUT) == PAUSE_BUT) {
        for (;;) {
            but = get_button_status(which);
            if ((but & POWEROFF_BUT) == POWEROFF_BUT)
                my_poweroff();

            if ((but & CONTINUE_BUT) == CONTINUE_BUT) 
                break;
        }
    }

    return 0;
}

int  get_button_status(int which)
{
    OSContStatus     sdata[MAXCONTROLLERS];
    OSContPad        ctrl_data[MAXCONTROLLERS];
    char pattern;
    int key, i;

    osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
    osContInit(&siMessageQ, &pattern, &sdata[0]);

    osContStartReadData(&siMessageQ);
    osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
    osContGetReadData(ctrl_data);

    for (i=key=0; i<MAXCONTROLLERS; i++) {
        if ( (!(ctrl_data[i].errno &CONT_NO_RESPONSE_ERROR)) &&
             ((which < 0) || (which == i)))
             key |= ctrl_data[i].button;
    }

    return key;
}

int polling_button_down(int which, int key_mask)
{
    OSContStatus     sdata[MAXCONTROLLERS];
    OSContPad        ctrl_data[MAXCONTROLLERS];
    char pattern;
    int key, i;

    osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
    osContInit(&siMessageQ, &pattern, &sdata[0]);

    for (; ;) {
        osContStartReadData(&siMessageQ);
        osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
        osContGetReadData(ctrl_data);

        for (i=key=0; i<MAXCONTROLLERS; i++) {
             if ( (!(ctrl_data[i].errno &CONT_NO_RESPONSE_ERROR)) &&
                  ((which < 0) || (which == i)))
                 key |= ctrl_data[i].button;
        }
      
        if (key & key_mask) break;
    } 
 
    return key;
}

FONT_RENDER font_render;
char font_msg[128*5];
#define DONE_INTERVAL 8438818565    /* 3 minutes */

int bb_jctrl_test()
{
    FONT_RENDER *jctrl=&font_render;
    int i, done[3], buf_change=1;
    OSTime end; 
    OSContStatus     sdata[MAXCONTROLLERS];
    OSContPad        ctrl_data[MAXCONTROLLERS];
    u16              but, old_but[MAXCONTROLLERS], key;
    s8               stickX, old_stickX[MAXCONTROLLERS],
                     stickY, old_stickY[MAXCONTROLLERS];
    char *msg, sxy[64];
    char pattern;


    jctrl->num = 5;
    jctrl->xpos[0] = 70;   jctrl->ypos[0] =  50;
    jctrl->xpos[1] = 40;   jctrl->ypos[1] =  80;
    jctrl->xpos[2] = 40;   jctrl->ypos[2] =  110;
    jctrl->xpos[3] = 40;   jctrl->ypos[3] =  140;
    jctrl->xpos[4] = 30;   jctrl->ypos[4] =  190;

    for (i=0; i<jctrl->num; i++) {
        jctrl->r[i] = jctrl->b[i] = jctrl->g[i] = jctrl->a[i] = 255;
        jctrl->width[i] = 32;
        jctrl->height[i] = 2;
        jctrl->text[i] = font_msg + 128*i;
    }

    sprintf(jctrl->text[0], "BB joy controller test");
    sprintf(jctrl->text[1], "J1 : ");
    sprintf(jctrl->text[2], "J2 : ");
    sprintf(jctrl->text[3], "J3 : ");
    sprintf(jctrl->text[4], "    Please pressed A on all \n controllers within 3 minutes");

    my_gngreport(jctrl);
    for (i=0; i<3; i++) done[i] = 0;

    end = osGetTime() + DONE_INTERVAL;
    osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
    osContInit(&siMessageQ, &pattern, &sdata[0]);
    while (osGetTime() < end) {
        osContStartReadData(&siMessageQ);
        osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
        osContGetReadData(ctrl_data);

        for (i=1, buf_change=0; i<MAXCONTROLLERS; i++) {
             but = ctrl_data[i].button;
             stickX = ctrl_data[i].stick_x & 0xff;
             stickY = ctrl_data[i].stick_y & 0xff;
             buf_change |= (but != old_but[i]) || (old_stickY[i] != stickY) 
                            || (old_stickX[i] != stickX);
        }

        for (i=1; i<MAXCONTROLLERS; i++) {
            but = ctrl_data[i].button;
            stickX = ctrl_data[i].stick_x & 0xff;
            stickY = ctrl_data[i].stick_y & 0xff;
            msg = jctrl->text[i];
            sprintf(msg, "J%d :", i);
            if ( !(ctrl_data[i].errno & CONT_NO_RESPONSE_ERROR )) {
                if (but & CONT_START) my_strcat(msg, " Start");
                if (but & CONT_A) {
                    my_strcat(msg, " A");
                    done[i-1] = 1;
                    jctrl->r[i] = jctrl->b[i] = jctrl->g[i] = 64;
                }
                if (but & CONT_B) my_strcat(msg, " B");
                if (but & CONT_G) my_strcat(msg, " G");
                if (but & CONT_L) my_strcat(msg, " L");
                if (but & CONT_R) my_strcat(msg, " R");
                if (but & CONT_E) my_strcat(msg, " E");
                if (but & CONT_C) my_strcat(msg, " C");
                if (but & CONT_F) my_strcat(msg, " F");
                if (but & CONT_D) my_strcat(msg, " D");
                if (but & CONT_UP) my_strcat(msg, " Up");
                if (but & CONT_DOWN) my_strcat(msg, " Down");
                if (but & CONT_LEFT) my_strcat(msg, " Left");
                if (but & CONT_RIGHT) my_strcat(msg, " Right");
                if (but) my_strcat(msg, "  pressed");

                sprintf(sxy, "\n  x=%d, y=%d\n", stickX, stickY);
                my_strcat(msg,  sxy);
            }
            old_but[i] = but;
            old_stickX[i] = stickX;
            old_stickY[i] = stickY;
        }

        if (buf_change) my_gngreport(jctrl);
        if ((done[0] + done[1] + done[2]) == 3) return 0;

        key = CONT_B | CONT_A | CONT_START;
        if ((ctrl_data[0].button & key) == key) return 0;

        key = CONT_B | CONT_A | CONT_G;
        if ((ctrl_data[0].button & key) == key) 
            return (done[0]?0:JCTRL1_ERR) | (done[1]?0:JCTRL2_ERR) | 
                  (done[2]?0:JCTRL3_ERR);

        if ((ctrl_data[0].button & PAUSE_BUT) == PAUSE_BUT) 
            polling_button_down(0,  CONTINUE_BUT);

        if ((ctrl_data[0].button & POWEROFF_BUT) == POWEROFF_BUT) 
            my_poweroff();
    }
 
    return  (done[0]?0:JCTRL1_ERR) | (done[1]?0:JCTRL2_ERR) | 
            (done[2]?0:JCTRL3_ERR);
}

int bb_sp_test()
{
    rsp();
    return 0;
}

int bb_dp_test()
{
    dlDriver();
    return 0;
}

void my_SasanoFunc();
int bb_vi_test()
{
    my_SasanoFunc();
    gng_report("Video test passed" , 1, GNG_INHIBIT_PRINT, 1);
    return 0;
}

int bb_ai_test()
{
    gng_report("\n Output Sin wave \n\n\n Left channel only" , 1, GNG_INHIBIT_PRINT, 1);
    audioTest(ZARU_ONLYL,SNDLENGTHDEFAULT);

    gng_report("\n Output Sin wave \n\n\n Right channel only" , 1, GNG_INHIBIT_PRINT, 1);
    audioTest(ZARU_ONLYR,SNDLENGTHDEFAULT);

    gng_report("\n Output Sin wave \n\n\n Both channel (stereo)" , 1, GNG_INHIBIT_PRINT, 1);
    audioTest(ZARU_SINWAVE, SNDLENGTHDEFAULT);
    return 0;
}

int bb_flash_err()
{
    int rv, ret=1, count, i;
    char msg[256];

    msg[0] = '\0';
    osBbCardInit();
    if ((rv = osBbFInit(&fs)) < 0) {
        sprintf(msg, "osBbFInit\n Failed %d", rv); 
        goto out;
    }

    if ((rv = osBbFStatFs(&fsb)) < 0) {
        sprintf(msg, "osBbFStatFs\n Failed %d", rv);
        goto out;
    }

    if (fsb.files < 2 /* At lease should have launcher and  gng */
        || fsb.freeFiles != BB_INODE16_ENTRIES-fsb.files)  {
        sprintf(msg, "osBbFFStatFS files failed");
        goto out;
    }

    if ((count = osBbFReadDir(dir, NDIR)) < 0) {
        sprintf(msg, "osBbFReadDir\n failed %d", count);
        goto out;
    }

    if ((i = osBbFCreate("junk.gng", 1, BB_FL_BLOCK_SIZE)) == BBFS_ERR_EXISTS) {
        if ((rv = osBbFDelete("junk.gng")) < 0) {
            sprintf(msg, "Cannot delete existing GNG test %d", rv);
            goto out;
        } 
        i = osBbFCreate("junk.gng", 1, BB_FL_BLOCK_SIZE);
    } 
  
    if (i<0) {
        sprintf(msg, "osBbFCreate\n junk.gng failed %d", i);
        goto out;
    }

    for (count =0; count < sizeof(blocks); count++) 
         blocks[count] = (u8) (count & 0xff);

    if ((rv = osBbFWrite(i, 0, blocks, sizeof blocks)) < 0) {
        sprintf(msg, "osBbFWrite failed");
        goto out;
    }

    if ((rv = osBbFWrite(i, 2, blocks, sizeof blocks/2)) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBbFWrite unaligned test failed");
        goto out;
    }
    
    bzero(blocks, sizeof blocks);
    if ((rv = osBbFRead(i, 0, blocks, sizeof blocks)) < 0 
        || rv != sizeof blocks) {
        sprintf(msg, "osBbFRead failed %d\n", rv);
        goto out;
    }

    for (count =0; count < sizeof(blocks); count++) 
        if ( blocks[count] != (u8) (count & 0xff)) {
            sprintf(msg, "mismatch at %d \n Exp=%d ret=%d", 
                    count, (u8) (count & 0xff), blocks[count]);
            goto out;
        }

    if ((rv = osBbFClose(i)) < 0) {
        sprintf(msg, "osBbFClose failed %d", rv);
        goto out;
    }

    if ((rv = osBbFDelete("junk.gng")) < 0) {
        sprintf(msg, "Cannot delete GNG tmp file %d", rv);
        goto out;
    }

       /* test bad parameters descriptors */
    if ((rv = osBbFClose(-1)) != BBFS_ERR_INVALID ||
	(rv = osBbFClose(1000)) != BBFS_ERR_INVALID ||
	(rv = osBbFClose(400)) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBBFClose error test failed %d", rv);
        goto out;
    }

    if	((rv = osBbFRead(-1, 0, blocks, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFRead(1000, 0, blocks, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFRead(400, 0, blocks, 0)) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBBFRead error test failed %d", rv);
        goto out;
    }

    if ((rv = osBbFWrite(1000, 0, blocks, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFWrite(400, 0, blocks, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFWrite(-1, 0, blocks, 1)) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBBFWrite error test failed %d", rv);
        goto out;
    }
 
    if ((rv = osBbFStat(-1, 0, 0, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFStat(1000, 0, 0, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFStat(400, 0, 0, 0)) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBBFStat error test failed %d", rv);
        goto out;
    }

    if ((rv = osBbFOpen("", "r")) != BBFS_ERR_INVALID ||
	(rv = osBbFOpen(".", "r")) != BBFS_ERR_INVALID ||
	(rv = osBbFOpen("blahblah", "r")) != BBFS_ERR_ENTRY) {
        sprintf(msg, "osBBFOpen error test failed %d", rv);
        goto out;
    }

    if ((rv = osBbFCreate("", 1, 0)) != BBFS_ERR_INVALID ||
	(rv = osBbFCreate(".", 1, 0)) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBBFCreate error test failed %d", rv);
        goto out;
    }

    if ((rv = osBbFDelete("")) != BBFS_ERR_INVALID ||
	(rv = osBbFDelete(".")) != BBFS_ERR_INVALID) {
        sprintf(msg, "osBBFDelete error test failed %d", rv);
        goto out;
    }
	
    if ((rv = osBbFRename("", "foo")) != BBFS_ERR_INVALID ||
	(rv = osBbFRename(".", "foo")) != BBFS_ERR_INVALID ||
	(rv = osBbFRename("foo", "")) != BBFS_ERR_INVALID ||
	(rv = osBbFRename("foo", ".")) != BBFS_ERR_INVALID 
	) {
	sprintf(msg, "osBbFRename invalid param failed %d\n", rv);
	goto out;
    }

    sprintf(msg, "FS test passed\n");
    ret = 0;
out:
    gng_report(msg, 1, GNG_INHIBIT_PRINT,1);
    return ret;
}

#define MON_START_ADDR  (0x01A00000 | K0BASE)
#define LAUNCHER_ADDR   (MON_START_ADDR + (1<<16))
#define NO_SYS_BLOCK    0x01
#define FINIT_ERR       0x10
#define FOPEN_ERR       0x100
#define FSTAT_ERR       0x1000
#define FREAD_ERR       0x10000
#define FWRITE_ERR      0x100000
static u8 block[BB_FL_BLOCK_SIZE] __attribute__((aligned(16)));
static u8 spare[16];
static u32 launcher_size;
static u16 blockList[4096];

int cp_system_data_into_mem()
{
    int rv, i, bad, sysblk=0, fd, offset;
    OSBbStatBuf sb;

    osBbCardInit();  
    for (i=0; i<osBbCardBlocks(0); i++) {
        rv = osBbCardReadBlock(0, i, block, spare);
        bad = (rv < 0) | (spare[BB_FL_BLOCK_STATUS_OFF] != 0xff);
        if (bad==0)  {
            bcopy(block, (void *) (MON_START_ADDR + sysblk*BB_FL_BLOCK_SIZE), BB_FL_BLOCK_SIZE);
            sysblk++;
            if (sysblk == 4) break;
        }
    }
    if (sysblk < 4) return NO_SYS_BLOCK;
    if ((rv = osBbFInit(&fs)) < 0) return FINIT_ERR;
   
    launcher_size = 0;
    if ((fd = osBbFOpen("gng2.aes", "r"))  < 0) 
        return FOPEN_ERR;
    if (osBbFStat(fd, &sb, blockList, sizeof(blockList)) < 0) 
        return FSTAT_ERR;
    launcher_size = sb.size;

    offset = 0;
    while (offset < launcher_size) {
        if (osBbFRead(fd, offset, (void *) (LAUNCHER_ADDR+offset), BB_FL_BLOCK_SIZE) < 0)
            return FREAD_ERR;
        offset += BB_FL_BLOCK_SIZE;
    }
    
    osBbFClose(fd);     
    return 0;
}

#define GNG_NO_CARD   0x1
#define GNG_BAD_PLUG  0x10
#define GNG_SYS_CARD  0x100

int write_system_data()
{
    int rv, i, bad, sysblk=0, fd, offset, blocks;

    osBbCardInit();
    if ((rv = osBbFInit(&fs)) < 0) return FINIT_ERR;
    
    osBbFDelete("launch.gng");
    osBbFRename("launch.aes", "launch.gng");
    if ((fd = osBbFCreate("launch.aes", 1, launcher_size))  < 0)
        return FOPEN_ERR;

    offset = 0;
    while (offset < launcher_size) {
        if (osBbFWrite(fd, offset, (void *) (LAUNCHER_ADDR+offset), BB_FL_BLOCK_SIZE) < 0)
            return FWRITE_ERR;
        offset += BB_FL_BLOCK_SIZE;
    }

    osBbFClose(fd);
    return 0; 
}

int create_burnin_card()
{
    char msg[128];
    int rv, last_status=0, i, start=0, key;
    u8 status;
    OSTime end;

    osBbCardInit();
    for (; ;) {
        if (IO_READ(MI_EINTR_REG) & MI_EINTR_BUT_STATUS) 
            my_poweroff();
        rv = __osBbCardPresent();
        if (!rv) {
            start = 1;
            if (last_status != GNG_NO_CARD)
                gng_report("System card removed. \n\n Please insert a blank card ...",
                           1, GNG_INHIBIT_PRINT, 1);
            last_status = GNG_NO_CARD;
            continue;
        } else if (!start) continue;

        if ((rv = osBbFInit(&fs)) >= 0) {
            i = osBbFOpen("gng2.aes", "r");
            if (i >= 0) {
                osBbFClose(i);
                if (last_status == GNG_SYS_CARD)
                    gng_report("Sorry...\n\nYou might put system card back", 1, GNG_INHIBIT_PRINT, 1);
                last_status = GNG_SYS_CARD;
                continue;
            }
        }

           /* Double confirm */
        if (!__osBbCardPresent()) continue;
        if ((rv = osBbFInit(&fs)) >= 0) {
            if ((i = osBbFOpen("gng2.aes", "r")) >= 0) {
                osBbFClose(i);
                gng_report("Sorry...\n\nYou might put system card back", 1, GNG_INHIBIT_PRINT, 1);
                last_status = 0;
                continue;
            }
        }
        if (!__osBbCardPresent()) continue;

        gng_report("       WARNING\n\n\nLaunch program wil be replaced. \n\nContinue? (A-Yes B-No)",
                   1, GNG_INHIBIT_PRINT, 1); 
        key = polling_button_down(-1, CONT_A | CONT_B);
        if (key & CONT_B) {
            gng_report("\n\nPlease re-insert a blank card...", 1, GNG_INHIBIT_PRINT, 1);
            last_status = 0;
            start = 0;
            continue; 
        }

        rv = write_system_data();
        if (rv) {
            sprintf(msg, "Failed error = %d \n\nPlease re-insert another\n blank card...", rv);
            gng_report(msg, 1, GNG_INHIBIT_PRINT, 1);
            last_status = 0;
            start = 0;
            continue; 
        } 

        gng_report("\n\n      Succeed! \n\n Press Yellow button to\nturn off power ...",
                       1, GNG_INHIBIT_PRINT, 1); 
        polling_button_down(-1, CONT_C | CONT_D | CONT_E | CONT_F);
        my_poweroff();
        break;
    } 
    return 0;
}

#ifdef OLD_METHOD
int create_burnin_card()
{
    char msg[128];
    int rv, num_files, i, ret;
    OSTime end;

    for (; ;) {
        osBbCardInit();

        while (osBbCardUnhappy() != BBCARD_ERR_NO_CARD);
        gng_report("System card removed. \n\n Please insert a blank card ...", 
                   1, GNG_INHIBIT_PRINT, 1);

        while (osBbCardUnhappy() != BBCARD_ERR_CHANGED);
        if (osBbCardUnhappy() == BBCARD_ERR_NO_CARD) continue; 

        osBbCardInit();
        if ((rv = osBbFInit(&fs)) < 0) {
            sprintf(msg, "Please check \nif card is pluged in properly!");
            gng_report(msg, 1, GNG_INHIBIT_PRINT, 1);
            continue;
        }

        if ((rv = osBbFCreate("gng2.aes", 1, BB_FL_BLOCK_SIZE)) 
               == BBFS_ERR_EXISTS) {
            i=1;
        } else {
            if (rv >= 0) osBbFClose(rv);
            osBbFDelete("gng2.aes");
            i=-1;
        }
        if (osBbCardUnhappy() == BBCARD_ERR_NO_CARD) continue;  

        /* i = osBbFOpen("gng2.aes", "r");
        if (i>=0) {
            osBbFClose(i);
            i = osBbFOpen("launch.aes", "r");
            if (i >= 0) osBbFClose(i);
            else continue; 
        }
        if (osBbCardUnhappy() == BBCARD_ERR_NO_CARD) continue;  */
   
        /* for (i=0; i<sizeof(dir); i++) dir[i].name[0] = '\0';
        num_files = osBbFReadDir(dir, sizeof(dir));
        for (i=0; i<num_files; i++) {
           if (bcmp(dir[i].name, "gng2.aes", sizeof("gng2.aes")) == 0) 
               break;
        }

        if (num_files < 0) {
            sprintf(msg, "Please check \nif card is pluged in properly!");
            gng_report(msg, 1, GNG_INHIBIT_PRINT, 1);
            continue;
        }*/

        if (i>=0) {
            gng_report("Sorry...\n\nYou might put system card back",
                       1, GNG_INHIBIT_PRINT, 1);
        } else {
            gng_report("\n\n  Creating in progress\n Please wait...",
                       1, GNG_INHIBIT_PRINT, 1); 
            end = osGetTime() + ONE_SEC * 3;
            while (osGetTime() < end); 
            gng_report("\n\n  Succeed! \nPress A to power off",
                       1, GNG_INHIBIT_PRINT, 1); 
            polling_button_down(-1, CONT_A);
            ret = create_system_apps();
            break;
        }
    } 
    return 0;
}
#endif

void board_level_test() 
{
    int dp_err=0, sp_err=0, vi_err=0, ai_err=0, ret;
    int total, jctrl_err=0, pi_err=0, key, do_next=1;
    char message[512], tmp[128];

    gng_report("    BB Tests", 1, GNG_INHIBIT_PRINT, 1);
    jctrl_err = bb_jctrl_test();
    sp_err = bb_sp_test();
    dp_err = bb_dp_test();
    vi_err = bb_vi_test();
    ai_err = bb_ai_test();

    osBbCardInit();
    pi_err = bb_flash_err();

    total = jctrl_err | sp_err | dp_err | vi_err | ai_err | pi_err;
    
    if (total  != 0) {
        sprintf(message, "    BB test Report\n\n");
        my_strcat(message, "Jctrl ");
        if (jctrl_err & JCTRL1_ERR) my_strcat(message, "1 ");
        if (jctrl_err & JCTRL2_ERR) my_strcat(message, "2 ");
        if (jctrl_err & JCTRL3_ERR) my_strcat(message, "3 ");
        if (jctrl_err) my_strcat(message, "Failed\n");
        else my_strcat(message, "tests passed");
        
        if (sp_err) {
            sprintf(tmp, "%d of 392 SP tests failed\n", sp_err);
            my_strcat(message, tmp);
        } else my_strcat(message, "All 392 sp tests passed\n");
        
        if (dp_err) {
            sprintf(tmp, "%d of 340 DP tests failed\n", dp_err);
            my_strcat(message, tmp);
        } else my_strcat(message, "All 340 DP tests passed\n");
        
        my_strcat(message, vi_err?"Video test failed\n":"Video test passed\n");
        my_strcat(message, ai_err?"Audio test failed\n":"Audio test passed\n");
        my_strcat(message, 
                  pi_err?"Flash test failed\n":"Flash test passed\n\n");

        my_strcat(message, "Yellow button to continue\n");
        my_strcat(message, "A button to poweroff");
        gng_report(message, 1, GNG_INHIBIT_PRINT, 1);

        key = polling_button_down(-1, CONT_A | CONT_E | CONT_C | 
                                      CONT_F | CONT_D);
        do_next = (key & CONT_A)?0:1;
        sprintf(message,"\n\n\n");
    } else sprintf(message, "   ALL tests passed \n\n\n");

    my_strcat(message, "To continue burn-in test\n");
    my_strcat(message, "Please insert a blank card ...");
    if (do_next) {
        gng_report("\n\n\n Prepare system data ...", 1, GNG_INHIBIT_PRINT, 1);   
        if ((ret=cp_system_data_into_mem())) {
            sprintf(message, "\n\n\n Error %d \nCannot copy system data",ret); 
            my_strcat(message, "\ninto memory for new card!");
           
            gng_report(message, 1, GNG_INHIBIT_PRINT, 1);
            key = polling_button_down(-1, CONT_A); 
        }  

        gng_report(message, 1, GNG_INHIBIT_PRINT, 1); 
        create_burnin_card();
    }

    my_poweroff();
    return;
}

#define ERR_OFF   IO_WRITE(PI_GPIO_REG, \
                          ((PI_GPIO_ERROR_BIT | PI_GPIO_POWER_BIT) << PI_GPIO_ENABLE_SHIFT) | \
                           (PI_GPIO_ERROR_BIT | PI_GPIO_POWER_BIT))
#define ERR_ON    IO_WRITE(PI_GPIO_REG, \
                          ((PI_GPIO_ERROR_BIT | PI_GPIO_POWER_BIT) << PI_GPIO_ENABLE_SHIFT) | \
                           PI_GPIO_POWER_BIT)

#define BURN_IN_LEN  20
int do_burn_in_test()
{
    OSTime end;
    OSContStatus     sdata[MAXCONTROLLERS];
    OSContPad        ctrl_data[MAXCONTROLLERS];
    char pattern;
    int key, i, key_mask1=CONT_G|CONT_L|CONT_R, err_on=1, burns;
    int key_mask2=CONT_B|CONT_L|CONT_R;

    osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
    osContInit(&siMessageQ, &pattern, &sdata[0]);

    gng_report("  Burn in test" , 1, GNG_INHIBIT_PRINT, 1);
    for (burns=0; burns<BURN_IN_LEN;) {
        if (err_on < 0) {
            if (osGetTime() > end) 
                err_on = (err_on==-2)?1:2;
        } else { 
            (err_on==2)?ERR_OFF:ERR_ON;
            end = osGetTime() + (ONE_SEC*3/4);
            err_on = -err_on;
            burns++;
        }

        osContStartReadData(&siMessageQ);
        osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
        osContGetReadData(ctrl_data);

        for (i=key=0; i<MAXCONTROLLERS; i++) {
            if ( !(ctrl_data[i].errno & CONT_NO_RESPONSE_ERROR )) 
                 key |= ctrl_data[i].button;
        }

        if ((key & key_mask1) == key_mask1) {
            ERR_ON;
            return 1001;
        }
        if ((key & key_mask2) == key_mask2) {
            ERR_ON;
            return 4321;
        }
    }
     
    ERR_OFF;
    return 0;
}

#define JOY_XY_MIN  52
int bb_lctrl_test()
{
    FONT_RENDER *lctrl=&font_render;
    OSContStatus     sdata[MAXCONTROLLERS];
    OSContPad        ctrl_data[MAXCONTROLLERS];
    char  lctrl_down[19];
    char pattern;
    int  width[19] = { 32, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
    int   xpos[19] = { 80, 80, 30, 120, 80, 80, 30, 120, 80, 60, 260, 100, 150, 
                       240, 210, 270, 240, 200, 230}, done=-1, but, i;
    int   ypos[19] = { 20, 90, 110, 110, 130, 160, 180, 180, 200, 60, 60, 60, 80, 
                       90, 110, 110, 130, 155, 180}; 
    char  *lctrl_name[19]={"BB Local Ctrl Test", "JT", "JL", "JR", "JB", "Up", "Left",
                         "Right", "Down", "L", "R", "Z", "Start",
                         "C", "D", "E", "F", "B", "A"}, x, y, old_x=0, old_y=0, buf_change=1;
    
    lctrl->num = 19;
    for (i=0; i<lctrl->num; i++) {
        lctrl->xpos[i] = xpos[i];
        lctrl->ypos[i] = ypos[i];
        lctrl->r[i] = lctrl->b[i] = lctrl->g[i] = lctrl->a[i] = 255;
        lctrl->width[i] = width[i];
        lctrl->height[i] = 1;
        lctrl->text[i] = font_msg + 32*i;
        sprintf(lctrl->text[i], "%s", lctrl_name[i]);
        lctrl_down[i] = 0;
    }
    
    osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
    osContInit(&siMessageQ, &pattern, &sdata[0]);
    for ( ; ;) {
        if (IO_READ(MI_EINTR_REG) & MI_EINTR_BUT_STATUS) 
            my_poweroff();

        osContStartReadData(&siMessageQ);
        osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
        osContGetReadData(ctrl_data);

        x = ctrl_data[0].stick_x & 0xff;
        y = ctrl_data[0].stick_y & 0xff;
        but = ctrl_data[0].button;
   
        if (y > JOY_XY_MIN)  lctrl_down[1] = 1;
        if (x < -JOY_XY_MIN) lctrl_down[2] = 1;
        if (x > JOY_XY_MIN)  lctrl_down[3] = 1;
        if (y < -JOY_XY_MIN)  lctrl_down[4] = 1;

        if (but & CONT_UP) lctrl_down[5] = 1;
        if (but & CONT_LEFT) lctrl_down[6] = 1;
        if (but & CONT_RIGHT) lctrl_down[7] = 1;
        if (but & CONT_DOWN) lctrl_down[8] = 1;

        if (but & CONT_L) lctrl_down[9] = 1;
        if (but & CONT_R) lctrl_down[10] = 1;
        if (but & CONT_G) lctrl_down[11] = 1;
        if (but & CONT_START) lctrl_down[12] = 1;

        if (but & CONT_E) lctrl_down[13] = 1;
        if (but & CONT_C) lctrl_down[14] = 1;
        if (but & CONT_F) lctrl_down[15] = 1;
        if (but & CONT_D) lctrl_down[16] = 1;

        if (but & CONT_A) lctrl_down[18] = 1;
        if (but & CONT_B) lctrl_down[17] = 1;
 
        for (i=but=0; i<19; i++) {
            if (lctrl_down[i]) {
                but++;
                lctrl->r[i] = lctrl->b[i] = lctrl->g[i] = lctrl->a[i] = 64;
            }
        }
        sprintf(lctrl->text[2], "%s(%d)", lctrl_name[2], x); 
        sprintf(lctrl->text[4], "%s(%d)", lctrl_name[4], y); 

        if (!buf_change) {
            if ((x != old_x) || (y != old_y) || (but != done)) buf_change=1;
        } 
        
        if (buf_change) my_gngreport(lctrl);
        buf_change = 0;
        old_x = x;
        old_y = y;
        done = but;
        if (done >= 18) break;
    }
    return 0;
}

void burn_in_test()
{
    int rv, fd, key;
    char message[1024];
    OSBbStatBuf sb;
    OSTime burn_in_start;

    IO_WRITE(PI_ACCESS_REG, PI_ACCESS_ALL);
restart:
    osBbCardInit();
    if ((rv = osBbFInit(&fs)) < 0) {
        ERR_ON;
        for (;;);
        return;
    }

    if ((fd = osBbFOpen("errlog.gng", "r")) < 0) { 
         /* No file exist, run burn in test */
         burn_in_start = osGetTime();
         if ((rv=do_burn_in_test())) {
             fd = osBbFCreate("errlog.gng", 1, BB_FL_BLOCK_SIZE);
             sprintf(blocks, "Error code: %d \n Fail at %8.3f secs\n",
                     rv, (float) (1. * (osGetTime() - burn_in_start) /ONE_SEC)); 
             if (fd>=0) {
                 osBbFWrite(fd, 0, blocks, sizeof(blocks));
                 osBbFClose(fd);
             } 
             ERR_ON;
             __osDisableInt();
             IO_WRITE(MI_SEC_MODE_REG, 0);
             for (;;) {
                 if (IO_READ(MI_EINTR_REG) & MI_EINTR_BUT_STATUS)
                     my_poweroff();
             }
             return;
         } else osBbFCreate("errlog.gng", 1, 0);
    } else { /* Check err message */
         if (osBbFStat(fd, &sb, blockList, sizeof(blockList)) < 0) {
             osBbFClose(fd);
             ERR_ON;
             for (;;);
             return;
         }
         if (sb.size == 0) { /* do vi/ai test and lctrl test */ 
             bb_vi_test();
             bb_ai_test();
             bb_lctrl_test();
             osBbFClose(fd);
             osBbFDelete("errlog.gng");
         } else {
             ERR_ON;
             sprintf(message," Burn in test Failure report\n\n");
             bzero(blocks, sizeof blocks);
             if ((rv = osBbFRead(fd, 0, blocks, sizeof blocks)) < 0
                  || rv != sizeof blocks) {
                 sprintf(message,
                       " Burn in test Failure report\n\n read=%d Bytes\n", rv);
             } else {
                 my_strcat(message, blocks);
             }   
             my_strcat(message, "\nPress A to restart, ");
             my_strcat(message, "\n  yellow button to power off");
             gng_report(message, 1, GNG_INHIBIT_PRINT, 1);
             key = polling_button_down(-1, CONT_A | CONT_E | CONT_C |
                                      CONT_F | CONT_D);
             osBbFClose(fd);
             if (key & CONT_A) {
                 osBbFDelete("errlog.gng");
                 ERR_OFF;
                 goto restart;
             }
         }
    }
    
    my_poweroff();
    return;
}