/*	Title	All-Pro Diagnostics*/
/******************************************************************************/
/*				     Diagdrvr				      */
/*			       All-pro Diagnostic Pin driver tests	      */
/*									      */
/* Module ...................... DIAGDRVR				      */
/* Title ....................... All-pro Diagnostic Pin driver tests	      */
/* Developing Engineer ......... Steven T. Harr 			      */
/* Company ..................... Logical Devices			      */
/* Revision Level .............. 1.00					      */
/* Date of Last Revision ....... 17-FEB-87				      */
/* Last Revision by ............ STH					      */
/*									      */
/*									      */
/******************************************************************************/
/*	Subttl	Includes*/
#include <stdio.h>
#include <ctype.h>
#include "pindrivr.h"
#include "diag2.h"

/*	Subttl	Externals */
extern unsigned char analog_id;
extern int real_pin [];
extern unsigned int phys_pin_offset;
extern unsigned int pin_base_address[];

extern func_null ();
extern func_help ();
extern pin_driver_test_misc ();
extern se6440_flag;
extern int selected_zif_row;
extern int selected_zif_col;
extern unsigned int max_pins;

static int loop_flag;

pd_test_separation ()
{
    int stop_flag;
    int pin_num;
    int error_pin;
    int i;

    enable_selected_socket();

    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    for (pin_num = 1; pin_num <= max_pins; pin_num++)
    {
	if (real_pin [pin_num])
	{
	    set_pin_cnf (pin_num, GD_DRIVER_ON);
	}
    }
    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		for (i = 1; (i <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); i++)
		{
		    if ((real_pin [i]) && (i != pin_num))
		    {
			if (read_zif_pin (i))
			{
			    error_pin = pin_num;
			}
		    }
		}
		set_pin_cnf (pin_num, GD_DRIVER_ON | PU_DRIVER_ON);
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}



pd_test_source_drivers ()
{
    int stop_flag;
    int pin_num;
    int error_pin;


    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		set_pin_dac (pin_num, 1000);
		set_pin_cnf (pin_num, SRC_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_dac (pin_num, 0);
		set_pin_cnf (pin_num, PD_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}


pd_test_ground_drivers ()
{
    int stop_flag;
    int pin_num;
    int error_pin;


    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, GD_DRIVER_ON | PU_DRIVER_ON);
		if (read_zif_pin (pin_num))  {error_pin = pin_num;}
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, PD_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}


pd_test_pullup_drivers ()
{
    int stop_flag;
    int pin_num;
    int error_pin;


    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, PD_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}


pd_test_ttl_drivers ()
{
    int stop_flag;
    int pin_num;
    int error_pin;


    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		set_pin_cnf (pin_num, TTL_HI_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, TTL_LOW_DRIVER_ON | PU_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, PD_DRIVER_ON);
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}

pd_test_decouple_drivers ()
{
    int stop_flag;
    int pin_num;
    int error_pin;


    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		/* decouple off test */
		decouple_pin(pin_num,OFF);
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, PD_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}

		/* decouple on test */
		decouple_pin(pin_num,ON);
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
		delay_us(200);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, PD_DRIVER_ON);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		delay_us(400);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
		decouple_pin(pin_num,OFF);
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}


pd_test_disconnect_drivers ()
{
    int stop_flag;
    int pin_num;
    int error_pin;


    if (loop_flag)
    {
	crt_pos_cursor (15, 15, 0);
	printf ("Looping");
    }

    stop_flag = FALSE;
    do
    {
	error_pin = 0;
	for (pin_num = 1; (pin_num <= max_pins) && (error_pin == 0) && (stop_flag == FALSE); pin_num++)
	{
	    if (real_pin [pin_num])
	    {
		set_pin_cnf (pin_num, PU_DRIVER_ON);
		set_pin_cnf (pin_num, 0);
		if (!read_zif_pin (pin_num)) {error_pin = pin_num;}
		set_pin_cnf (pin_num, PD_DRIVER_ON);
		if (read_zif_pin (pin_num)) {error_pin = pin_num;}
	    }

	    if ((loop_flag) && (keyboard_status () != -1))
	    {
		stop_flag = TRUE;
	    }
	}
    }
    while ((loop_flag) && (stop_flag == FALSE));

    return (error_pin);
}


struct menu_struc menu_ground_driver  [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_ground_drivers,   7,  'L', "1. Ground driver test." },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_pullup_driver  [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_pullup_drivers,   7,  'L', "1. Pull-up driver test." },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_disconnect_driver  [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_disconnect_drivers,   7,  'L', "1. Disconnect test." },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_source_driver  [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_source_drivers,   7,  'L', "1. Source driver test." },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_ttl_driver  [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_ttl_drivers,   7,  'L', "1. TTL driver test." },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_separation_test	[] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_separation,   7,  'L', "1. Separation test." },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_decouple_driver	[] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TEST and TROUBLE SHOOT" },
    { '1',*pd_test_decouple_drivers,   7,  'L', "1. Decouple driver test." },
    { '\0',*func_null, 0,  0, "" }
};



    /* Test pin separation */
pin_driver_test_separation ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1000, 250, 500, 100);

    if (error = pd_test_separation ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed separation test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_separation_test );
    }
    reset_allpro ();
    return (error);
}


    /* Test disconnect pins */
pin_driver_test_dc ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1000, 250, 500, 100);

    if (error = pd_test_disconnect_drivers ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed disconnect test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_disconnect_driver );
    }
    reset_allpro ();
    return (error);
}


    /* Test pull up drivers */
pin_driver_test_pu ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1000, 250, 500, 100);

    if (error = pd_test_pullup_drivers ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed pull-up driver test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_pullup_driver );
    }
    reset_allpro ();
    return (error);
}


    /* Test ground drivers */
pin_driver_test_gd ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1000, 250, 500, 100);

    if (error = pd_test_ground_drivers ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed ground driver test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_ground_driver );
    }
    reset_allpro ();
    return (error);
}


    /* Test source drivers */
pin_driver_test_src ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1500, 950, 500, 100);

    if (error = pd_test_source_drivers ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed source driver test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_source_driver );
    }
    reset_allpro ();
    return (error);
}


    /* Test TTL drivers */
pin_driver_test_ttl ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1000, 250, 500, 100);

    if (error = pd_test_ttl_drivers ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed TTL driver test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_ttl_driver );
    }
    reset_allpro ();
    return (error);
}

pin_driver_test_decouple ()
{
    int error = 0;

    loop_flag = FALSE;
    enable_selected_socket();
    setup_supplies (1000, 250, 500, 100);

    if (error = pd_test_decouple_drivers ())
    {
	crt_pos_cursor (22, 5, 0);
	printf ("Failed decouple driver test, pin %2d", error);
	keybrd_getc ();
	loop_flag = TRUE;
	select_command (menu_decouple_driver );
    }
    reset_allpro ();
    return (error);
}


pin_driver_test_full ()
{
    int error = 0;

    if (!error)
    {
	error = pin_driver_test_dc ();
    }

    if (!error)
    {
	error = pin_driver_test_pu ();
    }

    if (!error)
    {
	error = pin_driver_test_gd ();
    }

    if (!error)
    {
    /*	  error = pin_driver_test_separation ();*/
    }

    if (!error)
    {
	error = pin_driver_test_src ();
    }

    if (!error)
    {
	error = pin_driver_test_ttl ();
    }

    if (se6440_flag & !error)
    {
	error = pin_driver_test_decouple ();
    }

    if (!error)
    {
	/* print passed test */
    }

}



struct menu_struc menu_pin_driver_test [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TESTS" },
    { '1',*pin_driver_test_full,       7,  'L', "1. Full test." },
    { '2',*pin_driver_test_dc,         8,  'L', "2. Disconnect test." },
    { '3',*pin_driver_test_pu,         9,  'L', "3. Pull-up driver test." },
    { '4',*pin_driver_test_gd,         10,  'L', "4. Ground driver test." },
    { '5',*pin_driver_test_src,        11,  'L', "5. Source driver test." },
    { '6',*pin_driver_test_ttl,        12,  'L', "6. TTL driver test." },
/*    { '7',*pin_driver_test_separation, 13,  'L', "7. Separation test." },*/
    { '8',*pin_driver_test_decouple,   14,  'L', "8. Decouple driver test." },
    { '9',*pin_driver_test_misc,       15,  'L', "9. Test other." },
    { '\0',*func_null, 0,  0, "" }
};


func_system_test ()
{

}


func_pin_driver_test ()
{
    select_command ( menu_pin_driver_test );
    reset_allpro ();
}


setup_supplies (vadj, vth, vpu, vsr)
int vadj;
int vth;
int vpu;
int vsr;
{
    reset_allpro();
    udi_write_bit (DAC_REFERENCE_BIT, ON);			/* make sure dac reference on			*/
    udi_write_mem(ANALOG_CONTROL,DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
    udi_write_dac (SUPPLY1_POWER_DAC, vadj);
    udi_write_dac (PU_CONTROL_DAC, vpu);
    udi_write_dac (THRESH_HOLD_DAC, vth);
    udi_write_dac (SLEW_RATE_DAC, vsr);
    delay_ms (100);
}




decouple_pin(pn,state)
int pn, state;
{
    if (se6440_flag)
       udi_write_mem(pin_base_address[pn+phys_pin_offset]+1,state);
    else
       udi_write_mem(0x280 + (pin_base_address[pn+phys_pin_offset] >> 4),state);
}


enable_selected_socket()
{
    if (se6440_flag)
    {
	udi_write_mem (ANALOG_CONTROL,GREEN_LED_OFF_BIT); /* turn off all led's */
	select_zif(selected_zif_row, selected_zif_col);
    }
}

read_zif_pin(pin)
{
    return udi_read_mem(pin_base_address[phys_pin_offset+pin]) & 1;
}

read_voltage_at_pin (pin)
unsigned int pin;
{
    unsigned int approx;
    unsigned int guess;

    approx = 0; 						/* initial approximation			*/

    for (guess = 0x80; guess > 0; guess >>= 1)			/* addend and control				*/
    {
	udi_write_mem (THRESH_HOLD_DAC, approx + guess);	/* set comp drive				*/

	if (read_zif_pin (pin)) 				/* was it too low?				*/
	{
	    approx = approx + guess;				/* yes - restore AND add			*/
	}
    }

    return (approx * 10);					/* return pinvalue				*/
}

select_zif(row,col)
{
}

select_zif_socket()
{
}

