//
//    Copyright (c) 2004/2005 Thomas Fuchs / The Rockin'-B, www.rockin-b.de
//
//    Permission is granted to anyone to use this software for any purpose
//    and to redistribute it freely, subject to the following restrictions:
//
//    1.  The origin of this software must not be misrepresented. You must not
//        claim that you wrote the original software. If you use this software
//        in a product, an acknowledgment in the product documentation would be
//        appreciated but is not required.
//
//    2.  ANY COMMERCIAL USE IS PROHIBITED. This includes that you must not use
//        this software in the production of a commercial product. Only an explicit
//        permission by the author can allow you to do that.
//
//    3.  Any modification applied to the software must be marked as such clearly.
//
//    4.  This license may not be removed or altered from any distribution.
//
//
//                  NO WARRANTY
//
//    THERE IS NO WARRANTY FOR THE PROGRAM.
//    THE PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
//    EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
//    THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.
//    THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
//    SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
//    SERVICING, REPAIR OR CORRECTION.
//
//    IN NO EVENT WILL THE COPYRIGHT HOLDER BE LIABLE TO YOU FOR DAMAGES,
//    INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
//    OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
//    TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
//    YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
//    PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
//    POSSIBILITY OF SUCH DAMAGES.
//



#include <SGL.H>

/* Debug stuff. */
#include "debug.h"







///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////




/*
 * ss_main
 */
void ss_main(void)
{
    /* SatCom: initialization. */
    sc_init();


    /* System and VDP 1 */
    slInitSystem(TV_320x240, 0, 1);
    /* VDP 2 */
    slTVOff();
    /*
     * the address for background color in VRAM is taken from various examples,
     * don't know why
     * !!! beware, 1st try the bgcol was the only thing to see on real Saturn
     * what exactly is the back_col_adr? maybe the color at this adress is
     * interpreted as background color and wheereever such color occurs,
     * it's replaced by the given one
     * !! all i see is the background color....
     */
    slBack1ColSet((void *)BACK_CRAM, CD_Black);


    /*
     * the debug text lib take the NBG0 scroll <- the only one with 24bits color depth
     * priority = 0 -> scroll not displayed
     * take farest away
     */
    slPriorityNbg0(6);
    slPriorityNbg1(5);
    //    slPrioritySpr0-7(0);

    /*
     * we want bitmap scrolls
     * only NBG0 and NBG1 can handle bitmap mode
     * color modes:
     *      palettet:           COL_TYPE_16, COL_TYPE_256, COL_TYPE_2048
     *      rgb:                COL_TYPE_32768, COL_TYPE_1M
     * bitmap sizes(BM_HxV):    BM_512x256, BM_512x512, BM_1024x256, BM_1024x512
     */
    slBitMapNbg1(COL_TYPE_32768, BM_512x256, (void *)VDP2_VRAM_A0);

    //    memset((void *)VDP2_VRAM_A0, CD_Black, 256*512*2);

    /*
     * monitor position in scroll coordinate space
     * sets the position of monitors topleft corner(fixed format)
     *    slScrPosNbg1(toFIXED(-33.680/2.0), toFIXED(0.0));
     */
    slScrPosNbg1(toFIXED(0.0), toFIXED(0.0));

    /*
     * reduction setting: only NGB0 and NBG1 are able to RBG, too)
     * modes:   ZOOM_1, ZOOM_HALF, ZOOM_QUATER      each untill x256
     */
    slZoomModeNbg1(ZOOM_1);

    /*
     * zoom
     * fixed point reziprocal of scale ratio in x and y direction
     */
    slZoomNbg1(toFIXED(1.0), toFIXED(1.0));

    /*
     * registeres scrolls
     * call only after all scroll function settings
     * returns 0 or -1(ERROR)
     */
    slScrAutoDisp(NBG0ON | NBG1ON);

    slTVOn();




    LogOut("Start", 0);
    LogOut("Z: rand number", 0);

    Uint16 push, data;


    /* init srand. */
    {
        SmpcDateTime *time = &(Smpc_Status->rtc);
        unsigned long seed;
        seed = slDec2Hex((Uint32)time->second)
             + 60*(slDec2Hex((Uint32)time->minute)
             + 60*(slDec2Hex((Uint32)time->hour)
             + 24*(slDec2Hex((Uint32)time->date)
             + 31*(time->month & 0x0f
             + 12* slDec2Hex((Uint32)time->year)))));
        LogOut("Seed = %08X", seed);
        srand(seed);
    }


    /* start multitester */
    unsigned char cntr = 0;
    while(1)
    {
        push = Smpc_Peripheral[0].push;
        data = Smpc_Peripheral[0].data;

        if( (data & PER_DGT_TX) == 0 )
        { /* Log to SatCom debugger only. */
            scd_logout("cntr = %d", cntr);
        }

        if( (push & PER_DGT_TZ) == 0 )
        { /* Log to screen, and to SatCom debugger too. */
            LogOut("rand = %d", rand()&0xFF);
        }

        slSynch();
        cntr++;
    }



    /* return to demo loader
     * or activate multiplayer
     */
    SYS_Exit(0);
}

