
#include "shared.h"



void test_bget(void)
{
    /* bget tayst STT */
    scm_alloc_init(SCM_LRAM);
    unsigned char* buff = (unsigned char*)scm_malloc(500, /*SCM_LRAM | */SCM_RAMCART);
    scd_logout("scm_malloc = 0x%08X", buff); scd_logflush();
    while(1)
    {
        sc_check();
    }
    /* bget tayst END */
}



void test_sleep(void)
{
    /* Sleep tayst STT */
    while(1)
    {
        int ii;

        /* SatCom: if needed, transfer data. */
        sc_check();

        /* Wait for V-Blank. */
        wait_vblank_out();
        wait_vblank_in();

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


        if(pad.new & PAD_X)
        {
            scd_logout("Sleep for 5 seconds ...", 0); scd_logflush();
            sc_usleep(5*1000*1000);
            scd_logout("Sleep ended !", 0); scd_logflush();
        }
        if(pad.new & PAD_Y)
        {
            scd_logout("Sleep for 5msec * 1000 ...", 0); scd_logflush();
            for(ii=0; ii<1000/20; ii++)
            {
                sc_usleep(5*1000 * 20);
            }
            scd_logout("Sleep ended !", 0); scd_logflush();
        }
    }
    /* Sleep tayst END */
}


void test_bup(void)
{
#if 0
    /* BUP tayst STT */
//typedef struct BupDate {
//	Uint8	year;		/* Year - 1980 */
//	Uint8	month;		/* Month (1~12) */
//	Uint8	day;		/* Day (1~31) */
//	Uint8	time;		/* Hour (0~23) */
//	Uint8	min;		/* Minute (0~59) */
//	Uint8	week;
//}
BupDate date;
unsigned long ret;
int ii;

for(ii=0; ii<8; ii++)
{
if(ii == 0)
{
    date.year  = 0;
    date.month = 0;
    date.day   = 0;
    date.time  = 0;
    date.min   = 0;
    date.week  = 0;
}
else if(ii == 1)
{
    date.year  = 2010 - 1980;
    date.month = 11;
    date.day   = 10;
    date.time  = 0;
    date.min   = 47;
    date.week  = 3;
}
else if(ii == 2)
{
    date.year  = 2010 - 1980;
    date.month = 11;
    date.day   = 10;
    date.time  = 1;
    date.min   = 47;
    date.week  = 3;
}
else if(ii == 3)
{
    date.year  = 2010 - 1980;
    date.month = 11;
    date.day   = 10;
    date.time  = 1;
    date.min   = 46;
    date.week  = 3;
}
else if(ii == 4)
{
    date.year  = 2010 - 1980;
    date.month = 11;
    date.day   = 10;
    date.time  = 1;
    date.min   = 46;
    date.week  = 4;
}
else if(ii == 5)
{
    date.year  = 2010 - 1980;
    date.month = 11;
    date.day   = 11;
    date.time  = 1;
    date.min   = 46;
    date.week  = 4;
}
else if(ii == 6)
{
    date.year  = 2010 - 1980;
    date.month = 12;
    date.day   = 11;
    date.time  = 1;
    date.min   = 46;
    date.week  = 4;
}
else if(ii == 7)
{
    date.year  = 2011 - 1980;
    date.month = 12;
    date.day   = 11;
    date.time  = 1;
    date.min   = 46;
    date.week  = 4;
}
ret = BUP_SetDate(&date);
scd_logout("%4d/%2d/%2d %2d:%2d (%d) => 0x%08X (%d)", date.year+1980, date.month, date.day, date.time, date.min, date.week, ret, ret);
}
#endif

    //char* bupstr = "HELLO";
    //char* bupstr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In pulvinar molestie elit non consequat. Sed facilisis nibh nec arcu euismod viverra. Mauris quis pretium orci. Sed magna purus, adipiscing vel gravida quis, dictum nec risus. Praesent libero sem, mollis sit amet congue vitae, ornare vel urna. Duis cursus rhoncus lectus, vitae porttitor ligula fermentum sed. Ut nibh odio, rutrum vitae sagittis ut, mattis sed sem. Duis vehicula purus molestie orci gravida quis tincidunt massa luctus. Nullam scelerisque euismod leo, sit amet dictum orci lobortis at. Quisque consequat consectetur dictum. Donec adipiscing ultrices purus, pretium scelerisque turpis adipiscing mattis. Ut in dui lectus. Sed et nisi neque. Vestibulum dignissim vulputate erat at volutpat. Nulla convallis, ipsum vel aliquet tempus, ligula nisl porta nibh, quis euismod nisi ipsum a enim. Sed et velit metus. Phasellus vehicula vehicula elit at tempus. Vestibulum dolor leo, elementum non mattis sit amet, molestie nec tellus. Nam scelerisque enim id metus pulvinar et iaculis leo cursus. Nunc dictum cursus dui sed posuere. Ut mattis vulputate libero. Morbi sodales justo risus. Fusce arcu magna, posuere eu auctor vel, adipiscing vel massa. Pellentesque ultrices lorem vel diam convallis id imperdiet mi lobortis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Integer tortor est, vulputate ut consequat vitae, varius eget purus. Nunc dictum posuere ornare. Duis mollis nulla non augue ornare ac facilisis nunc venenatis. Suspendisse in purus nunc. Aliquam pharetra, nibh a dictum gravida, felis ante adipiscing mi, eget pulvinar purus arcu non turpis. Praesent luctus felis sit amet dolor accumsan ut feugiat velit vehicula. Mauris semper erat leo, sed feugiat velit. Donec sed pulvinar dui. Mauris lacus odio, tempor sed faucibus eu, euismod vitae elit. Maecenas feugiat fringilla tincidunt. Praesent eu metus vitae quam venenatis euismod. Nulla velit lacus, adipiscing nec pulvinar eu, tempus eu orci. Donec dui diam, molestie sit amet elementum ac, lacinia eu libero. Aliquam mauris risus, scelerisque eleifend tristique ut, accumsan eu odio. Maecenas fringilla dapibus nisl in egestas. Nam euismod risus id libero tristique eget viverra velit rutrum. Maecenas eget dui vitae massa varius luctus vel quis turpis. In sagittis sodales nisi. Morbi vitae dolor purus, eu aliquet tortor. Vestibulum euismod neque eget justo elementum varius. Vivamus mauris magna, sodales sit amet ultricies sit amet, dignissim a neque. Aliquam pretium convallis sollicitudin. Nam ut nunc a nisi porta egestas. Vestibulum scelerisque blandit erat vel rhoncus. Donec ac elit eros, eu tempus dui. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aenean risus nulla, venenatis ut consequat a, venenatis eget nunc. Vivamus facilisis metus et metus imperdiet aliquam vel quis dolor. In euismod diam eros.";
    char* bupstr = "ZZZZZZZZZZZZZZZZZZZZZZZZZ";
    //long sc_bup_write(void* data, unsigned long datalen, char* name, char* comment);
    sc_bup_write((void*)bupstr, strlen(bupstr), "TEST0", "COMMENT");
    /* BUP tayst END */
}


void test_pad(void)
{
    /* Pad tayst STT */
    scd_logout("1000  ticks = %d usec", sc_tmr_tick2usec(1000));
    scd_logout("65535 ticks = %d usec", sc_tmr_tick2usec(65535));
    _printf(2, row+0, COLOR_TEAL, "Hit any key !");
    while(1)
    {
        /* SatCom: if needed, transfer data. */
        sc_check();

        /* Wait for V-Blank. */
        wait_vblank_out();
        wait_vblank_in();

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

        /* Up */
        _printf( 5, row+1, COLOR_TEAL, " %c", 
                    (pad.new & PAD_UP ? 'U' : ' '));
        /* Left, Right */
        _printf( 5, row+2, COLOR_TEAL, "%c %c", 
                    (pad.new & PAD_LEFT  ? 'L' : ' '), 
                    (pad.new & PAD_RIGHT ? 'R' : ' '));
        /* Down */
        _printf( 5, row+3, COLOR_TEAL, " %c", 
                    (pad.new & PAD_DOWN ? 'D' : ' '));



        /* L,R */
        _printf(20, row+1, COLOR_TEAL, "%c   %c", 
                    (pad.new & PAD_LBUTTON ? 'L' : ' '), 
                    (pad.new & PAD_RBUTTON ? 'R' : ' '));
        /* X,Y,Z */
        _printf(20, row+2, COLOR_TEAL, "%c %c %c", 
                    (pad.new & PAD_X ? 'X' : ' '), 
                    (pad.new & PAD_Y ? 'Y' : ' '), 
                    (pad.new & PAD_Z ? 'Z' : ' '));
        /* A,B,C */
        _printf(20, row+3, COLOR_TEAL, "%c %c %c", 
                    (pad.new & PAD_A ? 'A' : ' '), 
                    (pad.new & PAD_B ? 'B' : ' '), 
                    (pad.new & PAD_C ? 'C' : ' '));

        /* Start */
        _printf(10, row+4, COLOR_TEAL, "%s", (pad.new & PAD_START ? "START" : "     "));

    }
}





void test_timer(void)
{
    /* Timer tayst STT */
    unsigned char cntr = 0;
    _printf(2, row++, COLOR_TEAL, "TAYST START");
    while(1)
    {
//#define TEST1
//#define TEST2
#define TEST3

#ifdef TEST1
        /* SatCom: if needed, transfer data. */
        sc_check();

        /* Wait for V-Blank. */
        wait_vblank_out();
        wait_vblank_in();


        sc_tmr_start();
        int pouet = rand();
        _printf(2, row+0, COLOR_TEAL, "-> %02X, T=%7d , P%7d", cntr++, sc_tmr_lap_usec(), pouet);
        pouet+=rand();
        _printf(2, row+1, COLOR_TEAL, "-> %02X, T=%7d , P%7d", cntr++, sc_tmr_lap_usec(), pouet);
        pouet+=rand();
        _printf(2, row+2, COLOR_TEAL, "-> %02X, T=%7d , P%7d", cntr++, sc_tmr_lap_usec(), pouet);
#endif

#ifdef TEST2
        /* Wait for V-Blank. */
        sc_tmr_start();
        wait_vblank_out();
        wait_vblank_in();
        unsigned short delay = sc_tmr_lap_usec();
        _printf(2, row+0, COLOR_TEAL, "-> %02X, T=%7d", cntr++, delay);
#endif

#ifdef TEST3
        sc_tmr_start();
        sc_check();
        unsigned short delay = sc_tmr_lap_usec();
        _printf(2, row+0, COLOR_TEAL, "=> %02X, T=%7d", cntr++, delay);

        /* Wait for V-Blank. */
        wait_vblank_out();
        wait_vblank_in();
#endif
    }
    /* Timer tayst END */
}




typedef struct fread_callback_t
{
    unsigned char row; /* Row where to write progress. */
} fread_callback_t;
fread_callback_t _fread_callback_data;
void fread_callback(unsigned long data_transfer, unsigned long data_total, void* callback_data)
{
    char txt[256];
    fread_callback_t* dat = (fread_callback_t*)callback_data;

    sprintf(txt, "Loading data = %3d / 100 ...    ", (int)((100*data_transfer)/data_total));
    scd_logout("loading = >%s<", txt); scd_logflush();
    _printf(2, dat->row, COLOR_SILVER, txt);
}


void test_cdrom(void)
{
    /* CD-ROM test. */
    _printf(2, row, COLOR_SILVER, "Start test ...                  ");
    scd_logout("Start ...", 0); scd_logflush();
    sc_file_t* fh1 = NULL;
    sc_file_t* fh2 = NULL;
    sc_file_t* fh3 = NULL;
    unsigned long ret;

    _printf(2, row, COLOR_SILVER, "Start fopen ...                 ");
    scd_logout("fopen ...", 0); // scd_logflush();
    fh1 = sc_fopen(SC_FILE_CDROM, "./FOLDER1/pouet\\file.txt");
    scd_logout("handle1 = %d", fh1); // scd_logflush();
#ifndef SC_RELEASE
    unsigned long s1 = sc_fsize(fh1); // scd_logflush();
#endif
    memset((void*)test_buffer, '\0', TEST_BUFFER_SIZE);
    ret = sc_fread(fh1, 0/*offset*/, TEST_BUFFER_SIZE-1/*size*/, (void*)test_buffer/*test_buffer*/);
    scd_logout("READ1 = %d bytes, >%s<", ret, test_buffer); // scd_logflush();

    fh2 = sc_fopen(SC_FILE_CDROM, "FOLDER2/test.txt");
    scd_logout("handle2 = %d", fh2); // scd_logflush();
#ifndef SC_RELEASE
    unsigned long s2 = sc_fsize(fh2); // scd_logflush();
#endif
    memset((void*)test_buffer, '\0', TEST_BUFFER_SIZE);
    ret = sc_fread(fh2, 2040/*offset*/, 6000/*size*/, (void*)test_buffer/*test_buffer*/);
    scd_logout("READ2 = %d bytes, >%s<", ret, test_buffer); // scd_logflush();
    sc_fclose(fh2);
    sc_fclose(fh1);

    fh3 = sc_fopen(SC_FILE_CDROM, "abs.txt");
    scd_logout("handle3 = %d", fh3); // scd_logflush();
    unsigned long s3 = sc_fsize(fh3); // scd_logflush();
    memset((void*)test_buffer, '\0', TEST_BUFFER_SIZE);
    ret = sc_fread(fh3, 0/*offset*/, TEST_BUFFER_SIZE-1/*size*/, (void*)test_buffer/*test_buffer*/);
    scd_logout("READ3 = %d bytes, >%s<", ret, test_buffer); // scd_logflush();
    sc_fclose(fh3);

    scd_logout("size1 = %d, size2 = %d, size3 = %d", s1, s2, s3); scd_logflush();


    fh3 = sc_fopen(SC_FILE_CDROM, "0.bin");
    scd_logout("handle4 = %d", fh3); // scd_logflush();
    s3 = sc_fsize(fh3); // scd_logflush();
    memset((void*)test_buffer, '\0', TEST_BUFFER_SIZE);
    _fread_callback_data.row = row;
    ret = sc_callback_fread(fh3, 0/*offset*/, TEST_BUFFER_SIZE-1/*size*/, (void*)test_buffer/*test_buffer*/, 
                            10/*callback_count*/, 
                            fread_callback/*callback_fct*/, (void*)(&_fread_callback_data)/*callback_data*/);
    scd_logout("READ4 = %d bytes", ret); // scd_logflush();
    sc_fclose(fh3);

    _printf(2, row, COLOR_SILVER, "Read test ended !               ");

    /* Test CD-ROM: END */
}



void test_gouraud(void)
{
#if 0
    /* Move objects */
    for(i=0;i<4;i++)
        update_pos(&foo[i]);

    /* Make new list */
    list_clear();
    list_set_system_clip(320, 224);
    list_set_user_clip(0, 0, 320, 224);
    list_set_local_coords(0, 0);

    MESH(0);
    list_polyline(319,0,319,223,0,223,0,0,COLOR(31,0,0));

    MESH(1);
    list_polygon( \
        foo[0].c[0].x,foo[0].c[0].y, \
        foo[0].c[1].x,foo[0].c[1].y, \
        foo[0].c[2].x,foo[0].c[2].y, \
        foo[0].c[3].x,foo[0].c[3].y, \
        COLOR(15,15,15), 0x60000);

    MESH(0);
    list_polyline( \
        foo[1].c[0].x,foo[1].c[0].y, \
        foo[1].c[1].x,foo[1].c[1].y, \
        foo[1].c[2].x,foo[1].c[2].y, \
        foo[1].c[3].x,foo[1].c[3].y, \
        COLOR(31,0,31));

    list_distorted_sprite( \
        foo[2].c[0].x,foo[2].c[0].y, \
        foo[2].c[1].x,foo[2].c[1].y, \
        foo[2].c[2].x,foo[2].c[2].y, \
        foo[2].c[3].x,foo[2].c[3].y, \
        (0x40000)>>3, TX_W, TX_H);

    list_end();
#endif
}

