/**
 *  Sega Saturn Shift benchmark program (Featuring RTC acquisition test code).
 *  See: http://segaxtreme.net/community/topic/18391-vdp-1-3d
 *  by cafe-alpha.
 *  WWW: http://ppcenter.free.fr/
 *
 *  Based on Sega Saturn VDP1 example program
 *  by Charles MacDonald
 *  WWW: http://cgfm2.emuviews.com
**/

#include "shared.h"


/* Define line filled with spaces in order to erase a line. */
char* empty_line = "                                      ";


/* Common use in tests. */
unsigned char test_buffer[TEST_BUFFER_SIZE];



char* menu_list[] = { 
    "* BGET                ", 
    "* SLEEP               ", 
    "* BUP                 ", 
    "* PAD                 ", 
    "* TIMER               ", 
    "* CDROM               ", 
    "* GOURAUD             ", 
    "* SHIFTBENCH          ", 
    "* SMPC_RTC            ", 
    NULL};
int menu_index = 0;
int menu_select(void)
{
    char* str = NULL;
    unsigned short fctr=0;
    int i, n, l = menu_index;

    for(i=0; i<20; i++)
    {
        _printf(2, i+6, COLOR_WHITE, empty_line);
    }

    n = 0; while(menu_list[n+1] != NULL) {n++;} n++;

    do
    {
        /* Wait for V-Blank + perform transfers for SatCom. */
        my_wait_vblank();

        /* Test debugger: STT */
        if(pad.new & PAD_LBUTTON)
        { /* Send debug message to PC. */
            scd_logout("==FRAME=%d, rand1=0x%04X, rand2=0x%04X", fctr, rand()&0xFFFF, rand()&0xFFFF);
            if(fctr%2 == 0) scd_logout("**[%d]Test2", fctr, 42);
        }
        if(pad.delta & PAD_RBUTTON)
        { /* Prompt message from PC. */
            _printf(2, row-1, COLOR_WHITE, empty_line);
            _printf(2, row-1, COLOR_WHITE, "What's your name ?");
            str = scd_prompt("Your name:");
            _printf(2, row-1, COLOR_WHITE, empty_line);
            _printf(2, row-1, COLOR_WHITE, "Hello %s !", str);

        }
        /* Test debugger: END */


        /* Poll joypad */
        pad_read(&pad);

        /* Start - Quit program */
        if(pad.new & PAD_START)
        { /* Exit to multiplayer. */
            sc_exit_to_multiplayer();
        }


        if((pad.delta & PAD_UP) || (pad.new & PAD_LEFT))
        {
            l = (l != 0 ? l-1 : n-1);
        }
        else if((pad.delta & PAD_DOWN) || (pad.new & PAD_RIGHT))
        {
            l = (l != (n-1) ? l+1 : 0);
        }

        _printf(2, 6, COLOR_YELLOW, "        --- Selection Menu ---        ");
        for(i=0; i<n; i++)
        {
            _printf(2, i+7, (i==l ? COLOR_BG_FG(COLOR_RED, COLOR_WHITE) : COLOR_WHITE), "%s", menu_list[i]);
        }
        _printf(2, i+7 + 0, COLOR_YELLOW, "Note:Logger needed when * is on left. ");
        _printf(2, i+7 + 1, COLOR_YELLOW, "Note:Press L/R to output log message. ");
    }
    while(!((pad.delta & PAD_A) || (pad.delta & PAD_B) || (pad.delta & PAD_C)) );
    for(i=0; i<20; i++)
    {
        _printf(2, i+6, COLOR_WHITE, empty_line);
    }
    if((pad.delta & PAD_A) || (pad.delta & PAD_C))
    {
        menu_index = l;
    }
    return menu_index;
}




/* Global variables (for use from test functions). */
int row;
int startrow;
pad_t pad;

void _main(void)
{
    unsigned short* p;
    int x, y;

    /* SatCom: initialization. */
    sc_init();

    vdp_init();
    conio_init();
    pad_init();

    /* Set up VDP1 to appear under VDP2 graphics (0=hide, 1-7=visible) */
    PRISA = 0x0101;
    PRISB = 0x0101;
    PRISC = 0x0101;
    PRISD = 0x0101;

    /* Set up VDP1 registers */
    TVMR = 0x0000;
    FBCR = 0x0000;
    PTMR = 0x0002;
    EWDR = COLOR(7,7,7);    /* Erase color: gray */
    EWLR = 0x0000;          /* Erase range: (0,0) -> (352,240) */
    EWRR = 0x50DF;

    /* Make texture map */
    p = (unsigned short *)0x25C40000;
    for(y=0;y<TX_H;y++)
    {
        for(x=0;x<TX_W;x++)
        {
            p[y*TX_W+x] = COLOR(x*x/16, x*y/16, y*y/16);
            //p[y*TX_W+x] = COLOR((x*x-y*y)>>4,x^y,(x*x+y*y)>>6);
        }
    }

    /* Make Gouraud shading table */
    p = (unsigned short *)0x25C60000;
    p[0] = COLOR(31,0,0);
    p[1] = COLOR(0,31,0);
    p[2] = COLOR(0,0,31);
    p[3] = COLOR(31,31,31);


    /* Print startup message */
    row=1;

    _printf(2, row++, COLOR_TEAL, "=/      Shift Benchmark program   \\=");
    _printf(2, row++, COLOR_YELLOW, "= By cafe-alpha (ppcenter.free.fr) =");
    _printf(2, row++, COLOR_YELLOW, "Build on " RELEASE_DATE);
    row++;
    _printf(2, row++, COLOR_SILVER, "From VDP1EX by Charles MacDonald");
    _printf(2, row++, COLOR_SILVER, "WWW: http://cgfm2.emuviews.com");
    row++;

    /* Startup screen end. */
    row++;


    /* Screen on */
    TVMD = 0x8000;
    TVMR = 0x0000;
    FBCR = 0x0001;
    EWRR = 0x50DF;


    /* Initialize VDP1 display list */
    list_clear();
    list_end();


    startrow = row;

    /* By default, select the last entry in menu. */
    menu_index = 0; while(menu_list[menu_index+1] != NULL) {menu_index++;}

    while(1)
    {
        /* Select which pattern we will test. */
        menu_select();


        switch(menu_index)
        {
        default:
        case(0):
            test_bget();
            break;

        case(1):
            test_sleep();
            break;

        case(2):
            test_bup();
            break;

        case(3):
            test_pad();
            break;

        case(4):
            test_timer();
            break;

        case(5):
            test_cdrom();
            break;

        case(6):
            test_gouraud();
            break;

        case(7):
            test_shiftbench();
            break;

        case(8):
            test_smpc_rtc();
            break;
        }
    }


    vdp_shutdown();
    pad_shutdown();
}

