/*	Title	All-Pro Diagnostics*/
/******************************************************************************/
/*				     Diagctrl				      */
/*			       SE6440 Diagnostic Controler tests	      */
/*									      */
/* Module ...................... DIAGCTRL				      */
/* Title ....................... SE6440 Diagnostic Controler tests	      */
/* Developing Engineer ......... Steven T. Harr 			      */
/* Company ..................... Logical Devices			      */
/* Revision Level .............. 1.00					      */
/* Date of Last Revision ....... 10-OCT-89				      */
/* Last Revision by ............ STH					      */
/*									      */
/*									      */
/******************************************************************************/
/*	Subttl	Includes*/
#include <stdio.h>
#include <ctype.h>
#include "pindrivr.h"
#include "diag2.h"


extern unsigned char analog_id;
extern int cursor_top;
extern int cursor_bottom;
extern int func_help();
extern int func_null();
extern struct menu_struc menu_sequencer_test [];
extern struct menu_struc menu_controller_test [];


func_temp_test()
{
    int upper, lower;
    pump ();							/* unlock udi interface for access		*/
    hide_cursor();
    while(keyboard_status() == -1)
    {
	crt_pos_cursor (7, 36, 0);
	upper = read_voltage(UPPER_TEMP_SENSE) * 100 / 256;
	lower = read_voltage(LOWER_TEMP_SENSE) * 100 / 256;
	printf ("Upper = %4d, Lower = %4d.",upper,lower);
    }
    show_cursor();
    unpump ();							  /* unlock udi interface for access		  */
}

func_load_counter_test()
{
    int i;

    print_running_msg(menu_sequencer_test[2].set_menu_row);
    for(i=0;keyboard_status() == -1;i+=8)
    {
	udi_write_mem(SEQUENCER_COUNTER,i);
    }
}

func_load_ram_test()
{
    int i;

    print_running_msg(menu_sequencer_test[3].set_menu_row);
    while(keyboard_status() == -1)
    {
	udi_write_mem(SEQUENCER_COUNTER,0);
	for(i=0;i<256;i++)
	{
	    udi_write_mem(SEQUENCER_RAM,i);
	}
    }
}

func_shift_reg_test()
{
    int i,j;

    print_running_msg(menu_sequencer_test[4].set_menu_row);
    j=0xaa;
    udi_write_dac(THRESH_HOLD_DAC,1000);
    while(keyboard_status() == -1)
    {
	udi_write_mem(SEQUENCER_COUNTER,0);
	for(i=0;i<32;i++)
	{
	    udi_write_mem(SEQUENCER_SHIFTER,i);
	    delay_us(20);
	}
	j ^= 0xff;
    }
}

struct menu_struc menu_sequencer_test [] =
{
    { '?',*func_help,  2, 'C', "SEQUENCER TESTS" },
    { '?',*func_help,  3, 'C', "CHECK SIGNALS WITH SCOPE" },
    { '1',*func_load_counter_test,      6,  'L', "1. Load Counter test." },
    { '2',*func_load_ram_test,          7,  'L', "2. Load Ram test." },
    { '3',*func_shift_reg_test,         8,  'L', "3. Load Shift Register test." },
    { '\0',*func_null, 0,  0, "" }
};
func_sequencer_test()
{
    select_command ( menu_sequencer_test );
}

func_bus_test()
{
    int i;
    print_running_msg(menu_controller_test[2].set_menu_row);
    for(i=1;keyboard_status() == -1;)
    {
#asm
dataseg segment para public 'data'
    extrn   UdiSegment_:word
dataseg ends
    Push    es
    Mov     ax,[UdiSegment_]
    Mov     es,ax
    Mov     al,byte ptr es:[0]
    Mov     al,1
    Mov     byte ptr es:[07ffh],al
    Shl     al,1
    Mov     byte ptr es:[0bffh],al
    Shl     al,1
    Mov     byte ptr es:[0dffh],al
    Shl     al,1
    Mov     byte ptr es:[0effh],al
    Shl     al,1
    Mov     byte ptr es:[0f7fh],al
    Shl     al,1
    Mov     byte ptr es:[0fbfh],al
    Shl     al,1
    Mov     byte ptr es:[0fdfh],al
    Shl     al,1
    Mov     byte ptr es:[0fefh],al
    Mov     al,1
    Mov     byte ptr es:[0ff7h],al
    Shl     al,1
    Mov     byte ptr es:[0ffbh],al
    Shl     al,1
    Mov     byte ptr es:[0ffdh],al
    Shl     al,1
    Mov     byte ptr es:[0ffeh],al
    Pop     es
#endasm
    }
}

func_enable_test()
{
    int temp, cntr;
    print_running_msg(menu_controller_test[3].set_menu_row);
    while(keyboard_status() == -1)
    {
	cntr = 1;
	while(cntr != 256)
	{
	    udi_write_mem(0x297,cntr);
	    cntr <<= 1;
	}
    }
}

func_pclock_test()
{
    print_running_msg(menu_controller_test[4].set_menu_row);
    while(keyboard_status() == -1)
    {
	udi_write_mem(PCLOCK,0);
	udi_write_mem(PCLOCK,1);
	udi_write_mem(PCLOCK,2);
	udi_write_mem(PCLOCK,4);
	udi_write_mem(PCLOCK,8);
	udi_write_mem(PCLOCK,16);
	udi_write_mem(PCLOCK,32);
	udi_write_mem(PCLOCK,64);
	udi_write_mem(PCLOCK,128);
    }
}

func_system_id_test()
{
    print_running_msg(menu_controller_test[5].set_menu_row);
    while(keyboard_status() == -1)
    {
	get_analog_id ();
	crt_pos_cursor (menu_controller_test[5].set_menu_row, 40, 0);
	printf ("System ID code = %02xh.", analog_id);
    }
}

func_front_panel_test()
{
    int sw;
    print_running_msg(menu_controller_test[6].set_menu_row);
    while(keyboard_status() == -1)
    {
	sw = udi_read_mem(ANALOG_CONTROL);
    /*	sw &= 0xf0;*/
	crt_pos_cursor (menu_controller_test[6].set_menu_row, 40, 0);
	printf ("Switch status = %c%c%c%c", sw&0x10?25:24,sw&0x20?25:24,sw&0x40?25:24,sw&0x80?25:24);
    }
}


struct menu_struc menu_controller_test [] =
{
    { '?',*func_help,  2, 'C', "CONTROLLER TESTS" },
    { '?',*func_help,  3, 'C', "CHECK SIGNALS WITH SCOPE" },
    { '1',*func_bus_test,               6,  'L', "1. Bus test." },
    { '2',*func_enable_test,            7,  'L', "2. Enable Register test." },
    { '3',*func_pclock_test,            8,  'L', "3. PCLOCK test." },
    { '4',*func_system_id_test,         9,  'L', "4. System ID test." },
    { '5',*func_front_panel_test,      10,  'L', "5. Front Panel switch test." },
    { '6',*func_sequencer_test,        11,  'L', "6. Sequencer test." },
    { '7',*func_temp_test,             12,  'L', "7. Temperature sense test." },
    { '\0',*func_null, 0,  0, "" }
};
func_controller()
{
    select_command ( menu_controller_test );
}



read_voltage (loc)
unsigned int loc;
{
    unsigned int approx;
    unsigned int guess;

    approx = 0; 						/* initial approximation			*/

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

	if (udi_read_mem (loc) & 1)				/* was it too low?				*/
	{
	    approx = approx + guess;				/* yes - restore AND add			*/
	}
    }

    return (approx);						/* return dac value				*/
}






