/*	Title	Global Data*/
/******************************************************************************/
/*				    Diagsys				      */
/*			      Allpro diagnostic 3			      */
/*									      */
/* Project .................... Allpro - Universal Programmer		      */
/* Module ..................... DIAGSYS.C				      */
/* Title ...................... Allpro diagnostic 3			      */
/* Developing Engineer ........ Steven T. Harr				      */
/* Company .................... Logical Devices 			      */
/* Revision Level ............. v3.00					      */
/* Date of Last Revision ...... 08-09-90				      */
/* Last Revision by ........... STH					      */
/* Developing System .......... IBM PC or compatible			      */
/* Target System .............. IBM PC or compatible			      */
/* Language ................... C					      */
/* Compiler ................... Manx Aztec C v3.20e			      */
/* Compiler Command(s) ........ cc Diag3				      */
/*									      */
/* Interpreter ................ none					      */
/*									      */
/* Synopsis ................... This module contains the system configuration */
/*				test and display routines.		      */
/*									      */
/* Revision History:							      */
/*  v3.00   08-09-90		Original version - STH			      */
/*									      */
/*									      */
/******************************************************************************/
/*	Subttl	Includes    */
#include <stdio.h>
#include "diag2.h"
#include "window.h"
#include "pindrivr.h"


struct system_struc system_info;
extern struct PC_equip Installed_equip;

extern int real_pin [];
extern unsigned char analog_id;
extern unsigned int max_pins;


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

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



/*
 *  This function fills the system structure with all of the
 *  system information.
 */
get_system_info()
{
    int i;

    pump ();
    get_analog_id();
    system_info.analog_id = analog_id;
    system_info.module_id = udi_read_mem(0x280);
    half_pump ();
    delay_ms (1);
    system_info.interface_type = 0; /* default */
    if (detect_tic () == TRUE)
    {
	system_info.interface_type = 1;
	system_info.interface_vers = get_tic_version();
    }
    if (system_info.analog_id == 0x03)
    {
	system_info.pindriver_type = 8;
	max_pins = 88;
    }
    else if (system_info.analog_id == 0x02)
    {
	system_info.pindriver_type = 4;
	max_pins = 40;
    }
    else
    {
	system_info.pindriver_type = 0;
	max_pins = 40; /* default to 40 pin unit */
    }
    system_info.pindrivers = 0;
    /*if(system_info.pindriver_type > 0)*/
    {
	reset_allpro();
	setup_supplies (1000, 250, 500, 100);
	for(i=1;i<= 88;i++)
	{
	    set_pin_cnf(i,GD_DRIVER_ON);
	    if (!read_pin (i))
	    {
		system_info.pindrivers++;
		real_pin[i] = 1;
	    }
	    else
		real_pin[i] = 0;
	}
    }
    unpump ();
}


init_equip_struc()
{
    int i, j;

    strcpy(Installed_equip.CPU_speed	 ,"????");
    strcpy(Installed_equip.EMS_page_frame," N/A");

    get_installed_equip();

}


show_system_info()
{
    int col1 = 5;
    int row1 = 2;
    int col2 = 45;
    int row2 = 2;
    int row3 = 13;
    int col3 = 55;
    int row4 = 10;
    int col4 = 5;
    int i, j;
    char get_hex_val();

    get_system_info();
    get_installed_equip();
    setup_window(WND_SYSTEM_STATUS);
    pos_write_window (WND_SYSTEM_STATUS, row1, col1,   "Interface type:     %s",
	system_info.interface_type?"TURBO":"?????");
    pos_write_window (WND_SYSTEM_STATUS, row1+1, col1, "Interface Vers:     %5x",
	system_info.interface_vers);
    pos_write_window (WND_SYSTEM_STATUS, row1+2, col1, "System Id:          %5x",
	system_info.analog_id & 0xf);
    pos_write_window (WND_SYSTEM_STATUS, row1+3, col1, "Adapter Id:         %5x",
	system_info.module_id & 0xff);
    pos_write_window (WND_SYSTEM_STATUS, row1+4, col1, "Pindrivers:         %5d",
	system_info.pindrivers);
 /*   pos_write_window (WND_SYSTEM_STATUS, row1+5, col1, "Pindriver Type:     %s",
	system_info.pindriver_type==8?"OCTAL":" QUAD");
  */

    /* show PC equipment structure */
    pos_write_window (WND_SYSTEM_STATUS, row2, col2,   "DOS Version:            %4s",
	Installed_equip.DOS_version);
    pos_write_window (WND_SYSTEM_STATUS, row2+1, col2,	 "Video Adapter:          %4s",
	Installed_equip.video_adaptor);
    pos_write_window (WND_SYSTEM_STATUS, row2+2, col2, "Video Memory:          %5s",
	Installed_equip.video_mem);
    pos_write_window (WND_SYSTEM_STATUS, row2+3, col2, "CPU Type:           %8s",
	Installed_equip.CPU_type);
    pos_write_window (WND_SYSTEM_STATUS, row2+4, col2, "CPU Speed:            %6s",
	Installed_equip.CPU_speed);
    pos_write_window (WND_SYSTEM_STATUS, row2+5, col2, "EMS Version:       %9s",
	Installed_equip.EMS_version);
    pos_write_window (WND_SYSTEM_STATUS, row2+6, col2, "EMS Page Frame:        %5s",
	Installed_equip.EMS_page_frame);
    pos_write_window (WND_SYSTEM_STATUS, row2+7, col2, "Conventional Memory:   %5s",
	Installed_equip.conv_mem);
    pos_write_window (WND_SYSTEM_STATUS, row2+8, col2, "Extended Memory:     %7s",
	Installed_equip.ext_mem);

    /* display the PC memory map */
    pos_write_window (WND_SYSTEM_STATUS, row3, col3, "PC Memory Map:");
    for(i=0;i<16;i++)
	pos_write_window (WND_SYSTEM_STATUS, row3+1, col3+i+2, "%c", get_hex_val(i));
    for(i=0;i<6;i++)
    {
	pos_write_window (WND_SYSTEM_STATUS, row3+2+i, col3, "%c", 'A'+i);
	for(j=0;j<16;j++)
	{

	    pos_write_window (WND_SYSTEM_STATUS, row3+2+i, col3+j+2, "%c",
		Installed_equip.mem_map[i][j]);
	}
    }

    /* display the pindriver groups */
    pos_write_window (WND_SYSTEM_STATUS, row4, col4, "Pin Driver Cards:");
    if(system_info.analog_id == 3)
    {
	for(i=0;i<11;i++)
	    pos_write_window (WND_SYSTEM_STATUS, row4+1, col4+i*3+3, " %d", i);
	for(i=0;i<8;i++)
	{
	    pos_write_window (WND_SYSTEM_STATUS, row4+2+i, col4, "%c", 'A'+i);
	    for(j=0;j<11;j++)
	    {
		pos_write_window (WND_SYSTEM_STATUS, row4+2+i, col4+j*3+3, " %c",
		    real_pin[pin_group_3[j][i]]?'X':'-');
	    }
	}
    }
    else if(system_info.analog_id == 2)
    {
	for(i=0;i<10;i++)
	    pos_write_window (WND_SYSTEM_STATUS, row4+1, col4+i*3+3, " %d", i);
	for(i=0;i<4;i++)
	{
	    pos_write_window (WND_SYSTEM_STATUS, row4+2+i, col4, "%c", 'A'+i);
	    for(j=0;j<10;j++)
	    {
		pos_write_window (WND_SYSTEM_STATUS, row4+2+i, col4+j*3+3, " %c",
		    real_pin[pin_group_2[j][i]]?'X':'-');
	    }
	}
    }
    else
    {
	pos_write_window (WND_SYSTEM_STATUS, row4, col4 + 20, "NONE");
    }
    show_cursor();
    pos_write_window (WND_SYSTEM_STATUS, 21, 5, "Save status on disk (Y/N)?");
    if((toupper(keybrd_getc())) == 'Y') print_system_info();
    hide_cursor();
}

print_system_info()
{
    int col1 = 5;
    int row1 = 2;
    int col2 = 40;
    int row2 = 2;
    int row3 = 11;
    int col3 = 50;
    int row4 = 10;
    int col4 = 5;
    int i, j;
    char get_hex_val();
    FILE *fp, *fopen();


    if ((fp = fopen("DIAGSTAT","w")) == NULL)
    {
	pos_write_window (WND_SYSTEM_STATUS,23,8,"Can't open file 'DIAGSTAT'");
	while (keyboard_status() == -1){}
	return;
    }

    fprintf (fp,"\n\tInterface type:     %s", system_info.interface_type?"TURBO":"?????");
    fprintf (fp,"\n\tInterface Vers:     %5x", system_info.interface_vers);
    fprintf (fp,"\n\tSystem Id:          %5x", system_info.analog_id & 0xf);
    fprintf (fp,"\n\tAdapter Id:         %5x", system_info.module_id & 0xff);
    fprintf (fp,"\n\tPindrivers:         %5d", system_info.pindrivers);
/*    fprintf (fp,"\n\tPindriver Type:     %s", system_info.pindriver_type==8?"OCTAL":" QUAD");*/

    fprintf (fp,"\n\n\t");

    /* show PC equipment structure */
    fprintf (fp,"\n\tDOS Version:            %4s", Installed_equip.DOS_version);
    fprintf (fp,"\n\tVideo Adapter:          %4s", Installed_equip.video_adaptor);
    fprintf (fp,"\n\tVideo Memory:          %5s", Installed_equip.video_mem);
    fprintf (fp,"\n\tCPU Type:             %6s", Installed_equip.CPU_type);
    fprintf (fp,"\n\tCPU Speed:             %5s", Installed_equip.CPU_speed);
    fprintf (fp,"\n\tEMS Version:       %9s", Installed_equip.EMS_version);
    fprintf (fp,"\n\tEMS Page Frame:        %5s", Installed_equip.EMS_page_frame);
    fprintf (fp,"\n\tConventional Memory:   %5s", Installed_equip.conv_mem);
    fprintf (fp,"\n\tExtended Memory:     %7s", Installed_equip.ext_mem);

    fprintf (fp,"\n\n\n\t\n\t");

    /* display the PC memory map */
    fprintf (fp,"\n\tPC Memory Map:");
    fprintf (fp,"\n\t       ");
    for(i=0;i<16;i++) fprintf (fp,"%c", get_hex_val(i));
    for(i=0;i<6;i++)
    {
	fprintf (fp,"\n\t    %c  ", 'A'+i);
	for(j=0;j<16;j++) fprintf (fp,"%c", Installed_equip.mem_map[i][j]);
    }

    /* display the pindriver groups */
    if(system_info.analog_id == 3)
    {
	fprintf (fp,"\n\n\n\tPin Driver Cards:");
	fprintf (fp,"\n\t       ");
	for(i=0;i<11;i++) fprintf (fp," %d", i);
	for(i=0;i<8;i++)
	{
	    fprintf (fp,"\n\t    %c  ", 'A'+i);
	    for(j=0;j<11;j++) fprintf (fp," %c", real_pin[pin_group_3[j][i]]?'X':'-');
	}
    }
    else if(system_info.analog_id == 2)
    {
	fprintf (fp,"\n\n\n\tPin Driver Cards:");
	fprintf (fp,"\n\t       ");
	for(i=0;i<10;i++) fprintf (fp," %d", i);
	for(i=0;i<4;i++)
	{
	    fprintf (fp,"\n\t    %c  ", 'A'+i);
	    for(j=0;j<10;j++) fprintf (fp," %c", real_pin[pin_group_2[j][i]]?'X':'-');
	}
    }
    else
    {
	fprintf (fp,"\n\n\n\tPin Driver Cards:   NONE\n");
    }
    fprintf(fp,"\n\n");
    fclose(fp);
}

char get_hex_val(x)
int x;
{
    x &= 0x0f;
    if(x >= 0 && x <= 9) return '0'+x;
    else return 'A'+ (x - 10);
}

