/*FILE HEADER**************************************************************
 *
 *  File name		: rtgs_env.c
 *  RCS Revision Number	: $Revision: 1.1 $
 *  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 the set of functions which can be used by
 *	application programs to drive a DY 4 Graphics Controller Card
 *	equipped with Real-Time Graphics System - Command Processor
 *	firmware.
 *  History		:
 *	1991 Nov 15	Jeff Sanderson
 *	    	Release Rev A.
 *	1994 Oct 10	Sylvain Coudray
 *		Taking out all statements relative to CIT that is
 *		not provided with video card.
 *
 *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"


/*- Macros ------------------------------------------------------------------*/

/* macros to access the interrupt control register of the SVME-770           */

#define HSTCLTL		*((uint16 *)( VME_ADDR( 0xC00000E0 )))
#define CLR_INT_MASK	( 0xFF0F )
#define CLR_INT()	HSTCLTL &= CLR_INT_MASK;


/*- Local function prototypes -----------------------------------------------*/

static void Isr_general( int vect );
static void Isr_bus_err( int vect );
static void Isr_error( int vect );
static void Isr_raise_int( int vect );
static void Isr_buff_flip( int vect );
static void Isr_malloc( int vect );
static void Isr_inq_text( int vect );
static void Isr_inq_pixs( int vect );
static void Isr_inq_pt( int vect );
static void Isr_inq_addr( int vect );
static void Isr_tick( int vect );
static void Isr_new_fifo( int vect );
static void Isr_get_pixl( int vect );


/*- Global variables --------------------------------------------------------*/

css_jump_table_struct   *W_Css_jump_table; 
uint32                  W_psize;

uint32                  W_interrupt_flag;
uint32                  W_flip_flag;
uint32                  W_malloc_flag;
uint32                  W_inq_text_flag;
uint32                  W_inq_pixs_flag;
uint32                  W_inq_pt_flag;
uint32                  W_inq_addr_flag;
uint32                  W_tick_flag;
uint32                  W_new_fifo_flag;
uint32                  W_get_pixl_flag;
int                     W_vector_mask;

css_mark_struct         *Mark_stack;
css_mark_struct         buff_general;
css_mark_struct         buff_bus_err;
css_mark_struct         buff_error;
css_mark_struct         buff_raise_int;
css_mark_struct         buff_buff_flip;
css_mark_struct         buff_malloc;
css_mark_struct         buff_inq_text;
css_mark_struct         buff_inq_pixs;
css_mark_struct         buff_inq_pt;
css_mark_struct         buff_inq_addr;
css_mark_struct         buff_tick;
css_mark_struct         buff_new_fifo;
css_mark_struct         buff_get_pixl;


/*---------------------------------------------------------------------------*/
/* This function initializes the local environment on the host for RTGS      */
/*---------------------------------------------------------------------------*/

void Init( void )
{

/*MBARI-IFREMER ### This part of software is not provided with our card
 *   es_cit_entry_struct *cit_entry;
 *
 *     * 
 *     * Initialize global pointer to Card Specific Services (CSS) 
 *     * jump table 
 *    cit_entry = Get_cit_entry( ES_CSS_PID, ES_HOST_CARD_SID );
 *    W_Css_jump_table = (css_jump_table_struct *)cit_entry->jump_table; 
 *ENDMBARI-IFREMER*/

    /*
     * Initialize the GCC
     */

/*MBARI-IFREMER ### our address ### */
    Set_gcc_base_address(0x70000000);
/*ENDMBARI-IFREMER*/
    Set_current_fifo( DEFAULT_FIFO );
    Take_gcc_out_of_bi_mode();
    Initialize_graphics();
    Initialize_video( VIDEO_MODE ); /* VIDEO_MODE is defined in video.h */


    /* 
     * Initialize interrupt environment 
     */

/*MBARI-IFREMER ### This part of software is not provided with our card
 *   Mark_stack = 0;
 *   Set_mark_stack( &Mark_stack );
 *   Init_mark_evt(( uint32 * )( 0 ));
 *ENDMBARI-IFREMER*/


    /*
     * Install all the interrupt handlers 
     */

/*MBARI-IFREMER ### This part of software is not provided with our card
 *   Call_mark( CSS_GENERAL_MARK, &buff_general, Isr_general );
 *   Call_mark( CSS_BUS_ERROR_MARK, &buff_bus_err, Isr_bus_err );
 *ENDMBARI-IFREMER*/

    Call_mark( VECT_ERROR, &buff_error, Isr_error );
    Call_mark( VECT_RAISE_INTERRUPT, &buff_raise_int, Isr_raise_int );
    Call_mark( VECT_BUFFER_FLIP, &buff_buff_flip, Isr_buff_flip );
    Call_mark( VECT_MALLOC, &buff_malloc, Isr_malloc );
    Call_mark( VECT_INQUIRE_TEXT_EXTENT, &buff_inq_text, Isr_inq_text );
    Call_mark( VECT_INQUIRE_PIXEL_SIZE, &buff_inq_pixs, Isr_inq_pixs );
    Call_mark( VECT_COMPARE_CURS_CLIPRECT, &buff_inq_pt, Isr_inq_pt );
    Call_mark( VECT_GET_PIXEL_ADDRESS, &buff_inq_addr, Isr_inq_addr );
    Call_mark( VECT_FRAME_TICK, &buff_tick, Isr_tick );
    Call_mark( VECT_CREATE_FIFO, &buff_new_fifo, Isr_new_fifo );
    Call_mark( VECT_GET_PIXEL, &buff_get_pixl, Isr_get_pixl );


    /*
     * Select which sources for interrupts
     */
#ifdef FULL_INTS
    W_vector_mask = ( CONDITION_ERROR | 
                      CONDITION_RAISE_INTERRUPT | 
                      CONDITION_BUFFER_FLIP | 
                      CONDITION_MALLOC | 
                      CONDITION_INQUIRE_TEXT_EXTENT | 
                      CONDITION_INQUIRE_PIXEL_SIZE | 
                      CONDITION_COMPARE_CURS_CLIPRECT | 
                      CONDITION_GET_PIXEL_ADDRESS | 
                      CONDITION_FRAME_TICK | 
                      CONDITION_CREATE_FIFO | 
                      CONDITION_GET_PIXEL );
#else
    W_vector_mask = ( CONDITION_ERROR | 
                      CONDITION_RAISE_INTERRUPT | 
                      CONDITION_BUFFER_FLIP | 
                      CONDITION_MALLOC | 
                      CONDITION_INQUIRE_TEXT_EXTENT | 
                      CONDITION_INQUIRE_PIXEL_SIZE | 
                      CONDITION_COMPARE_CURS_CLIPRECT | 
                      CONDITION_GET_PIXEL_ADDRESS | 
                      CONDITION_CREATE_FIFO | 
                      CONDITION_GET_PIXEL );
#endif
    Set_interrupt_mode( ( int )GCC_VECT, ( int )GCC_LEVEL, W_vector_mask );


    /*
     * Enable the interrupt level on the host
     */
    Enable_vme_ints( ( int )GCC_LEVEL, TRUE );
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for unexpected exceptions                               */
/*---------------------------------------------------------------------------*/

static void Isr_general( int vect )
{
    Printf( "Unexpected exception: vector 0x%02x\n", vect );
    Key_wait();
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for bus errors                                          */
/*---------------------------------------------------------------------------*/

static void Isr_bus_err( int vect )
{
    Printf( "Bus error, vector 0x%02x\n", vect );
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for RTGS errors                                         */
/*---------------------------------------------------------------------------*/

static void Isr_error( int vect )
{
    uint32 err;

    CLR_INT();

    err = Get_error_code();

    /*
     * Print out the funtion code and the error code
     */
    Printf( " %s, %s\n", gcc_command_strings[ (( err >> 16 ) & 0xFFFF ) ],
       gcc_error_strings[ ( err & 0xFFFF ) ] );

}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Raise_interrupt()                                   */
/*---------------------------------------------------------------------------*/

static void Isr_raise_int( int vect )
{
    CLR_INT();
    W_interrupt_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Flip_buffers()                                      */
/*---------------------------------------------------------------------------*/

static void Isr_buff_flip( int vect )
{
    CLR_INT();
    W_flip_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Gcc_malloc()                                        */
/*---------------------------------------------------------------------------*/

static void Isr_malloc( int vect )
{
    CLR_INT();
    W_malloc_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Inquire_text_extent()                               */
/*---------------------------------------------------------------------------*/

static void Isr_inq_text( int vect )
{
    CLR_INT();
    W_inq_text_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Inquire_pixel_size()                                */
/*---------------------------------------------------------------------------*/

static void Isr_inq_pixs( int vect )
{
    CLR_INT();
    W_inq_pixs_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Compare_point_to_cliprect()                         */
/*---------------------------------------------------------------------------*/

static void Isr_inq_pt( int vect )
{
    CLR_INT();
    W_inq_pt_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Inquire_pixel_address()                             */
/*---------------------------------------------------------------------------*/

static void Isr_inq_addr( int vect )
{
    CLR_INT();
    W_inq_addr_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Tick interupt (vertical retrace, 1/60th of a second)*/
/*---------------------------------------------------------------------------*/

static void Isr_tick( int vect )
{
    CLR_INT();
    W_tick_flag++;
}

/*---------------------------------------------------------------------------*/
/* Interrupt hanlder for Create_fifo()                                       */
/*---------------------------------------------------------------------------*/

static void Isr_new_fifo( int vect )
{
    CLR_INT();
    W_new_fifo_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
/* Interrupt handler for Get_pixel()                                         */
/*---------------------------------------------------------------------------*/

static void Isr_get_pixl( int vect )
{
    CLR_INT();
    W_get_pixl_flag = TRUE;
}

/*---------------------------------------------------------------------------*/
