/*************************************************************
  ROVINT.C  Rover Initialization and Interrupts
*************************************************************/

#include <tt7.h>
#include "rovint.h"
#include "rovsys.h"
#include "rovlib.h"
#include "rovfile.h"
#include <stdlib.h>
#include "tserial.h"


#ifdef AZTEC_C    /* Include if compiler is Aztec C */
#include <sim.h>
#include <pins.h>
#include <atod.h>
#include <tpu.h>
#include <timing.h>
#include <excepts.h>
#include <string.h>
#include <options.h>
#define IOPORT1   ((uspv) (0xE00000))
#define IOPORT2   ((uspv) (0xE00002))

#else
#include "tt7pclib.h"
#include "conio.h"
#endif



/* Global Variables */

int g_odometer_ticks, g_stbd_leadscrew_ticks, g_port_leadscrew_ticks;
int g_transpond_in;
ulong g_ping_time;

/************************************************
	Tattletale Mod. 7 Initialization
*************************************************/

void tt7init(void){
  short         result;
  short chan;

#ifdef AZTEC_C
  static ExcCFrame      framebuf1, framebuf2, framebuf3;

/*
** DEFINE CHIP SELECT FOR MEMORY MAPPING INTO 0xE00000
*/
   *CSBAR7 = 0xE000 | SZ2K; /* 2K @ E00000, E00002, E00004, E00006 */
   *CSOR7 =
		M_MDSYNC   &   CLR   /* 0 = async, 1 = synchronous      */
	|       M_BYTEU    &   SET   /* upper byte                      */
	|       M_BYTEL    &   SET   /* lower byte                      */
	|       M_CSWRITE  &   SET   /* write                           */
	|       M_CSREAD   &   SET   /* read                            */
	|       M_DSSTRB   &   SET   /* 0 = address, 1 = data strobe    */
	|       M_WAIT0              /* wait states                     */
	|       M_SPCS     &   SET   /* supervisor                      */
	|       M_SPCU     &   SET   /* user                            */
	|       M_AVEC     &   CLR   /* 0 = ext rupt, 1 = autovector    */
   ;


   *IOPORT1 = 0;   /* Clear Aux. I/O ports */
   *IOPORT2 = 0;

/************* Setup Port Pins, TPU Channels and Interrupts ***********/

   PConfOutp(D,3);
   PClear(D,3);
   PConfOutp(D,4);
   PClear(D,4);
   PConfOutp(D,5);
   PClear(D,5);
   PConfOutp(E,0);
   PSet(E,0);        /* Manually set these pins to make sure they're correct */
   PConfOutp(E,1);
   PSet(E,1);
   PConfOutp(E,2);
   PSet(E,2);
   PConfOutp(E,5);
   PClear(E,5);
   PConfOutp(E,6);
   PClear(E,6);
   PConfOutp(E,7);
   PClear(E,7);
   PConfOutp(F,1);
   PClear(F,1);
   PConfOutp(F,6);
   PSet(F,6);
   PConfOutp(F,7);
   PSet(F,7);

   TPUSetPin(0,0);
   TPUSetPin(1,0);
   TPUSetPin(2,1);
   for(chan=3;chan<=6;chan++)
     TPUGetPinES(chan, DetRising);
   TPUGetPin(7);                    /* Transpond-in */
   TPUSetPin(8,1);
   TPUGetPin(9);
   TPUSetPin(10,0);
   TPUSetPin(11,0);

  *CISR &= 0xC000;  /* Clear TPU0-13 interrupts */
  *CIER &= 0xC000;

/******** Setup Interrupt Handlers ****************/

   InstallHandler(odometer, TPU_INT_VECTOR + ODOMETER, OS_VECTOR_BASE,
	&framebuf1);

   InstallHandler(stbd_leadscrew, TPU_INT_VECTOR + STBD_LEADSCREW, OS_VECTOR_BASE,
	&framebuf2);

   InstallHandler(port_leadscrew, TPU_INT_VECTOR + PORT_LEADSCREW, OS_VECTOR_BASE,
	&framebuf3);

#endif

/********* OPEN THE TPU SERIAL DRIVERS *********************/

   if ((result = TSerOpen(13, INP, 100, 9600, 'N', 8, 1)) != tsOK)
     {
     printf("\nFailed TSerOpen %d", result);
     return;
     }

   if ((result = TSerOpen(12, OUTP, 100, 9600, 'N', 8, 1)) != tsOK)
     {
     printf("\nFailed TSerOpen %d", result);
     return;
     }

}  /* End of tt7init() */

/*************************************************
	Interrupt Handlers
*************************************************/

void odometer(void){

#ifdef AZTEC_C

/*  printf("\nOdometer Tick");   */
  g_odometer_ticks++;
  *CISR ^= (1 << ODOMETER);  /* Acknowledge interrupt */

#else
  g_odometer_ticks++;

#endif
}

/************************************************************/

void stbd_leadscrew(void){

#ifdef AZTEC_C

  g_stbd_leadscrew_ticks++;
  *CISR ^= (1 << STBD_LEADSCREW); /* Acknowledge interrupt */

#else
  g_stbd_leadscrew_ticks++;
  return;

#endif
}



/************************************************************/

void port_leadscrew(void){

#ifdef AZTEC_C
  g_port_leadscrew_ticks++;
  *CISR ^= (1 << PORT_LEADSCREW);  /* Acknowledge interrupt */
  return;

#else
  g_port_leadscrew_ticks++;
  return;

#endif
}

/************************************************************/

/*
void transpond_in(void){

  g_transpond_in = 1;

#ifdef AZTEC_C
  *CISR ^= (1 << TRANSPOND_IN);

  printf("\nPing in!");
#endif

  return;
}
*/

/******************Misc. Interrupt Functions***************/

void tpurupt(char chan, char on_off){

#ifdef AZTEC_C

  if(on_off == ON) *CIER += (1 << chan);
  else *CIER &= ~(1 << chan);

#endif
}

/************************************************************
*       Turbo-C Simulation Functions
************************************************************/
#ifdef __TURBOC__

void check_int(void){

  char c;

  if( kbhit() ){
    c = getch();
    switch(c){
      case 'o':
	odometer();
	break;
    }
  }
  return;
}

#endif
