/*	Title	All-Pro Diagnostics*/
/******************************************************************************/
/*				     Diagcnfg				      */
/*			       All-pro Diagnostic Configuration 	      */
/*									      */
/* Module ...................... DIAGCNFG				      */
/* Title ....................... All-pro Diagnostic Configuration	      */
/* 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"
#include "windows.h"
#include "window.h"
#include "keybrd.h"
#include "device.h"

#define REVERSE_VIDEO			0x70
#define NORMAL_VIDEO			0x07
#define DECIMAL 		1
#define HEX			2
#define ASCII			3

extern int func_help ();
extern int func_null ();
extern unsigned int UdiSegment;
extern char user_file_name [];
extern int allpro88_flag;
extern int se6440_flag;
extern unsigned int max_pins;

int file_byte_ctr;
int file_eof_flag;
char *file_buffer_ptr;
char file_buffer [512];

static int pin = 1;

#define TEST_PINS	    1
#define SEGMENT 	    2
#define TEST_FILE	    3
#define PROGRAMMER	    4


#define MAX_INIT_FUNCS		5
char func_ptr [MAX_INIT_FUNCS] [11] =
{
    {""},
    {"TEST_PINS"},
    {"SEGMENT"},
    {"TEST_FILE"},
    {"PROGRAMMER"}
};



FILE *fptr, *fopen ();
char file_path [64];

int real_pin [MAX_PROGRAMMER_PINS+1];



struct menu_struc menu_config [] =
{
    { '?',*func_help,  4, 'C', "ALLPRO DIAGNOSTICS CONFIGURATION" },
    { '\0',*func_null, 0,  0, "" }
};

struct pin_num_struct
{
    unsigned char row;
    unsigned char col;
    unsigned char left;
    unsigned char right;
    unsigned char up;
    unsigned char down;
    unsigned char flag;
} pin_menu [MAX_PROGRAMMER_PINS+1] =
{
    {0,0}, /* dummy pin */

    { 2, 3,00,21,00, 2},
    { 3, 3,00,22, 1, 3},
    { 4, 3,00,23, 2, 4},
    { 5, 3,00,24, 3, 5},
    { 6, 3,00,25, 4, 6},
    { 7, 3,00,26, 5, 7},
    { 8, 3,00,27, 6, 8},
    { 9, 3,00,28, 7, 9},
    {10, 3,00,29, 8,10},
    {11, 3,00,30, 9,11},
    {12, 3,00,31,10,12},
    {13, 3,00,32,11,13},
    {14, 3,00,33,12,14},
    {15, 3,00,34,13,15},
    {16, 3,00,35,14,16},
    {17, 3,00,36,15,17},
    {18, 3,00,37,16,18},
    {19, 3,00,38,17,19},
    {20, 3,00,39,18,20},
    {21, 3,00,40,19,21},

    { 2,13, 1,41,20,22},
    { 3,13, 2,42,21,23},
    { 4,13, 3,43,22,24},
    { 5,13, 4,44,23,25},
    { 6,13, 5,45,24,26},
    { 7,13, 6,46,25,27},
    { 8,13, 7,47,26,28},
    { 9,13, 8,48,27,29},
    {10,13, 9,49,28,30},
    {11,13,10,50,29,31},
    {12,13,11,51,30,32},
    {13,13,12,52,31,33},
    {14,13,13,53,32,34},
    {15,13,14,54,33,35},
    {16,13,15,55,34,36},
    {17,13,16,56,35,37},
    {18,13,17,57,36,38},
    {19,13,18,58,37,39},
    {20,13,19,59,38,40},
    {21,13,20,60,39,41},


    { 2,23,21,61,40,42},
    { 3,23,22,62,41,43},
    { 4,23,23,63,42,44},
    { 5,23,24,64,43,45},
    { 6,23,25,65,44,46},
    { 7,23,26,66,45,47},
    { 8,23,27,67,46,48},
    { 9,23,28,68,47,49},
    {10,23,29,69,48,50},
    {11,23,30,70,49,51},
    {12,23,31,71,50,52},
    {13,23,32,72,51,53},
    {14,23,33,73,52,54},
    {15,23,34,74,53,55},
    {16,23,35,75,54,56},
    {17,23,36,76,55,57},
    {18,23,37,77,56,58},
    {19,23,38,78,57,59},
    {20,23,39,79,58,60},
    {21,23,40,80,59,61},

    { 2,33,41,81,60,62},
    { 3,33,42,82,61,63},
    { 4,33,43,83,62,64},
    { 5,33,44,84,63,65},
    { 6,33,45,85,64,66},
    { 7,33,46,86,65,67},
    { 8,33,47,87,66,68},
    { 9,33,48,88,67,69},
    {10,33,49,00,68,70},
    {11,33,50,00,69,71},
    {12,33,51,00,70,72},
    {13,33,52,00,71,73},
    {14,33,53,00,72,74},
    {15,33,54,00,73,75},
    {16,33,55,00,74,76},
    {17,33,56,00,75,77},
    {18,33,57,00,76,78},
    {19,33,58,00,77,79},
    {20,33,59,00,78,80},
    {21,33,60,00,79,81},

    { 2,43,61,00,80,82},
    { 3,43,62,00,81,83},
    { 4,43,63,00,82,84},
    { 5,43,64,00,83,85},
    { 6,43,65,00,84,86},
    { 7,43,66,00,85,87},
    { 8,43,67,00,86,88},
    { 9,43,68,00,87,00}
};

display_pin_state(pin)
{
    pos_write_window(WND_CNFG,pin_menu[pin].row,pin_menu[pin].col,"Pin %2d %c",pin,real_pin[pin]?'*':' ');
}

display_pin_menu()
{
    int i;
    for(i=1;i<=max_pins;i++)
    {
	display_pin_state(i);
    }
}


pin_menu_trav()
{
    int ch;
    setup_window(WND_CNFG);

    display_pin_menu();
    while(1)
    {
	highlight_window_line(WND_CNFG,pin_menu[pin].row,pin_menu[pin].col,9,REVERSE_VIDEO);
	ch = keybrd_getc();
	highlight_window_line(WND_CNFG,pin_menu[pin].row,pin_menu[pin].col,9,NORMAL_VIDEO);
	switch(ch)
	{
	    case KEY_CURSOR_UP:
		if(pin_menu[pin].up > 0 && pin_menu[pin].up <= max_pins) pin = pin_menu[pin].up;
		break;
	    case KEY_CURSOR_DOWN:
		if(pin_menu[pin].down > 0 && pin_menu[pin].down <= max_pins) pin = pin_menu[pin].down;
		break;
	    case KEY_CURSOR_LEFT:
		if(pin_menu[pin].left > 0 && pin_menu[pin].left <= max_pins) pin = pin_menu[pin].left;
		break;
	    case KEY_CURSOR_RIGHT:
		if(pin_menu[pin].right > 0 && pin_menu[pin].right <= max_pins) pin = pin_menu[pin].right;
		break;
	    case KEY_ESC:
		return();
		break;
	    case KEY_CR:
		break;
	    case ' ':
		real_pin[pin] ^= 1;
		display_pin_state(pin);
		break;
	}
    }
}




/*	Subttl	diagnostic configuration menu */
func_config ()
{
    char line_buffer [256];

    pin_menu_trav();
    return();

    print_menu ( menu_config );
    crt_pos_cursor (7, 4, 0);

    file_eof_flag = FALSE;

    if ((fptr = fopen ("diag4.cfg", "r")) == NULL)
    {
	printf ("No .cfg File Found");
    }
    else
    {
	while (file_eof_flag == FALSE)
	{
	    printf ("%c", get_file_char ());
	}
	fclose (fptr);
    }
    keybrd_getc ();
}




/*	Subttl	initialize diagnostic */
initialize_diagnostic ()
{
    char line_buffer [80];
    char fchar;
    char programmer_name[65];
    unsigned int num_ctr = 0;
    int char_ctr = 0;
    int init_func = 0;

    for (num_ctr = 1; num_ctr <= MAX_PROGRAMMER_PINS; num_ctr++) /* default all active */
    {
	real_pin [num_ctr] = TRUE;
    }
    UdiSegment = 0xa000; /* default segment */
    se6440_flag = FALSE; /* default programmer */

    max_pins = 40;	 /* default ALLPRO 40 */
    allpro88_flag = FALSE;
    se6440_flag = FALSE;


    if ((fptr = fopen ("diag4.cfg", "r")) == NULL)
    {
	return (FALSE);
    }
    else
    {
	while (file_eof_flag == FALSE)
	{
	    char_ctr = 0;
	    while ((((fchar = get_file_char ()) >= 'A') && (fchar <= 'Z')) || (fchar == '_'))
	    {
		line_buffer [char_ctr] = fchar;
		char_ctr++;
	    }
	    line_buffer [char_ctr] = 0;
	    init_func = get_function (line_buffer);

	    switch (init_func)
	    {
		case TEST_PINS:
		{
		    for (num_ctr = 1; num_ctr <= MAX_PROGRAMMER_PINS; num_ctr++)
		    {
			real_pin [num_ctr] = FALSE;
		    }

		    while ((num_ctr = get_digit (DECIMAL)) != -1)
		    {
			real_pin [num_ctr] = TRUE;
		    }
		    break;
		}

		case SEGMENT:
		{
		    if (((num_ctr = get_digit (HEX)) >= 0xa000) &&
			 (num_ctr <= 0xd000))
		    {
			UdiSegment = num_ctr;
		    }
		    break;
		}

		case TEST_FILE:
		{
		    for (char_ctr = 0;(char_ctr < 64) && (fchar = (char) get_digit (ASCII)) != -1; char_ctr++)
		    {
			user_file_name [char_ctr] = fchar;
		    }
		    user_file_name [char_ctr] = '\0';
		    break;
		}
		case PROGRAMMER:
		{
		    for (char_ctr = 0;(char_ctr < 64) && (fchar = (char) get_digit (ASCII)) != -1; char_ctr++)
		    {
			programmer_name [char_ctr] = fchar;
		    }
		    programmer_name [char_ctr] = '\0';
		    if(strcmp(programmer_name,"SE6440") == 0)
		    {
			se6440_flag = TRUE;
			max_pins = 40;
		    }
		    else if(strcmp(programmer_name,"ALLPRO88") == 0)
		    {
			allpro88_flag = TRUE;
			max_pins = 88;
		    }
		    break;
		}
	    }
	}
	fclose (fptr);
    }
    return (TRUE);
}


get_digit (radix)
int radix;
{
    char file_char;
    int valid_digit;
    int digit = 0;

    while (((file_char = get_file_char ()) == ' ') || (file_char == 0x09));

    if (file_char == ';')
    {
	while (((file_char = get_file_char ()) != 0x0d) &&
		(file_char != 0x0a) &&
		(file_eof_flag == FALSE));
    }

    if ((file_char == 0x0d) || (file_char == 0x0a))
    {
	return (-1);
    }

    if (radix == DECIMAL)
    {
	while ((file_char <= '9') && (file_char >= '0'))
	{
	    digit = digit * 10 + (file_char - 0x30);
	    file_char = get_file_char ();
	}
    }
    if (radix == HEX)
    {
	valid_digit = TRUE;
	while (valid_digit)
	{
	    if ((file_char <= '9') && (file_char >= '0'))
	    {
		digit = digit * 16 + (file_char - 0x30);
		file_char = get_file_char ();
	    }
	    else if ((file_char <= 'F') && (file_char >= 'A'))
	    {
		digit = digit * 16 + (file_char - 55);
		file_char = get_file_char ();
	    }
	    else
	    {
		valid_digit = FALSE;
	    }
	}
    }
    if (radix == ASCII)
    {
	digit = (unsigned int) file_char;
    }
    return (digit);
}





get_function (buffer_ptr)
char buffer_ptr [];
{
    int func_ctr;

    for (func_ctr = 1; func_ctr < MAX_INIT_FUNCS; func_ctr++)
    {
	if (strcmp (buffer_ptr, func_ptr [func_ctr]) == 0)
	{
	    return (func_ctr);
	}
    }
    return (0);
}




/*     Subttl  Get Character from File*/
get_file_char ()
{
    register char file_char;

    file_eof_flag = FALSE;

    if (file_byte_ctr == 0)
    {
	file_byte_ctr = fread (&file_buffer [0], 1, 512, fptr);

	if (file_byte_ctr == 0)
	{
	    file_eof_flag = TRUE;

	    return (0);
	}
	else
	{
	    file_buffer_ptr = &file_buffer [0];
	}
    }

    file_char = *file_buffer_ptr++;
    file_byte_ctr--;

    return (file_char);
}



/*     Subttl  Put Character into File*/
put_file_char (file_char)
char file_char;
{
	if (fwrite (&file_char, 1, 1, fptr) != 1)
	{
	    return (0);
	}

    return (1);
}

