/*FILE HEADER*************************************************************
 *
 *  File name		: mainvideo.c
 *  RCS Revision Number	: 
 *  Last Modified	: oct-10-1994 by s.coudray MBARI-IFREMER
 *  CSCI name		: Real-Time Graphics System-Applications Interface
 *  CSC name		: All functions
 *  Notes:
 *      This file contains code used to debug and test the SVME-770 RTGS.
 *      This code is provided as an example of how to call the RTGS 
 *      functions from a host application.
 *  History		:
 *      Created oct-10-1994 by s.coudray MBARI-IFREMER
 *
 *END FILE HEADER*********************************************************
 */

#include "dy4std.h"
#include "es.h"
#include "css.h"

#include "rtgs_ai.h"
#include "video.h"
#include "colours.h"

#include "test.h"


/* **********************************************************************
 * main function
 * **********************************************************************
 */


void test_video( void )
{

    register int32   i;
    uint32 i_pixl, i_line, i_incr, j_incr, i_gap;

    /*
     * Initialize the GCC
     */

    Set_gcc_base_address(0x70000000);
    Set_current_fifo( DEFAULT_FIFO );
    Take_gcc_out_of_bi_mode();
    wait ( 1);

    /*
     * Initialize graphic and video parameters
     */

    Initialize_graphics();
    Initialize_video( VIDEO_MODE );
    Clear_screen ( BLACK, 0, 0);
    My_custom_video();

    /*
     * Overlay
     */

    My_string();
    My_dots_rectangle();
    test_cursor();

/*
    test_loadfont();
    My_move_video();
    My_grid();
    My_lines_vertical_2 ();
    My_phase_alignement();
    printf ( "\n i_line = ");
    scanf ( "%d", &i_line);
    My_dots_horizontal ( i_line);
    My_dots_vertical ( i_line);
*/

}

/* **********************************************************************
 * debug function
 * **********************************************************************
 */

int Print_video_mode ( video_params_t *gcc_vid)
{ 
    printf ( "\n");
    printf ( "screenwide     = %d \n", gcc_vid->screenwide);
    printf ( "screenhigh     = %d \n", gcc_vid->screenhigh);
    printf ( "bits-per-pixel = %d \n", gcc_vid->psize);
    printf ( "scrn_pitch     = %d \n", gcc_vid->scrn_pitch);

    printf ( "\nTMS34020 registers :\n");
    printf ( "vesync         = %d \n", gcc_vid->vesync);
    printf ( "hesync         = %d \n", gcc_vid->hesync);
    printf ( "veblnk         = %d \n", gcc_vid->veblnk);
    printf ( "heblnk         = %d \n", gcc_vid->heblnk);
    printf ( "vsblnk         = %d \n", gcc_vid->vsblnk);
    printf ( "hsblnk         = %d \n", gcc_vid->hsblnk);
    printf ( "vtotal         = %d \n", gcc_vid->vtotal);
    printf ( "htotal         = %d \n", gcc_vid->htotal);
    printf ( "dpyctl         = %d \n", gcc_vid->dpyctl);
    printf ( "dpyint         = %d \n", gcc_vid->dpyint);
    printf ( "config         = %d \n", gcc_vid->config);
    printf ( "heserr         = %d \n", gcc_vid->heserr);
    printf ( "dpymsk         = %d \n", gcc_vid->dpymsk);
    printf ( "setvcount      = %d \n", gcc_vid->setvcount);
    printf ( "sethcount      = %d \n", gcc_vid->sethcount);
    printf ( "scrn_offset    = %d \n", gcc_vid->scrn_offset);
    printf ( "pal_gen_ctrl_reg       = %X \n", gcc_vid->pal_gen_ctrl_reg);
    printf ( "pal_output_clk_sel_reg = %X \n", gcc_vid->pal_output_clk_sel_reg);
    printf ( "pal_input_clk_sel_reg  = %X \n", gcc_vid->pal_input_clk_sel_reg);
    printf ( "pal_mux_ctrl_reg_1     = %X \n", gcc_vid->pal_mux_ctrl_reg_1);
    printf ( "pal_mux_ctrl_reg_2     = %X \n", gcc_vid->pal_mux_ctrl_reg_2);

    printf ( "ctla = %X \n", gcc_vid->ctrl_reg_a);
    printf ( "ctlb = %X \n", gcc_vid->ctrl_reg_b);
    printf ( "ctlc = %X \n", gcc_vid->ctrl_reg_c);
    printf ( "clock_sel_reg  = %X \n", gcc_vid->clock_sel_reg);
    printf ( "pll_mult_reg   = %X \n", gcc_vid->pll_mult_reg);
    printf ( "pll_div_reg    = %X \n", gcc_vid->pll_div_reg);
    printf ( "phase_adj_reg  = %X \n", gcc_vid->phase_adj_reg);
    printf ( "max_buf_num    = %X \n", gcc_vid->max_buf_num);
}



/* ************************************************************************
 * wait function
 * ************************************************************************
 */

int wait ( int32 i_wait)
{ 
    register int32  i, j;

    for (i=1; i<=i_wait * 1000000; i++) {
	j = 1;
    }
    return;
}

/* ************************************************************************
 * testing screen definition with lines
 * ************************************************************************
 */

int My_lines_vertical ( void)
{
    register int32  i, j;

    Set_line_width ( 1);

    j = 1;
    for ( i=1; i<=105; i++) {      
      Set_foreground_colour ( RED);
      Draw_line ( j, 0, j, 478); 
      j = j+2;

      Set_foreground_colour ( BLUE);
      Draw_line ( j, 0, j, 478);
      j = j+2;

      Set_foreground_colour ( GREEN);
      Draw_line ( j, 0, j, 478);
      j = j+2;
    }
    return;
}

int My_lines_vertical_2 ( uint32 i_gap)
{
    register int32 i, j;
    uint32   i_gap_red, i_gap_green, i_max;

    Set_line_width ( 1);
    printf ( "\n i_gap_red = ");
    scanf ( "%d", &i_gap_red);
    printf ( "\n i_gap_green = ");
    scanf ( "%d", &i_gap_green);

    j = 1;
    i_max = 640 / (i_gap_red + i_gap_green);

    for ( i=1; i<=i_max; i++) {      
      Set_foreground_colour ( RED);
      Draw_line ( j, 1, j, 478); 
      j = j+i_gap_red;
      Set_foreground_colour ( GREEN);
      Draw_line ( j, 1, j, 478);
      j = j+i_gap_green;
    }
    return;
}

int My_grid ()
{
    register int32 i, j;
    uint32   i_gap_red, i_gap_green, i_max;
    Set_line_width ( 1);

    printf ( "\n i_gap_red = ");
    scanf ( "%d", &i_gap_red);
/*
    printf ( "\n i_gap_green = ");
    scanf ( "%d", &i_gap_green);
*/
    /* vertical lines */

    j = 1;
    i_max = 640 / i_gap_red;

    for ( i=1; i<=i_max; i++) {      
      Set_foreground_colour ( RED);
      Draw_line ( j, 1, j, 478); 
      j = j+i_gap_red;
    }

    /* horizontal lines */

    j = 2;
    i_max = 480 / i_gap_red;

    for ( i=1; i<=i_max; i++) {      
      Set_foreground_colour ( RED);
      Draw_line ( 1, j, 639, j); 
      j = j+i_gap_red;
    }

    return;
}

/* ************************************************************************
 * testing screen horizontal resolution with dots
 * ************************************************************************
 */

int My_dots_horizontal ( uint32 line)
{
    register int32  i, j;

    Set_line_width ( 1);

    j= 1;
    for ( i=0; i<=318; i++) {      
      Put_pixel ( j, line, RED); 
      j = j+1;
      Put_pixel ( j, line, GREEN);
      j = j+1;
    }
    return;
}


/* ************************************************************************
 * testing screen vertical resolution with dots
 * ************************************************************************
 */

int My_dots_vertical ( uint32 line)
{
    register int32  i, j;

    Set_line_width ( 1);

    j= 1;
    for ( i=0; i<=238; i++) {      
      Put_pixel ( line, j, RED); 
      j = j+1;
      Put_pixel ( line, j, GREEN);
      j = j+1;
    }
    return;
}

/* ************************************************************************
 * testing screen resolution with a rectangle
 * ************************************************************************
 */

int My_dots_rectangle ()
{
    register int32  i_line, j_column;

    i_line = 1;
    My_dots_horizontal ( i_line);
    j_column = 639;
    My_dots_vertical ( j_column);

    i_line = 478;
    My_dots_horizontal ( i_line);
    j_column = 0;
    My_dots_vertical ( j_column);

    return;
}

/* ************************************************************************
 * testing character display
 * ************************************************************************
 */

int My_string ()
{
    uint32 x, y, i_font;
    char *str = "example/exemple";
    char *dst;

    x = 530;
    y = 460;
    i_font = 0;
    Set_text_font ( i_font);
    Set_foreground_colour ( CYAN);

    dst = Gcc_malloc ( strlen(str) + 1, 0);
    Copy_string_to_gcc ( str, dst, strlen(str) + 1);
    Draw_text ( x, y, dst) ;

    return;
}


/* ************************************************************************
 * Alignement of overlay on video
 * ************************************************************************
 */

int My_move_video ()
{
    uint32 i_incr, j_incr;

    printf ( "\n i_incr = ");
    scanf ( "%d", &i_incr);
    Move_video_vertical ( i_incr);

    printf ( "\n j_incr = ");
    scanf ( "%d", &j_incr);
    Move_video_horizontal ( j_incr);
}

/* ************************************************************************
 * Phase alignement of overlay on video
 * ************************************************************************
 */

int My_phase_alignement ()
{
    uint32 i_incr;

    printf ( "\n i_incr = ");
    scanf ( "%d", &i_incr);
    Set_phase_alignment ( i_incr);
}


/* ************************************************************************
 * adjusting video parameters to align ovelay and input source
 * ************************************************************************
 */

int My_custom_video ( void)
{

    uint32 i;
    video_params_t *gcc_vid;

    gcc_vid = (video_params_t *)Gcc_malloc(sizeof(video_params_t), 0);
    Get_video_mode( gcc_vid );
    Print_video_mode( gcc_vid); 

    for (i=1; i<=100; i++) {
      gcc_vid->screenwide = 640;
      gcc_vid->screenhigh = 487;
      gcc_vid->psize = 8;
      gcc_vid->scrn_pitch = 0x2000;
      gcc_vid->vesync = 11;
      gcc_vid->hesync = 13;
      gcc_vid->veblnk = 24;
      gcc_vid->heblnk = 26;
      gcc_vid->vsblnk = 264;
      gcc_vid->hsblnk = 186;
      gcc_vid->vtotal = 271;
      gcc_vid->htotal = 193;
      gcc_vid->dpyint = 263;
      gcc_vid->heserr = 82;
    }
    Custom_initialize_video( gcc_vid );
    wait (1);
    Get_video_mode( gcc_vid );
    Print_video_mode( gcc_vid); 
 
    return;
}

