/*     Title   Status Windows and Functions */
/******************************************************************************/
/*				    Zifs				      */
/*			Status Windows and Functions			      */
/*									      */
/* Project .................... Allpro - Universal Programmer		      */
/* Module ..................... ZIF.C					      */
/* Title ...................... Status Windows and Functions		      */
/* Developing Engineer ........ Jun Flores				      */
/* Company .................... Logical Devices 			      */
/* Revision Level ............. v1.00					      */
/* Date of Last Revision ...... 					      */
/* Last Revision by ........... 					      */
/* Developing System .......... IBM PC or compatible			      */
/* Target System .............. IBM PC or compatible			      */
/* Language ................... C					      */
/* Compiler ................... Manx Aztec C v3.20e			      */
/* Compiler Command(s) ........ cc zifs 				      */
/*									      */
/* Interpreter ................ none					      */
/*									      */
/* Synopsis ................... This module contains the cycling status       */
/*				windows and the corresponding access	      */
/*				functions.				      */
/*									      */
/* Revision History:							      */
/*  v1.00   09-Sep-89		Original version - Jun F.		      */
/******************************************************************************/
/*     Subttl  Includes*/
#include <stdio.h>
#include "window.h"
#include "windows.h"
#include "color.h"
#include "keybrd.h"
#include "pindrivr.h"

/*	Subttl	Equates*/

#define TRUE	    1
#define FALSE	    0

char revision_level [] = "v3.5";
char cwd_path [] = "";
extern char command_buffer [];

#define MAX_ROWS	8
#define MAX_COLS	8

unsigned int row_offsets [MAX_ROWS] = {16, 14, 12, 10, 8, 6, 4, 2};
unsigned int col_offsets [MAX_COLS] = {6, 10, 14, 18, 22, 26, 30, 34};

unsigned char log_masks [MAX_ROWS] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char current_masks [MAX_ROWS] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char error_masks [MAX_ROWS] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char detect_masks [MAX_ROWS] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char bit0_buffer [MAX_ROWS];  /* will contain bits from a row/col of sockets */
unsigned char bit1_buffer [MAX_ROWS];
unsigned char bit2_buffer [MAX_ROWS];
unsigned char bit3_buffer [MAX_ROWS];
unsigned char bit4_buffer [MAX_ROWS];
unsigned char bit5_buffer [MAX_ROWS];
unsigned char bit6_buffer [MAX_ROWS];
unsigned char bit7_buffer [MAX_ROWS];

/* Status Windows */
struct window_def_struc status_window_table [MAX_WNDS] =
{
    {
	-1,
	2, 2, 24, 43,
	0, 0, 0,
	0x1e, 0x1f
    },
    {
	-1,
	2, 44, 24, 79,
	0, 0, 0,
	0x1e, 0x1f
    },
    {
	-1,
	2, 2, 24, 79,
	0, 0, 0,
	0x1e, 0x1f
    }
};

struct window_usage_struc status_display_table [MAX_WND_TYPES] =
{
    {
	CWD_ZIFS,
	1, 1, 12, 76,
	0,
	"Zif Sockets"
    },
    {
	CWD_AUX,
	1, 1, 12, 76,
	0,
	"Session Status"
    },
    {
	CWD_ZIFS,
	1, 1, 12, 76,
	0,
	"Log/Unlog Sockets"
    },
    {
	CWD_AUX,
	1, 1, 12, 76,
	0,
	"Messages/Inputs"
    },
    {
	CWD_ALL,
	1, 1, 22, 76,
	0,
	"ALLPRO / SE6440 Main Menu"
    },
    {
	CWD_ALL,
	1, 1, 22, 76,
	0,
	"ALLPRO / SE6440 Analog Test and Calibrate"
    },
    {
	CWD_AUX,
	1, 1, 12, 76,
	0,
	"Messages"
    },
    {
	CWD_ZIFS,
	1, 1, 12, 76,
	0,
	"Test Results"
    },
    {
	CWD_AUX,
	1, 1, 12, 76,
	0,
	"Messages/Inputs"
    },
    {
	CWD_ZIFS,
	1, 1, 12, 76,
	0,
	"Statistics"
    },
    {
	CWD_ALL,
	1, 1, 22, 76,
	0,
	"System Status"
    },
};

/* This function will draw 64 boxes. */

draw_sockets ()
{
    unsigned char row;
    unsigned char col;
    unsigned char tm[MAX_ROWS];

    clear_window (WND_ZIFS_STATUS);

    for(col = 0;col < MAX_COLS; col++)
    {
	write_col_number(col);
    }

    for (row = 0; row < MAX_ROWS; row++)	/* row counter */
    {
	write_row_number(row);

	for (col = 0; col < MAX_COLS; col++)	/* col counter */
	{
	    draw_zif (row, col, 0x1f);
	}
	tm[row] = 0xff;
    }
    deselect_zifs(tm);
}



write_row_number(row)
{
    write_window (WND_ZIFS_STATUS, row_offsets[row], 'L', "  %d", row+1);
}

write_col_number(col)
{
    pos_write_window (WND_ZIFS_STATUS,18,col_offsets[col], " %c", 'A' + col);
}

/* This function will be used to draw a zif socket.
    Enter with row and col numbers and the character
    attribute to be used. */

draw_zif (row, col, attr)
unsigned char row;
unsigned char col;
unsigned char attr;
{
    int trow, lcol;
    trow = row_offsets[row] + status_window_table[CWD_ZIFS].win_x_upper; /* row pos */
    lcol = col_offsets[col] + status_window_table[CWD_ZIFS].win_y_upper; /* col pos */

    video_write_char (trow, lcol, 1, attr, 201);
    video_write_char (trow, lcol+1, 1, attr, 205);
    video_write_char (trow, lcol+2, 1, attr, 187);
    video_write_char (trow+1, lcol, 1, attr, 200);
/*    video_write_char (trow+1, lcol+1, 1, attr, 205);*/
    video_write_char (trow+1, lcol+2, 1, attr, 188);
}

write_zif_led(row,col,ch,color)
unsigned char color,ch;
{
    int trow, lcol;
    int attr;
    attr = 0x18 | color;
    if(ch == ON) ch = 223;
    else
    {
	ch = 205;
    }
    trow = row_offsets[row] + status_window_table[CWD_ZIFS].win_x_upper; /* row pos */
    lcol = col_offsets[col] + status_window_table[CWD_ZIFS].win_y_upper; /* col pos */
    video_write_char (trow+1, lcol+1, 1, attr, ch);
}



/* This routine will be used to put characters onto the zif boxes.
    The character may be green to signify activity, or red
    to signify errors.

    Enter with masks of sockets to be updated and the color to
    display. */

update_zifs (masks, color)
unsigned char masks [];
unsigned char color;
{
    register unsigned char test_mask;
    register unsigned char bit_ctr;
    register unsigned char index;
    unsigned char attr;

    for (index = 0; index < MAX_ROWS; index++)	       /* process each row */
    {
	test_mask = 0x01;
	for (bit_ctr = 0; bit_ctr < MAX_COLS; bit_ctr++)      /* process each bit */
	{
	    if (masks [index] & test_mask)
	    {
		write_zif_led(index,bit_ctr,ON,color);
	    }
	    test_mask <<= 1;
	}
    }
}



/* This routine will be used to erase the characters from the zif boxes.
    Enter with masks of sockets to be restored. */

deselect_zifs (masks)
unsigned char masks [];
{
    register unsigned char test_mask;
    register unsigned char bit_ctr;
    register unsigned char index;
    unsigned char attr;

    attr = 0x11;  /* black foreground */

    for (index = 0; index < MAX_ROWS; index++)	       /* process each row */
    {
	test_mask = 0x01;
	for (bit_ctr = 0; bit_ctr < 8; bit_ctr++)      /* process each bit */
	{
	    if (masks [index] & test_mask)
	    {
		write_zif_led(index,bit_ctr,OFF,WHITE);
	    }
	    test_mask <<= 1;
	}
    }
}


/* This function will be used to determine if a socket mask is set. */

is_mask_set (masks, zif_row, zif_col)

unsigned char masks [];
unsigned char zif_row;
unsigned char zif_col;

{
    unsigned char state;
    unsigned char temp_mask;
    unsigned char bit_ctr;

    temp_mask = 0x01;
    temp_mask <<= zif_col;   /* position mask to specific bit */

    if (masks [zif_row] & temp_mask)
    {
	state = TRUE;
    }
    else
    {
	state = FALSE;
    }
    return (state);
}



/* This function will be used to set or reset a socket bit mask.
    Enter with socket's row and column position and the desired
    state for the bit. */

write_zif_mask (masks, zif_row, zif_col, state)

unsigned char masks [];
unsigned char zif_row;
unsigned char zif_col;
unsigned char state;

{
    unsigned char temp_mask;

    temp_mask = 0x01;
    temp_mask <<= zif_col;   /* position mask to specific bit */

    if (state == ON)	     /* set bit */
    {
	masks [zif_row] |= temp_mask;
    }
    else		     /* reset bit */
    {
	masks [zif_row] &= ~temp_mask;
    }
}


/* This function will be used to determine if sockets are selected. */
/*  It will return a TRUE if sockets are selected or FALSE if none  */
/*  is selected. */

selected_zifs ()
{
    unsigned char row;
    unsigned char skt_selected;

    skt_selected = FALSE;

    for (row = 0; (row < MAX_ROWS) && (skt_selected == FALSE); row++)
    {
	if (log_masks [row] != 0x00)
	{
	    skt_selected = TRUE;
	}
    }
    return (skt_selected);
}

write_enable_regs(masks)
unsigned char masks[];
{
/*
    udi_write_mem(ENABLE_REG_ALL,0xff);
    udi_write_mem(ANALOG_CONTROL,2);
*/
    udi_write_mem(ENABLE_REG_0,masks[0]);
    udi_write_mem(ENABLE_REG_1,masks[1]);
    udi_write_mem(ENABLE_REG_2,masks[2]);
    udi_write_mem(ENABLE_REG_3,masks[3]);
    udi_write_mem(ENABLE_REG_4,masks[4]);
    udi_write_mem(ENABLE_REG_5,masks[5]);
    udi_write_mem(ENABLE_REG_6,masks[6]);
    udi_write_mem(ENABLE_REG_7,masks[7]);
/*    udi_write_mem(ANALOG_CONTROL,6);*/
}



static int ctop, cbot;
static int cur_cnt = 0;

hide_cursor ()
{
    int temp;
    if(!cur_cnt)
    {
	temp = crt_get_cur_pos(0);
	ctop = temp >> 8;
	cbot = temp & 0xff;
	crt_cursor_type (32, 32);
    }
    cur_cnt++;
}

show_cursor ()
{
    if(--cur_cnt == 0)
	crt_cursor_type (ctop, cbot);
}

