/**
*	Title		ST-8000A Production Test Routines
*	Source		STA.C	RD	1-14-91
*
*	This program is used to control the prom based production test
*	routines for the ST-8000A product.
*
*	V1.1	3-8-91		This revision added the debug option repeated ram test
*				This also added commands to the modem board test requested by MT
*
*	V1.2   10-30-91		This revision adds several features requested by Mark T.
*				Adds an all segment on front panel test. This revision changed the
*				order of commands in the modem test sevction 3.4.5. This revision
*				adds a timeout check for a unit not turned on . The J2SW label was
*				dropped and the remcts and rsrts signals were fixed in the stat_test.
**/


#include "def.c"
#include <stdio.h>
#include <dos.h>
#include <bios.h>


/* command strings */

char	TC1[] = "P0 C02R1 M2000 S3000 B30 C01 M2000 S3000 B30\r";	/* test command 1 */
char	TC2[] = "P0 C02R1 B75 C01 B75\r";				/* test command 2 */

char	T0[] = "T0\r";							/* t0 command bit mode external */
char	T5[] = "T5\r";							/* t5 command bit mode internal 0dBm */
char	T6[] = "T6\r";							/* t6 command bit mode internal -20dBm */
char	T7[] = "T7\r";							/* t7 command bit mode internal -45dBm */

char	J0[] = "J0\r";							/* j0 command fsk mode */
char	J1[] = "J1\r";							/* j1 command mark only mode */
char	J2[] = "J2\r";							/* j2 command space only mode */

char	P1[] = "P1\r";							/*12 p1 command copy channel 1 settings to channel 2 */

char	C01[] = "C01\r";						/* c01 command channel 1 select */
char	C02[] = "C02\r";						/* c02 command channel 2 select */

char	A0[] = "A0\r";							/* a0 command amh off */

char	U1[] = "U1\r";							/* u1 command mute on */

char	W0[] = "W0\r";							/* w0 command sync mode */

char	D1[] = "D1\r";							/* d1 commnd div mode */

char	B30[] = "B30\r";
char	B75[] = "B75\r";
char	B86[] = "B86\r";
char	B114[] = "B114\r";
char	B145[] = "B145\r";
char	B170[] = "B170\r";
char	B202[] = "B202\r";
char	B253[] = "B253\r";						/* baud rate commands */
char	B327[] = "B327\r";
char	B337[] = "B337\r";
char 	B443[] = "B443\r";
char 	B540[] = "B540\r";
char	B625[] = "B625\r";
char	B689[] = "B689\r";
char	B760[] = "B760\r";
char	B854[] = "B854\r";
char	B952[] = "B952\r";
char	B1200[] = "B1200\r";

char	S2000[] = "S2000\r";
char	M3000[] = "M3000\r";

/* system variables and buffers */

char    in_buf [200];				/* text input buffer for text from cb */
char    buf [2000];				/* screen save buffer */

int	in_pntr = 0;				/* text input storage pointer */
int	out_pntr = 0;				/* text output pointer */
int	result_x = 19;				/* test result x display point */
int	result_y = 5;				/* test result y display point */
int	key_flag = 0;				/* key press stopped get_com flag */
int	disp_input = 0;				/* display get_com input flag 0 = ok -1 = no */

int	cur_test = 0;				/* modem board main test pointer */
int	sub_test = 0;				/* modem board sub test pointer */

struct	time	now;				/* current time structure */


/* main program */

main()
{
	union 	REGS in,out;

	int	x;	                        /* temporary character storage */
	char	ch;                     	/* keyboard input storage */
	int	stop_flag = 0;			/* stop flag  0 = go  1 = stop */

	clrscr();				/* clear the display */

	in.h.ah = 1;
	in.h.ch = 0x20;				/* turn the cursor off */
	in.h.cl = 0x00;
	int86 (0x10, &in, &out);

	setup_com();				/* set up com1 */

	while (stop_flag != 1)			/* this is the main program loop */
	{
	  clrscr();				/* clear the display screen */

	  gotoxy (HEADER_L,HEADER_T);
	  cprintf ("ST-8000A Test V");		/* display version number text */
	  outportb (COM1,ID_REQ);		/* send id request command */
	  delay (500);				/* delay to allow card to respond */

	  ch = inportb (COM1+LSR);		/* read port status */
	  if (ch & DATA_READY)			/* do the following if there is data */
	  {
	    ch = inportb (COM1);		/* read the version number */
	    gotoxy (VERSION_L,HEADER_T);
	    x = ch;
	    x >>= 4;				/* move upper 4 bits */
	    x = x & 0x0f;
	    cprintf ("%d",x);			/* display the version number */
	    cprintf (".");
	    x = ch & 0x0f;
	    cprintf ("%d",x);

	    gotoxy (VERSION_L+5,HEADER_T);
	    cprintf ("Checksum = ");		/* display checksum text */
	    outportb (COM1,CHECK_SUM);		/* send request for checksum command */
	    delay (800);			/* delay for a while */
	    ch = 0;
	    x = 0;
	    while (ch != DATA_READY)		/* wait for the response */
	    {
	      ch = inportb (COM1+LSR);		/* read the port status */
	      ch = ch & DATA_READY;		/* test for data ready */
	      x++;				/*12 increment timer */
	      if (x == 6000) ch = DATA_READY;	/*12 stop this loop on timeout */
	    }
	    ch = inportb (COM1);		/* read the checksum value */
	    cprintf ("%x",ch);			/* display the checksum value */
	  }
	  else					/* otherwise display the following on no response */
	  {
	    gotoxy (HEADER_L,HEADER_T);
	    cprintf ("Control Board Not Responding");
	  }

	  gotoxy (HEADER_L,HEADER_T+1);
	  cprintf ("STA.EXE  V1.2");		/* print c program revision level */

	  main_menu();				/* display the main test options */
	  ch = getch();				/* wait for a keypress */

	  switch (ch)				/* do one of the following based on the value of ch */
	  {
	    case '1':
	    cb_test();				/* go to do control board tests */
	    break;

	    case '2':
	    mb_test();				/* go to do modem board tests */
	    break;

	    case '3':
	    fp_test();				/* go to do front panel tests */
	    break;

	    case '4':				/* 11 */
	    debug_options();			/* 11 go to do debug options */
	    break;				/* 11 */

	    case '5':
	    stop_flag = 1;			/* set the stop flag to exit program */
	    break;
	  }
	}

	clrscr();				/* clear menu information */

}



/* This routine displays the main test options */

main_menu()
{
	gotoxy (MENU1_LEFT,MENU1_TOP);
	cprintf ("ST-8000A Test Options");	/* display the title line */
	gotoxy (MENU1_LEFT,MENU1_TOP+2);
	cprintf ("1...Control Board Tests");	/* display 1st test option */
	gotoxy (MENU1_LEFT,MENU1_TOP+3);
	cprintf ("2...Modem Board Tests");	/* display 2nd test option */
	gotoxy (MENU1_LEFT,MENU1_TOP+4);
	cprintf ("3...Front Panel Tests");	/* display 3rd test option */
	gotoxy (MENU1_LEFT,MENU1_TOP+5);
	cprintf ("4...Debug Options");		/* display 4th test option */
	gotoxy (MENU1_LEFT,MENU1_TOP+7);
	cprintf ("5...Exit Program");		/* displat the exit option */

}


/* This routine processes the control board tests */

cb_test()
{
	char	ch;				/* character storage */
	int	stop1 = 0;			/* stop flag  0 = go  1 = stop */

	clrscr();				/* clear the display */
	cmenu();				/* display the control board test options */

	while (stop1 != -1)			/* do the following until the stop is set */
	{
	  ch = getch();				/* wait for a test selection */
	  ch = toupper(ch);			/*12 convert input to upper case */
	  switch (ch)				/* do one of the following based on selection */
	  {
	    case '1':
	    ram_test();				/* go to do the ram test */
	    break;

	    case '2':
	    erom_test();			/* go to do eerom test */
	    break;

	    case '3':
	    switch_test();			/* go to do switch test */
	    break;

	    case '4':
	    synth_test();			/* go to do synthesizer test */
	    break;

	    case '5':
	    sanity_test();			/* go to do sanity timer test */
	    break;

	    case '6':
	    control_test();			/* go to do control signal test */
	    break;

	    case '7':
	    stat_test();			/* go to do status register test */
	    break;

	    case '8':
	    nmi_test();				/* go to do the nmi timer test */
	    break;

	    case '9':
	    stop1 = -1;				/* set the stop flag */
	    break;
	  }
	}

}


/* This routine displays the control board test options */

cmenu()
{
	gotoxy (CMENU_LEFT,CMENU_TOP);
	cprintf ("ST-8000A Control Board Tests");	/* display title line */
	gotoxy (CMENU_LEFT,CMENU_TOP+2);
	cprintf ("1...Ram Test");			/* display 1st test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+3);
	cprintf ("2...EEROM Test");			/* display 2nd test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+4);
	cprintf ("3...Switch Test");			/* display 3rd test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+5);
	cprintf ("4...Synthesizer Test");		/* display 4th test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+6);
	cprintf ("5...Sanity Timer Test");		/* display 5th test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+7);
	cprintf ("6...Control Signal Test");		/* display 6th test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+8);
	cprintf ("7...Status Register Test");		/* display 7th test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+9);
	cprintf ("8...NMI TImer Test");			/* display 8th test option */
	gotoxy (CMENU_LEFT,CMENU_TOP+11);
	cprintf ("9...Return To Main Options");		/* display exit option */
}

/* This routine controls the control board ram test */

ram_test()
{
	int	ptr;					/* message storage pointer */
	char	ch;					/* character storage */

	draw_window (RAM_T,RAM_B,RAM_L,RAM_R);		/* open the test option window */

	gotoxy (RAM_L+1,RAM_T+1);
	cprintf (" Control Board Ram Test");		/* display test header */

	result_x = RAM_L+2;
	result_y = RAM_T+3;				/* set test results display point */

	outportb (COM1, RAM_TEST);			/* send the ram test command to the control board */
	get_com();					/* display the test response */
	gotoxy (RAM_L+1,RAM_T+5);
	cprintf (" Press Any Key To Continue");

	getch();					/* wait for a keypress */
	puttext (RAM_L,RAM_T,RAM_R,RAM_B,buf);		/* restore text under window */

	gotoxy (CMENU_LEFT+13,CMENU_TOP+2);
	cprintf ("---------------");

	ptr = 3;
	while (ptr != 8)
	{
	  ch = in_buf [ptr];				/* display the results of the test */
	  putch (ch);
	  ptr++;
	}

}

/* This routine controls the control board EEROM test */

erom_test()
{
	unsigned char	last_sec;			/* current time storage */

	int	x;					/* screen x position */
	int	y;					/* screen y position */
	int	count	= 195;				/* test timer */
	int	ptr;					/* message storage pointer */
	char	ch;					/* character storage */

	draw_window (EROM_T,EROM_B,EROM_L,EROM_R);	/* open the test option window */

	gotoxy (EROM_L+1,EROM_T+1);
	cprintf (" Control Board EEROM Test");		/* display test header */
	gotoxy (EROM_L+1,EROM_T+5);
	cprintf (" Time Remaining In Test = ");

	result_x = EROM_L+2;
	result_y = EROM_T+3;				/* set test results display point */

	gettime (&now);					/* read the current time */
	last_sec = now.ti_sec;				/* get the current second value */

	outportb (COM1, EEROM_TEST);			/* send the eerom test command to the control board */

	while (count != 0)
	{
	  gettime (&now);				/* read the current time */

	  if (last_sec != now.ti_sec)			/* has 1 sec passed */
	  {
	    last_sec = now.ti_sec;			/* update seconds storage */
	    count--;					/* decrement test timer */
	    gotoxy (EROM_L+27,EROM_T+5);
	    cprintf ("%d",count);			/* display time left */
	    cprintf (" ");				/* erase any left over */
	  }
	}

	outportb (COM1, STOP_TEST);			/* request test results */
	get_com();					/* read in test results */

	gotoxy (EROM_L+1,EROM_T+5);
	cprintf (" Press Any Key To Continue   ");

	getch();					/* wait for a keypress */
	puttext (EROM_L,EROM_T,EROM_R,EROM_B,buf);	/* restore text under window */

	gotoxy (CMENU_LEFT+15,CMENU_TOP+3);
	cprintf ("-------------");

	x = CMENU_LEFT+28;
	y = CMENU_TOP+3;

	ptr = 5;
	while (ptr != 10)
	{
	  gotoxy (x,y);
	  ch = in_buf [ptr];				/* display the results of the test */
	  putch (ch);
	  x++;
	  ptr++;
	}

}


/* This routine controls the control board switch test */

switch_test()
{
	int	ptr;					/* message storage pointer */
	int	x;					/* screen x position */
	int	y;					/* screen y position */
	char	ch;					/* character storage */

	draw_window (SWITCH_T,SWITCH_B,SWITCH_L,SWITCH_R);	/* open the test option window */

	gotoxy (SWITCH_L+1,SWITCH_T+1);
	cprintf (" Control Board Switch Test");		/* display test header */
	gotoxy (SWITCH_L+1,SWITCH_T+3);
	cprintf ("    SW1       SW2       SW3       SW4   ");
	gotoxy (SWITCH_L+1,SWITCH_T+6);
	cprintf (" Press Any Key To Continue");


	outportb (COM1, SWITCH_TEST);			/* send the eerom test command to the control board */

	result_x = SWITCH_L+1;
	result_y = SWITCH_T+4;				/* set test results display point */

	key_flag = 0;					/* reset keypress flag */
	x = 0;
	while (x == 0)					/* repeat the following until a keypress stops the test */
	{
	  get_com();					/* display any switch change */
	  x = key_flag;
	  result_x = SWITCH_L+1;
	  result_y = SWITCH_T+4;			/* set test results display point */
	}
	key_flag = 0;					/* reset flag */

	outportb (COM1, STOP_TEST);			/* send the stop test command */
	puttext (SWITCH_L,SWITCH_T,SWITCH_R,SWITCH_B,buf);	/* restore text under window */

	gotoxy (CMENU_LEFT+16,CMENU_TOP+4);
	cprintf ("------------ Done");

}

/* This routine controls the synthesizer test */

synth_test()
{

	draw_window (SYN_T,SYN_B,SYN_L,SYN_R);		/* display the test window */
	gotoxy (SYN_L+2,SYN_T+1);
	cprintf ("Control Board Synthesizer Test");	/* display the test header */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, MARK_TEST1);			/* send test command */
	get_com();					/* display ack message */

	gotoxy (SYN_L+2,SYN_T+5);
	cprintf ("Press Any Key To Continue");		/* display test instructions */
	getch();					/* wait for a keypress to continue */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, MARK_TEST2);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, SPACE_TEST1);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1,SPACE_TEST2);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, MCLK_TEST1);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, MCLK_TEST2);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

	result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, SCLK_TEST1);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

        result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, SCLK_TEST2);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

        result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, HSCLK_TEST1);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

        result_x = SYN_L+2;
	result_y = SYN_T+3;				/* set display point for acknowledge message */
	outportb (COM1, HSCLK_TEST2);			/* send test command */
	get_com();					/* display ack message */
	getch();					/* wait for a keypress to continue */

	outportb (COM1, STOP_TEST);			/* end the test */
	puttext (SYN_L,SYN_T,SYN_R,SYN_B,buf);		/* restore the main test menu */
	gotoxy (CMENU_LEFT+21,CMENU_TOP+5);
	cprintf ("------- Done");			/* log test completed */

}

/* This test controls the control board control signal test */

control_test()
{

	draw_window (CONTROL_T,CONTROL_B,CONTROL_L,CONTROL_R);	/* display the test option window */
	gotoxy (CONTROL_L+2,CONTROL_T+1);
	cprintf ("Control Board Control Signal Test");		/* display test header */

	result_x = CONTROL_L+16;
	result_y = CONTROL_T+1;					/* set test ack display point */

	outportb (COM1, CONTROL_TEST);				/* send test command */

	gotoxy (CONTROL_L+2,CONTROL_T+3);
	cprintf ("Press Any Key To Continue");			/* display the test instructions */
	getch();						/* wait for a keypress */

	outportb (COM1, STOP_TEST);				/* stop the control signal test */
	puttext (CONTROL_L,CONTROL_T,CONTROL_R,CONTROL_B,buf);	/* restore main options window */

	gotoxy (CMENU_LEFT+24,CMENU_TOP+7);
	cprintf ("---- Done");					/* display test completed */

}


/* This test controls the control board sanity timer test */

sanity_test()
{

	draw_window (SANITY_T,SANITY_B,SANITY_L,SANITY_R);	/* display the test option window */
	gotoxy (SANITY_L+2,SANITY_T+1);
	cprintf ("Sanity Timer Test"); 				/* display test header */

	result_x = SANITY_L+2;
	result_y = SANITY_T+3;					/* set test results display point */

	outportb (COM1, SANITY_TEST);				/* send test command */
	get_com();						/* get the ack message */

	gotoxy (SANITY_L+2,SANITY_T+5);
	cprintf ("Press Any Key To Continue");			/* display the test instructions */
	getch();						/* wait for a keypress */

	puttext (SANITY_L,SANITY_T,SANITY_R,SANITY_B,buf);	/* restore main options window */

	gotoxy (CMENU_LEFT+22,CMENU_TOP+6);
	cprintf ("------ Done");				/* display test completed */

}


/* This routine controls the status register test */

stat_test()
{
	int	x = 0;						/* key flag storage */
	char	ch;						/* character storage */

	draw_window (STATUS_T,STATUS_B,STATUS_L,STATUS_R);	/* open test option window */

	gotoxy (STATUS_L+1,STATUS_T+1);
	cprintf (" Control Board Status Register Test");	/* display test header */
	gotoxy (STATUS_L+1,STATUS_T+3);
	cprintf (" RXDTR  AMHO  DGC  DGB  DGA  RSRTS  REMCTS");		/* display status settings checked */
	gotoxy (STATUS_L+1,STATUS_T+6);
	cprintf (" Press Any Key To Continue");			/* display stop test instructions */

	disp_input = -1;					/* disable get_com display of input */
	key_flag = 0;
	outportb (COM1,STAT_TEST);				/* send test command */

	while (x == 0)
	{
	  get_com();						/* get register setting */

	  gotoxy (STATUS_L+4,STATUS_T+4);
	  ch = in_buf [8];					/* get 8th character out of input buffer */
	  putch (ch);						/* display rxdtr status */

	  gotoxy (STATUS_L+10,STATUS_T+4);
	  ch = in_buf [1];					/* get 1st character out of input buffer */
	  putch (ch);						/* display amho status */

	  gotoxy (STATUS_L+16,STATUS_T+4);
	  ch = in_buf [4];					/* get 4th character out of input buffer */
	  putch (ch);						/* display dgc status */

	  gotoxy (STATUS_L+21,STATUS_T+4);
	  ch = in_buf [3];					/* get 3rd character out of input buffer */
	  putch (ch);						/* display dgb status */

	  gotoxy (STATUS_L+26,STATUS_T+4);
	  ch = in_buf [2];					/* get 2nd character out of input buffer */
	  putch (ch);						/* display dga status */

	  gotoxy (STATUS_L+31,STATUS_T+4);
	  ch = in_buf [11];					/* get 11th character out of input buffer */
	  putch (ch);						/* display rsrts status */

	  gotoxy (STATUS_L+38,STATUS_T+4);
	  ch = in_buf [10];					/* get 10th character out of input buffer */
	  putch (ch);						/* display remcts status */

	  x = key_flag;						/* check for keypress stopping getcom */
	}

	key_flag = 0;
	disp_input = 0;						/* reset the display enable flag */
	outportb (COM1, STOP_TEST);				/* send stop the test command */
	puttext (STATUS_L,STATUS_T,STATUS_R,STATUS_B,buf);	/* restore the control board menu */
	gotoxy (CMENU_LEFT+25,CMENU_TOP+8);
	cprintf ("--- Done");					/* display test completed */

}

/* This routine controls the nmi timer test */

nmi_test()
{
	int	ptr;					/* buffer pointer */
	char	ch;					/* character storage */

	draw_window (NMI_T,NMI_B,NMI_L,NMI_R);		/* display the test option window */

	gotoxy (NMI_L+1,NMI_T+1);
	cprintf (" Control Board NMI TImer Test");	/* display the test header */

	result_x = NMI_L+2;
	result_y = NMI_T+3;                     	/* set test result display point */
	outportb (COM1, NMI_TEST);			/* send test command to the control board */
	get_com();					/* wait for the test results */

	gotoxy (NMI_L+2,NMI_T+5);
	cprintf ("Press Any Key To Continue");		/* display the test instructions */
	getch();					/* wait for a keypress */

	puttext (NMI_L,NMI_T,NMI_R,NMI_B,buf);		/* restore main options window */

	gotoxy (CMENU_LEFT+19,CMENU_TOP+9);
	cprintf ("--------- ");				/* display test completed */

	ptr = 4;
	while (ptr != 8)
	{
	  ch = in_buf [ptr];				/* display the results of the test */
	  putch (ch);
	  ptr++;
	}

}



/* This routine processes the front panel tests */

fp_test()
{
	char	ch;				/* character storage */
	int	stop1 = 0;			/* stop flag  0 = go  1 = stop */

	clrscr();				/* clear the display */
	fpmenu();				/* display the front panel test options */

	while (stop1 != -1)
	{

	  ch = getch();				/* wait for a test selection */

	  switch (ch)				/* process the selected test */
	  {
	    case '1':
	    led_test();				/* go to do the led test */
	    break;

	    case '2':
	    seg_test();				/* go to do the seven segment test */
	    break;

	    case '3':
	    keypad_test();			/* go to do keypad test */
	    break;

	    case '4':
	    bar_test();				/* go to do bar graph test */
	    break;

	    case '5':
	    all_on();				/*12 do the all segments on test */
	    break;

	    case '6':				/*12 */
	    stop1 = -1;				/* stop these tests */
	    break;
	  }
	}

}


/* This routine displays the front panel test options */

fpmenu()
{
	gotoxy (FPMENU_LEFT,FPMENU_TOP);
	cprintf ("ST-8000A Front Panel Tests");	/* display title line */
	gotoxy (FPMENU_LEFT,FPMENU_TOP+2);
	cprintf ("1...Led Test");		/* display 1st test option */
	gotoxy (FPMENU_LEFT,FPMENU_TOP+3);
	cprintf ("2...Seven Segment Test");	/* display 2nd test option */
	gotoxy (FPMENU_LEFT,FPMENU_TOP+4);
	cprintf ("3...Keypad Test");		/* display 3rd test option */
	gotoxy (FPMENU_LEFT,FPMENU_TOP+5);
	cprintf ("4...Bar Graph Test");		/* display 4th test option */
	gotoxy (FPMENU_LEFT,FPMENU_TOP+6);
	cprintf ("5...Seven Segment Intensity Check");	/*12 */
	gotoxy (FPMENU_LEFT,FPMENU_TOP+8);		/*12 */
	cprintf ("6...Return To Main Options");	/* display exit option */
}


/* This routine controls the front panel led test */

led_test()
{
	draw_window (LED_T,LED_B,LED_L,LED_R);		/* display the test window */
	gotoxy (LED_L+2,LED_T+1);
	cprintf ("Front Panel Led Test");		/* display the test header */

	outportb (COM1,LED_TEST);			/* send the test command */
	gotoxy (LED_L+2,LED_T+3);
	cprintf ("Press Any Key To Continue");		/* display the test instructions */
	getch();					/* wait for a keypress */

	outportb (COM1,STOP_TEST);			/* send the stop test command */
	puttext (LED_L,LED_T,LED_R,LED_B,buf);		/* restore the test menu */
	gotoxy (FPMENU_LEFT+13,FPMENU_TOP+2);
	cprintf ("---------------------- Done");	/* display test completed */
}


/* This routine controls the front panel seven segment test */

seg_test()
{
	draw_window (SEG_T,SEG_B,SEG_L,SEG_R);		/* display the test window */
	gotoxy (SEG_L+2,SEG_T+1);
	cprintf ("Front Panel Seven Segment Test");	/* display the test header */

	outportb (COM1,SEGMENT_TEST);			/* send the test command */
	gotoxy (SEG_L+2,SEG_T+3);
	cprintf ("Press Any Key To Continue");		/* display the test instructions */
	getch();					/* wait for a keypress */

	outportb (COM1,STOP_TEST);			/* send the stop test command */
	puttext (SEG_L,SEG_T,SEG_R,SEG_B,buf);		/* restore the test menu */
	gotoxy (FPMENU_LEFT+23,FPMENU_TOP+3);
	cprintf ("------------ Done"); 			/* display test completed */
}

/* This routine processes the front panel keypad test */

keypad_test()
{
	int	stop = 0;				/* stop test flag */
	int	x = 0;					/* keyboard status storage */
	char	ch;					/* character storage */

	draw_window (KEYPAD_T,KEYPAD_B,KEYPAD_L,KEYPAD_R);	/* display test window */
	gotoxy (KEYPAD_L+2,KEYPAD_T+1);
	cprintf ("Front Panel Keypad Test");		/* display the test header */
	gotoxy (KEYPAD_L+2,KEYPAD_T+5);
	cprintf ("Press Any Key To Continue");		/* display test instructions */

	result_x = KEYPAD_L+6;
	result_y = KEYPAD_T+3;				/* init keycode display point */
	outportb (COM1,KEYPAD_TEST);			/* send test command */

	while (stop == 0)				/* repeat this loop */
	{
	  x = kbhit();					/* check for a keypress */
	  if (x != 0)					/* non-zero means a key was pressed */
	  {
	    stop = -1;					/* set the stop flag */
	    ch = getch();				/* read in the keypress */
	    outportb (COM1,STOP_TEST);			/* stop the keypad test */
	  }

	  x = inportb (COM1 + LSR); 			/* check for a keycode from cb */
	  if (x & DATA_READY)				/* do the following if we have a scan code */
	  {
	    ch = inportb (COM1);			/* read scan code */
	    ch = ch & 0x3f;				/* strip off upper 2 bits */
	    gotoxy (result_x,result_y);

	    switch (ch)					/* display the key that was pressed */
	    {
	      case 0x00:
	      cprintf ("ENTER ");			/* 00 = enter key */
	      break;

	      case 0x01:
	      cprintf ("  5   ");			/* 01 = number 5 */
	      break;

	      case 0x02:
	      cprintf ("  1   ");			/* 02 = number 2 */
	      break;

	      case 0x08:
	      cprintf ("  9   ");			/* 08 = number 9 */
	      break;

	      case 0x09:
	      cprintf ("  6   ");			/* 09 = number 6 */
	      break;

	      case 0x0a:
	      cprintf ("  2   ");			/* 0a = number 2 */
	      break;

	      case 0x10:
	      cprintf ("  0   ");			/* 10 = number 0 */
	      break;

	      case 0x11:
	      cprintf ("  7   ");			/* 11 = number 7 */
	      break;

	      case 0x12:
	      cprintf ("  3   ");			/* 12 = number 3 */
	      break;

	      case 0x18:
	      cprintf (" 0.5  ");			/* 18 = 0.5 key */
	      break;

	      case 0x19:
	      cprintf ("  8   ");			/* 19 = number 8 */
	      break;

	      case 0x1a:
	      cprintf ("  4   ");			/* 1a = number 4 */
	      break;

	      case 0x20:
	      cprintf (" 2nd  ");			/* 20 = 2nd key */
	      break;

	      case 0x21:
	      cprintf (" BIT  ");			/* 21 = bit key */
	      break;

	      case 0x22:
	      cprintf (" M/S  ");			/* 22 = ms key */
	      break;

	      case 0x28:
	      cprintf ("SPACE ");			/* 28 = space key */
	      break;

	      case 0x29:
	      cprintf ("MARK  ");			/* 29 = mark key */
	      break;

	      case 0x2a:
	      cprintf (" FSK  ");			/* 2a = fsk key */
	      break;

	      case 0x2b:
	      cprintf (" CHAN ");			/* 2b = chan key */
	      break;

	      case 0x30:
	      cprintf ("REMOTE");			/* 30 = remote key */
	      break;

	      case 0x31:
	      cprintf (" MUTE ");			/* 31 = mute key */
	      break;

	      case 0x32:
	      cprintf (" DIV  ");			/* 32 = div key */
	      break;

	      case 0x33:
	      cprintf (" BUAD ");			/* 33 = baud key */
	      break;

	      case 0x38:
	      cprintf (" SYNC ");			/* 38 = sync key */
	      break;

	      case 0x39:
	      cprintf (" NORM ");			/* 39 = norm key */
	      break;

	      case 0x3a:
	      cprintf (" AMH  ");			/* 3a = amh key */
	      break;

	      case 0x3b:
	      cprintf (" CLEAR");			/* 3b = clear key */
	      break;
	    }
	  }
	}
	puttext (KEYPAD_L,KEYPAD_T,KEYPAD_R,KEYPAD_B,buf);
	gotoxy (FPMENU_LEFT+16,FPMENU_TOP+4);
	cprintf ("------------------- Done");		/* display test completed */

}


/* This routine controls the front panel bar graph test */

bar_test()
{
	draw_window (BAR_T,BAR_B,BAR_L,BAR_R);		/* display the test window */
	gotoxy (BAR_L+2,BAR_T+1);
	cprintf ("Front Panel Bar Graph Test");		/* display the test header */

	outportb (COM1,BAR_TEST);			/* send the test command */
	gotoxy (BAR_L+2,BAR_T+3);
	cprintf ("Press Any Key To Continue");		/* display the test instructions */
	getch();					/* wait for a keypress */

	outportb (COM1,STOP_TEST);			/* send the stop test command */
	puttext (BAR_L,BAR_T,BAR_R,BAR_B,buf);		/* restore the test menu */
	gotoxy (FPMENU_LEFT+19,FPMENU_TOP+5);
	cprintf ("---------------- Done");		/* display test completed */
}


/* This routine controls the all segment intensity check for the front panel 7 segment leds */

all_on()
{
	draw_window (ALL_T,ALL_B,ALL_L,ALL_R);		/*12 draw the test option window */
	gotoxy (ALL_L+2,ALL_T+1);
	cprintf ("Seven Segment Intensity Check");	/*12 display the test header */

	outportb (COM1,ALLON_TEST);			/*12 send the test command to the pc card */
	gotoxy (ALL_L+2,ALL_T+3);
	cprintf ("Press Any Key To Continue");		/*12 display stop test message */
	getch();					/*12 wait for a keypress */

	outportb (COM1,STOP_TEST);			/*12 send the stop test command */
	puttext (ALL_L,ALL_T,ALL_R,ALL_B,buf);		/*12 erase the test window */
	gotoxy (FPMENU_LEFT+33,FPMENU_TOP+6);
	cprintf (" - Done");

}


/* This routine processes the modem board tests */

mb_test()
{
	char	ch;				/* character storage */
	int	stop1 = 0;			/* stop flag  0 = go  1 = stop */

	clrscr();				/* clear the display */
	mbmenu();				/* display the modem board test options */
	while (stop1 != 1)			/* this is the main test loop */
	{
	  ch = getch();				/* wait for a test selection */
	  ch = toupper (ch);			/* make sure letters are upper case */
	  switch (ch)				/* process the test selected */
	  {
	    case '1':
	    mod1();				/* setup for modulator output test */
	    cur_test = 1;			/* set main test pointer */
	    break;

	    case '2':
	    if1();				/* setup for input filter test */
	    cur_test = 2;			/* set main test pointer to if test */
	    break;

	    case '3':
	    mark1();				/* set up for mark filter alignment test */
	    cur_test = 3;			/* set main test pointer to mark filter test */
	    break;

	    case '4':
	    space1();				/* set up for space filter alignment test */
	    cur_test = 4;			/* set main test pointer to space filter test */
	    break;

	    case '5':
	    amh1();				/* set up for amh - gain control test */
	    cur_test = 5;			/* set main test pointer to amh test */
	    break;

	    case '6':
	    lpf1();				/* set up for lp filter detect select - hs demod test */
	    cur_test = 6;			/* set main test pointer to lpf test */
	    break;

	    case '7':
	    mio1();				/* set up for modem io test */
	    cur_test = 7;			/* set main test pointer to mio test */
	    break;

	    case '8':
	    mcio1();				/* set up for the modem - control test */
	    cur_test = 8;			/* set main test pointer to modem control test */
	    break;

	    case '9':
	    stop1 = 1;				/* return to main options */
	    break;
	  }
	  if (stop1 != 1) process_test();	/* goto main test process routine */
	}
}


/* This routine displays the modem board test options */

mbmenu()
{
	gotoxy (MMENU_LEFT,MMENU_TOP);
	cprintf ("ST-8000A Modem Board Tests");	/* display title line */
	gotoxy (MMENU_LEFT,MMENU_TOP+2);
	cprintf ("1...Modulator Output Test And Alignment");
	gotoxy (MMENU_LEFT,MMENU_TOP+3);
	cprintf ("2...Input Filter Test");
	gotoxy (MMENU_LEFT,MMENU_TOP+4);
	cprintf ("3...Mark Filter Alignment Test");
	gotoxy (MMENU_LEFT,MMENU_TOP+5);
	cprintf ("4...Space Filter Alignment Test");
	gotoxy (MMENU_LEFT,MMENU_TOP+6);
	cprintf ("5...AMH Calibration - Gain Control Test");
	gotoxy (MMENU_LEFT,MMENU_TOP+7);
	cprintf ("6...Low Pass Filter-Detect Select-HS Demod");
	gotoxy (MMENU_LEFT,MMENU_TOP+8);
	cprintf ("7...Modem I/O Check");
	gotoxy (MMENU_LEFT,MMENU_TOP+9);
	cprintf ("8...Modem-Control I/O Check");

	gotoxy (MMENU_LEFT,MMENU_TOP+11);
	cprintf ("9...Return To Main Options");	/* display exit option */
}

/* This routine is the main modem board test processor */

process_test()
{
	while (cur_test != 0)
	{
	  switch (cur_test)
	  {
	    case 1:
	    mod_out();				/* do the modulator output test */
	    break;

	    case 2:
	    if_test();				/* do the input filter test */
	    break;

	    case 3:
	    mark_test();			/* do the mark filter alignment test */
	    break;

	    case 4:
	    space_test();			/* do the space filter alignment test */
	    break;

	    case 5:
	    amh_test();				/* do the amh test */
	    break;

	    case 6:
	    lpf_test();				/* do the lp filter test */
	    break;

	    case 7:
	    mio_test();				/* do the modem io test */
	    break;

	    case 8:
	    mcio_test();			/* do the modem control io test */
	    break;
	  }
	}
}


/* 11 This routine processes the debug options */

debug_options()
{
	char	ch;				/* character storage */
	int	stop1 = 0;			/* stop flag  0 = go  1 = stop */

	clrscr();				/* clear the display */
	debug_menu();				/* display the debug options */

	while (stop1 != -1)			/* do the following until the stop flag is set */
	{
	  ch = getch();				/* wait for a test selection */
	  switch (ch)				/* do one of the following based on the value of ch */
	  {
	    case '1':
	    repeat_ram();			/* go to do the repeated ram test */
	    break;

	    case '2':
	    stop1 = -1;				/* set the stop flag */
	    break;
	  }
	}

}


/* This routine displays the debug options */

debug_menu()
{
	gotoxy (DMENU_LEFT,DMENU_TOP);
	cprintf ("ST-8000A Debug Options");		/* display title line */
	gotoxy (DMENU_LEFT,DMENU_TOP+2);
	cprintf ("1...Repeated Ram Test");		/* display the 1st debug option */
	gotoxy (DMENU_LEFT,DMENU_TOP+4);
	cprintf ("2...Return To Main Options");	/* display exit option */
}

/* This routine repeats the ram test and reports failures until a key is pressed */

repeat_ram()
{
	int	stop1 = 0;				/* stop flag */
	long int	passes = 0;			/* test counter */
	long int	fails = 0;			/* test failed counter */
	char	ch;					/* character storage */

	draw_window (RAM_T,RAM_B,RAM_L,RAM_R);		/* display the debug option window */
	gotoxy (RAM_L+1,RAM_T+1);
	cprintf ("     Repeated Ram Test");		/* display the debug header */
	gotoxy (RAM_L+1,RAM_T+3);
	cprintf (" Passes - 0");			/* display test results info */
	gotoxy (RAM_L+1,RAM_T+4);
	cprintf (" Fails  - 0");

	disp_input = -1;				/* disable display of test results */
	while (stop1 != -1)				/* repeat this test until the stop flag is set */
	{
	  outportb (COM1, RAM_TEST);			/* send the test command */
	  get_com();					/* get the test results */
	  if (key_flag == -1)				/* do the following if a key was pressed */
	  {
	    key_flag = 0;				/* reset the keypress flag */
	    stop1 = -1;					/* set the stop flag */
	  }
	  else       					/* otherwise report pass results */
	  {
	    passes++;
	    gotoxy (RAM_L+11,RAM_T+3);
	    cprintf ("%ld",passes);			/* update times test has been run */
	    ch = in_buf [4];
	    if (ch != 'P')				/* do the following if the test failed */
	    {
	      fails++;					/* increment the failure counter */
	      gotoxy (RAM_L+11,RAM_T+4);
	      cprintf ("%d",fails);			/* update times test has been failed */
	    }
	  }
	}

	disp_input = 0;					/* reset display flag */

	puttext (RAM_L,RAM_T,RAM_R,RAM_B,buf);		/* restore the screen display */

}


/* These last routines deal with com 1 communications */


setup_com()
{
    char  setup;

    outportb(COM1+LCR,0x80);           		/* set DLAB to baud div input */
    outportb(COM1+1,BAUD_9600 >> 8);        	/* set high byte of divisor */
    outportb(COM1,BAUD_9600 & 0xff);     	/* set low byte of divisor */
    setup = DBITS_8 | SBITS_2 | NO_PARITY;
    outportb(COM1+LCR,setup);          		/* set char attributes */
    outportb(COM1+MCR,0x03);           		/* set RTS and DTR ON permanently */
    outportb(COM1+IEN,0x00);           		/* no interrupts please */
}


/* This routine inputs and displays a message from the control board */

get_com()
{
	int	x = 0;				/* keyboard status storage */
	int	stop_this = 0;			/* stop flag */
	char	status = 0;			/* character storage */

	while (stop_this != -1)			/* do the following until a stop condition is found */
	{
	  x = kbhit();				/* check for a keypress */
	  if (x != 0)				/* do the following if a key was pressed */
	  {
	    status = getch();			/* read the keyboard key */
	    stop_this = -1;			/* set the stop flag */
	    key_flag = -1;			/* set the key pressed flag */
	  }


	  status = inportb(COM1 + LSR);		/* check for data from the control board */
	  if(status & DATA_READY)		/* do the following if we have data */
	  {
	    status = inportb (COM1);		/* read the data */
	    if (status == -1) stop_this = -1;	/* check for end of message and set flag if found */
	    else
	    {
	      in_buf [in_pntr] = status;	/* store the character */
	      in_pntr++;			/* increment the storage pointer */
	    }
	  }
	}
	if ((in_pntr != out_pntr) && (disp_input == 0))		/* do the following if there is a message to display */
	{							/* and display is enabled */
	  while (in_pntr != out_pntr)		/* display the entire message */
	  {
	    status = in_buf [out_pntr];		/* get the character to display */
	    gotoxy (result_x,result_y);		/* goto the screen display point */
	    putch (status);			/* display the message character */
	    out_pntr++;				/* increment the message pointer */
	    result_x++;				/* increment the screen pointer */
	  }
	}
	in_pntr = 0;
	out_pntr = 0;				/* reset the screen pointers */

}

/* This routine displays the test option window and saves the text under it */

draw_window (top,bottom,left,right)

int	top;					/* window top line pointer */
int	bottom;					/* window bottom line pointer */
int	left;					/* window left side */
int 	right;					/* window right side */

{
	int	x;				/* screen column pointer */
	int	y;				/* screen line pointer */

	gettext (left,top,right,bottom,buf);	/* save the text under the window */

	gotoxy (left,top);
	putch (DULEFT_CHAR);			/* display the upper left corner */
	x = left+1;
	y = top;
	while (x != right)
	{
	  gotoxy (x,y);				/* draw the top line of the window */
	  putch (DHORZ_CHAR);
	  x++;
	}
	putch (DURIGHT_CHAR);			/* display the upper right corner */

	x = left;
	y = top+1;
	while (y != bottom)
	{
	  gotoxy (x,y);
	  putch (VERT_CHAR);			/* draw the left side of the window */
	  y++;
	}
	gotoxy (left,bottom);
	putch (DLLEFT_CHAR);			/* display the lower left corner */

	x = right;
	y = top+1;
	while (y != bottom)
	{
	  gotoxy (x,y);
	  putch (VERT_CHAR);			/* draw the right side of the window */
	  y++;
	}

	x = left+1;
	y = bottom;
	while (x != right)
	{
	  gotoxy (x,y);
	  putch (DHORZ_CHAR);			/* draw the bottom line of the window */
	  x++;
	}
	putch (DLRIGHT_CHAR);			/* display the lower right corner of the window */

	y = top+1;
	while (y != bottom)
	{
	  gotoxy (left+1,y);			/* blank out the inside of the window */
	  finish_line (right);
	  y++;
	}
}


/* This routine finishes a line with spaces */

finish_line (stop_point)

int	stop_point;				/* when to stop */

{
	int	x;				/* screen pointer storage */

	x = wherex();				/* find out where we are */
	while (x != stop_point)
	{
	  cprintf(" ");
	  x++;					/* finish line with spaces */
	}

}


/* This routine sends a test command string to com1 and displays the characters echoed */

send_cmd (str)

char	*str;					/* command string pointer */

{
	char	ch = 0;				/* character storage */
	int	ptr = 0;			/* command string pointer */
	int	stat = 0;			/* status storage */

	while (ch != CR)			/* send the entire command string */
	{
	  while (stat != TX_READY)	 	/* wait for the transmit ready */
	  {
	    stat = inportb (COM1 + LSR); 	/* read the port status register */
	    stat = stat & TX_READY;	 	/* test for tx ready */
	  }

	  ch = str [ptr];			/* get a command character */
	  ptr++;				/* increment the string pointer */
	  outportb (COM1, ch);			/* send the command character */
	  stat = 0;				/* set status to a known value */

	  while (stat != DATA_READY)		/* wait for the echoed character */
	  {
	    stat = inportb (COM1 + LSR);	/* read the status register */
	    stat = stat & DATA_READY;		/* test for data ready */
	  }

	  ch = inportb (COM1);			/* read in the echoed character */
	  gotoxy (result_x,result_y);		/* goto screen display point */
	  putch (ch);				/* display the echoed character */
	  result_x++;				/* increment display x pointer */
	}

}

/* This routine erases the test command 1 display from the test window */

erase_tc1()
{
	result_x = 20;
	while (result_x != 67)
	{
	  gotoxy (result_x,result_y);
	  putch (0x20);					/* erase test command 1 */
	  result_x++;
	}
}