// splatisa.cpp: implementation of the splatisa class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "splatisa.h"

void splatisa()
{
	bio = 0;
	bio = board_scan();   /* scan for board id and set bid */
	if (bio != 0)
	{
		reg_default();
		reg_read();           /* read the regs */
	}
}

void csplatisa()
{
	reg_default();
	reset_board();
}

/*-------------------------------------------------------------
 board_scan
	search the PC I/O space for the S.P.L.A.T ISA card.
	return value    I/O location
		             or
			0x01 for board not found
 ---------------------------------------------------------------
*/

USIGN16  board_scan()
{
unsigned short i;
	for(i=0x000;i<=0x3e0;i=i+0x010)
	  if(INP(i) == 0x853A)		// board id is == to 0x853A
		return(i);
	return(0x3e0);
}

/*------------------------------------------------------
reg_read
		read all SPLAT registers and store in variable
---------------------------------------------------------
*/

void  reg_read()
{
  regs.boardid  = INP(bio+0x00);     /* board id*/
  regs.pmt_trs  = INP(bio+PMTTRS);    /* pmt integration threshold*/
  regs.pmt_wnd  = INP(bio+PMTWND);    /* pmt integration window */
  regs.pmt_sat  = INP(bio+PMTSAT);    /* pmt stauration limit */
  regs.chn_trs  = INP(bio+CHNTRS );   /* channeltron integration threshold*/
  regs.chn_wnd  = INP(bio+CHNWND );   /* channeltron integration window */
  regs.chn_enc  = INP(bio+CHNENC);    /* get channeltron output */
  regs.ltimeout = INP(bio+LTIMEOUT);  /* wait time after laser fired */
  regs.t1_wait  = INP(bio+T1WAIT);    /* WAIT TIME AFTER PMT1 TRIGGERED AND START OF PMT 2 */
  regs.t2_wait  = INP(bio+T2WAIT);    /* TOTAL WAIT TIME FOR PMT 2 TO TRIGGER BEFORE RESET  */
  regs.t3_wait  = INP(bio+T3WAIT);    /* WAIT TO TRIGGER LASER AFTER CHANNELTRON TRIGGERED  */
  regs.mult_sum = INP(bio+MULTSUM);   /* ---NOT USED--- */
  regs.errc     = INP(bio+ERRC);      /*ERROR CORRECTION FOR PMT1 TO PMT2 TO LASER */
  regs.pmt_del  = INP(bio+PMTDEL);    /* PMT TRIGGER DELAY*/
  regs.star	    = INP(bio+STAR);      /*user registers*/
  regs.ptrvlt   = INP(bio+PTRVLT);    /*partical travel time from t1 to t2 */
}


/*-----------------------------------------------
reg_bwrite()

	reg block write
-------------------------------------------------*/
void  reg_bwrite()
{
  OUTP(bio+PMTTRS, regs.pmt_trs);       /* pmt integration threshold*/
  OUTP(bio+PMTWND, regs.pmt_wnd);         /* pmt integration window */
  OUTP(bio+PMTSAT, regs.pmt_sat);          /* pmt stauration limit */
  OUTP(bio+CHNTRS, regs.chn_trs);       /* channeltron integration threshold*/
  OUTP(bio+CHNWND, regs.chn_wnd);       /* channeltron integration window */
  OUTP(bio+CHNENC, regs.chn_enc);       /* --NOT USED--  */
  OUTP(bio+LTIMEOUT, regs.ltimeout);       /* wait time after laser fired */
  OUTP(bio+T1WAIT, regs.t1_wait);         /* WAIT TIME AFTER PMT1 TRIGGERED AND START OF PMT 2 */
  OUTP(bio+T2WAIT, regs.t2_wait);       /* TOTAL WAIT TIME FOR PMT 2 TO TRIGGER BEFORE RESET  */
  OUTP(bio+T3WAIT,  regs.t3_wait);       /* WAIT TO TRIGGER LASER AFTER CHANNELTRON TRIGGERED  */
  OUTP(bio+MULTSUM, regs.mult_sum);       /* --NOT USED-- */
  OUTP(bio+ERRC,    regs.errc);       /*ERROR CORRECTION FOR PMT1 TO PMT2 TO LASER */
  OUTP(bio+PMTDEL,  regs.pmt_del);       /*PMT LASER TRGGEER DELAY*/
  OUTP(bio+STAR,    regs.star);       /*user registers*/
  OUTP(bio+PTRVLT,  regs.ptrvlt);       /*partical travel time from t1 to t2 */
}
/*-----------------------------------------------
reg_default

	load registers with default values
-------------------------------------------------*/
void  reg_default()
{
  OUTP(bio+PMTTRS,  0X1655);       /* pmt integration threshold*/
  OUTP(bio+PMTWND,  0Xfe);         /* pmt integration window */
  OUTP(bio+PMTSAT,  0X2);          /* pmt stauration limit */
  OUTP(bio+CHNTRS,  0X5);       /* channeltron integration threshold*/
  OUTP(bio+CHNWND,  0Xfe);       /* channeltron integration window */
//  OUTP(bio+CHNENC,  0X10);       /* --NOT USED--  */
  OUTP(bio+LTIMEOUT,0X1234);       /* wait time after laser fired */
  OUTP(bio+T1WAIT,  0X12);         /* WAIT TIME AFTER PMT1 TRIGGERED AND START OF PMT 2 */
  OUTP(bio+T2WAIT,  0Xfffe);       /* TOTAL WAIT TIME FOR PMT 2 TO TRIGGER BEFORE RESET  */
  OUTP(bio+T3WAIT,  0X1234);       /* WAIT TO TRIGGER LASER AFTER CHANNELTRON TRIGGERED  */
  OUTP(bio+MULTSUM, 0X1234);       /* --NOT USED-- */
  OUTP(bio+ERRC,    0X1000);       /*ERROR CORRECTION FOR PMT1 TO PMT2 TO LASER */
  OUTP(bio+PMTDEL,  0X0001);       /*PMT LASER TRGGEER DELAY*/
  OUTP(bio+STAR,    0X0000);       /*user registers*/
  OUTP(bio+PTRVLT,  0X1234);       /*partical travel time from t1 to t2 */
}


/*-----------------------------------------------
reset board()

		resets the boards registers	
-------------------------------------------------*/

void     reset_board()
{
  int i;
  		regs.star = INP(bio+STAR);
		while ((regs.star & 0x0003) != 0)
		{
		 regs.star = INP(bio+STAR);
		 OUTP(bio+STAR,0X0000);
		 OUTP(bio+STAR,0x00f0);
		 OUTP(bio+STAR,0X0000);
		}

}

/*-----------------------------------------------
single trigger()

	 generates a single run event  (one laser fire)

-------------------------------------------------*/

void     single_trigger()
{
	USIGN16 status;

		 status = INP(bio+STAR);
		 OUTP(bio+STAR,status && 0Xfffc);
		 OUTP(bio+STAR,status || 0x0001);
}

/*-----------------------------------------------
cont trigger

	generates a continueous laser fires. 
	register L TIMEOUT controls wait time between each event.
-------------------------------------------------*/

void     cont_trigger()
{

	USIGN16 status;

		 status = INP(bio+STAR);
		 OUTP(bio+STAR,status && 0Xfffc);
		 OUTP(bio+STAR,status || 0x0003);
		 OUTP(bio+STAR,0x3);

		regs.star = INP(bio+STAR);
		while ((regs.star & 0x0001) == 0)
		{
		 regs.star = INP(bio+STAR);
		 OUTP(bio+STAR,status && 0Xfffc);
		 OUTP(bio+STAR,status || 0x0003);
		 OUTP(bio+STAR,0x3);

		}

}

/*-----------------------------------------------
board status

		read the board status register.
-------------------------------------------------*/
USIGN16  board_status()
{
		return INP(bio+STAR);
}

/*-----------------------------------------------
check trigger()

	   poll this function to see if trigger was generated
-------------------------------------------------*/
bool     check_trigger()
{ 
	bool	trig;
	USIGN16 val;
		trig = false;
		val =   regs.star = INP(bio+STAR);
		if ((val & 0xf000) != 0) 
			trig = true;
		OUTP(bio+0x00,0x00);       /* reset the trigger reg*/
	return trig;
}

/*-----------------------------------------------
read trvl()

	data stored from channeltron or time from PMT1 to PMT2
-------------------------------------------------*/

USIGN16  read_trvl()
{
	 regs.ptrvlt = INP(bio+PTRVLT);
	return regs.ptrvlt;
}


/*-----------------------------------------------
set reg()

	SET ANY ON BOARD REGISTERS.
-------------------------------------------------*/
void     set_reg(USIGN16 addr,USIGN16 value)
{
		if ((addr >= 00) && (addr <= 0x1e))
		     OUTP(bio+addr,value);
}

void save_data(FILE *FP)
{

  regs.chn_enc  = INP(bio+CHNENC);    /* get channeltron output */
  regs.ptrvlt   = INP(bio+PTRVLT);    /*partical travel time from t1 to t2 */
  fprintf(FP, "%d\t%d\t%x\n",regs.ptrvlt*50,regs.chn_enc,regs.star); 
}
