/*     Title   Zif Editor */
/******************************************************************************/
/*				   Zifedit				      */
/*				  Zif Editor				      */
/*									      */
/* Project .................... SE6440 - Gang Programmer		      */
/* Module ..................... zif_edit				      */
/* Title ...................... Zif Editor				      */
/* Developing Engineer ........ Jun Flores				      */
/* Company .................... Logical Devices 			      */
/* Revision Level ............. 					      */
/* 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 zifedit				      */
/*									      */
/* Interpreter ................ none					      */
/*									      */
/* Synopsis ................... This module contains the function(s) to       */
/*				log and unlog the sockets.		      */
/*									      */
/* Revision History:							      */
/*  v1.00   26-Sep-89		Original version - Jun Flores		      */
/******************************************************************************/
/*     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


static zif_row = 0;
static zif_col = 0;

extern char revision_level [];
extern char cwd_path [];
extern char command_buffer [];
extern se6440_flag;
extern int selected_zif_row;
extern int selected_zif_col;
extern unsigned char current_masks[];

#define MAX_ROWS    8
#define MAX_COLS    8

extern unsigned char log_masks [];
unsigned char tst_masks [MAX_ROWS];

extern unsigned int row_offsets [];
extern unsigned int col_offsets [];

extern unsigned int array_ctr;

/* Status Windows */
extern struct window_def_struc status_window_table [];
extern struct window_usage_struc status_display_table [];

log_zifs ()
{
    unsigned char ctr;

    if(!se6440_flag) return;
    hide_cursor ();

    initialize_windows (status_window_table, status_display_table, MAX_WNDS, "SE6440 - Universal Gang Programmer Diagnostic - %s", revision_level);
    setup_window (WND_EDIT_ZIFS);
    setup_window (WND_ZIFS_PROMPT);

    draw_sockets ();
    update_zifs (log_masks, GREEN);

    write_window (WND_ZIFS_PROMPT,  2, 'L', "  Function Keys for Editing");
    write_window (WND_ZIFS_PROMPT,  4, 'L', "  F1 - Log a socket.");
    write_window (WND_ZIFS_PROMPT,  5, 'L', "  F2 - Unlog a socket.");
    write_window (WND_ZIFS_PROMPT,  7, 'L', "  F3 - Log a row of sockets.");
    write_window (WND_ZIFS_PROMPT,  8, 'L', "  F4 - Unlog a row of sockets.");
    write_window (WND_ZIFS_PROMPT, 10, 'L', "  F5 - Log a column of sockets.");
    write_window (WND_ZIFS_PROMPT, 11, 'L', "  F6 - Unlog a column of sockets.");
    write_window (WND_ZIFS_PROMPT, 13, 'L', "  F7 - Log all sockets.");
    write_window (WND_ZIFS_PROMPT, 14, 'L', "  F8 - Unlog all sockets.");

    write_window (WND_ZIFS_PROMPT, 16, 'L', "  F9 - Save Log Information.");
    write_window (WND_ZIFS_PROMPT, 17, 'L', "  F10 - Recall Log Information.");

    write_window (WND_ZIFS_PROMPT, 19, 'L', "  S-F10 - Display Logged Sockets.");

    write_window (WND_ZIFS_PROMPT, 21, 'L', "  ESC - Exit edit mode.");

    edit_sockets ();
    show_cursor();
    return (TRUE);
}

select_zif_socket()
{
    int key, exit_flag;

    hide_cursor ();

    initialize_windows (status_window_table, status_display_table, MAX_WNDS, "SE6440 - Universal Gang Programmer Diagnostic - %s", revision_level);
    setup_window (WND_ZIFS_STATUS);
    setup_window (WND_ZIFS_PROMPT);

    draw_sockets ();
    clear_masks(current_masks);
    write_zif_mask (current_masks, selected_zif_row, selected_zif_col, ON);

    write_window (WND_ZIFS_PROMPT,  4, 'L', "  Use Arrow Keys for selection.");
    write_window (WND_ZIFS_PROMPT,  6, 'L', "  F1 - Select Zif Socket.");
    write_window (WND_ZIFS_PROMPT,  8, 'L', "  ESC - Exit.");

    exit_flag = FALSE;
    write_zif_led(selected_zif_row,selected_zif_col,ON,GREEN);
    zif_row = selected_zif_row;
    zif_col = selected_zif_col;
    draw_cursor(selected_zif_row,selected_zif_col);

    while (exit_flag == FALSE)
    {
	key = keybrd_getc ();

	switch (key)
	{
	    case KEY_CURSOR_DOWN:
	    case KEY_CURSOR_UP:
	    case KEY_CURSOR_LEFT:
	    case KEY_CURSOR_RIGHT:
	    {
		move_cursor(key);
		break;
	    }
	    case KEY_ESC:
	    {
		exit_flag = TRUE;
		break;
	    }
	    case KEY_F1:
	    {
		write_zif_mask (current_masks, selected_zif_row, selected_zif_col, OFF);
		write_zif_mask (current_masks, zif_row, zif_col, ON);
		write_zif_led(selected_zif_row,selected_zif_col,OFF,WHITE);
		write_zif_led(zif_row,zif_col,ON,GREEN);
		selected_zif_row = zif_row;
		selected_zif_col = zif_col;
		select_zif(zif_row,zif_col);
		break;
	    }
	}
    }

    show_cursor();
    return (TRUE);
}

/* This function will be used to blink a socket to act as
    a cursor during the editing of sockets. */

draw_cursor (zif_row, zif_col)
unsigned char zif_row;
unsigned char zif_col;
{
    draw_zif (zif_row, zif_col, 0x9e);
    if (is_mask_set (current_masks, zif_row, zif_col))
    {
	write_zif_led(zif_row,zif_col,ON,0xa);
    }
    else write_zif_led(zif_row,zif_col,OFF,0x9e);
}

/* This function will be used to restore a socket from blinking to
    the previous state. */

undraw_cursor (zif_row, zif_col)
unsigned char zif_row;
unsigned char zif_col;
{
    draw_zif (zif_row, zif_col, 0x1f);
    if (is_mask_set (current_masks, zif_row, zif_col))
    {
	write_zif_led(zif_row,zif_col,ON,0xa);
    }
    else write_zif_led(zif_row,zif_col,OFF,WHITE);
}


/* This function will be used to log and unlog the zif sockets. */

edit_sockets ()
{
    unsigned char exit_flag;
    unsigned int key;
    unsigned char ctr;
    unsigned char row_ctr;
    unsigned char col_ctr;

    unsigned char log_file_path [80];
    unsigned char temp_buffer [80];
    FILE *fptr;

    exit_flag = FALSE;
    zif_row = 0;
    zif_col = 0;

    copy_masks(current_masks,log_masks);
    draw_cursor (zif_row, zif_col);

    while (exit_flag == FALSE)
    {
	key = keybrd_getc ();

	switch (key)
	{
	    case KEY_CURSOR_DOWN:
	    case KEY_CURSOR_UP:
	    case KEY_CURSOR_LEFT:
	    case KEY_CURSOR_RIGHT:
	    {
		move_cursor(key);
		break;
	    }

	    case KEY_F1:	/* log a socket */
	    {
		write_zif_mask (current_masks, zif_row, zif_col, ON);
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F3:	/* log a row of sockets */
	    {
		for (ctr = 0; ctr < MAX_COLS; ctr++)
		{
		    write_zif_mask (current_masks, zif_row, ctr, ON);
		    undraw_cursor (zif_row, ctr);
		}
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F5:	/* log a column of sockets */
	    {
		for (ctr = 0; ctr < MAX_ROWS; ctr++)
		{
		    write_zif_mask (current_masks, ctr, zif_col, ON);
		    undraw_cursor (ctr, zif_col);
		}
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F2:	/* unlog a socket */
	    {
		write_zif_mask (current_masks, zif_row, zif_col, OFF);
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F4:	/* unlog a row of sockets */
	    {
		for (ctr = 0; ctr < MAX_COLS; ctr++)
		{
		    write_zif_mask (current_masks, zif_row, ctr, OFF);
		    undraw_cursor (zif_row, ctr);
		}
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F6:	/* unlog a column of sockets */
	    {
		for (ctr = 0; ctr < MAX_ROWS; ctr++)
		{
		    write_zif_mask (current_masks, ctr, zif_col, OFF);
		    undraw_cursor (ctr, zif_col);
		}
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F7:	/* log all sockets */
	    {
		for (row_ctr = 0; row_ctr < MAX_ROWS; row_ctr++)
		{
		    for(col_ctr = 0;col_ctr < MAX_COLS; col_ctr++)
		    {
			write_zif_mask (current_masks, row_ctr, col_ctr, ON);
			undraw_cursor (row_ctr, col_ctr);
		    }
		}
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F8:	/* unlog all sockets */
	    {
		for (row_ctr = 0; row_ctr < MAX_ROWS; row_ctr++)
		{
		    for(col_ctr = 0;col_ctr < MAX_COLS; col_ctr++)
		    {
			write_zif_mask (current_masks, row_ctr, col_ctr, OFF);
			undraw_cursor (row_ctr, col_ctr);
		    }
		}
		write_enable_regs(current_masks);
		draw_cursor (zif_row, zif_col);
		break;
	    }

	    case KEY_F9:	/* save log information */
	    {
		strcpy (log_file_path, cwd_path);
		strcat (log_file_path, "LOG.SKT");

		write_window (WND_ZIFS_PROMPT, 20, 'L', "Replace Existing Log File? ");
		key = keybrd_getc ();
		clear_window_line (WND_ZIFS_PROMPT, 20);

		if ((key == 'y') || (key == 'Y'))
		{
		    if (!(fptr = fopen (log_file_path, "w")))
		    {
			write_window (WND_ZIFS_PROMPT, 20, 'L', "File Open Error.");
			write_window (WND_ZIFS_PROMPT, 21, 'L', "Press any key to continue.");
			while (keyboard_status () == -1){}
			clear_window_line (WND_ZIFS_PROMPT, 20);
			clear_window_line (WND_ZIFS_PROMPT, 21);
		    }
		    else
		    {
			fprintf (fptr, "%02x ", current_masks [0]);
			fprintf (fptr, "%02x ", current_masks [1]);
			fprintf (fptr, "%02x ", current_masks [2]);
			fprintf (fptr, "%02x ", current_masks [3]);
			fprintf (fptr, "%02x ", current_masks [4]);
			fprintf (fptr, "%02x ", current_masks [5]);
			fprintf (fptr, "%02x ", current_masks [6]);
			fprintf (fptr, "%02x ", current_masks [7]);
			fprintf (fptr, "  ;log enable register bits\n");
			if (fclose (fptr) != 0)
			{
			    write_window (WND_ZIFS_PROMPT, 20, 'L', "File Close Error.");
			    write_window (WND_ZIFS_PROMPT, 21, 'L', "Press any key to continue.");
			    while (keyboard_status () == -1){}
			    clear_window_line (WND_ZIFS_PROMPT, 20);
			    clear_window_line (WND_ZIFS_PROMPT, 21);
			}
		    }
		}
		break;
	    }

	    case KEY_F10:	 /* recall log information */
	    {
	       for (ctr = 0; ctr < 80; ctr++)
	       {
		   temp_buffer [ctr] = 0x00;
	       }

		strcpy (log_file_path, cwd_path);
		strcat (log_file_path, "LOG.SKT");

		if (!(fptr = fopen (log_file_path, "r")))
		{
		    write_window (WND_ZIFS_PROMPT, 20, 'L', "File Open Error.");
		    write_window (WND_ZIFS_PROMPT, 21, 'L', "Press any key to continue.");
		    while (keyboard_status () == -1){}
		    clear_window_line (WND_ZIFS_PROMPT, 20);
		    clear_window_line (WND_ZIFS_PROMPT, 21);
		}
		else
		{
		    /* clear sockets */
		    for (ctr = 0; ctr < MAX_ROWS; ctr++)
		    {
			tst_masks [ctr] = 0xff;
		    }
		    deselect_zifs (tst_masks);

		    if (!(fgets (temp_buffer, 80, fptr)))
		    {
			write_window (WND_ZIFS_PROMPT, 20, 'L', "File Read Error.");
			write_window (WND_ZIFS_PROMPT, 21, 'L', "Press any key to continue.");
			while (keyboard_status () == -1){}
			clear_window_line (WND_ZIFS_PROMPT, 20);
			clear_window_line (WND_ZIFS_PROMPT, 21);
		    }
		    else
		    {
			sscanf (temp_buffer, "%x %x %x %x %x %x %x %x", &current_masks [0], &current_masks [1], &current_masks [2], &current_masks [3],
				&current_masks [4], &current_masks [5], &current_masks [6], &current_masks [7]);

			update_zifs (current_masks, GREEN);   /* update screen */

			if (fclose (fptr) != 0)
			{
			    write_window (WND_ZIFS_PROMPT, 20, 'L', "File Close Error.");
			    write_window (WND_ZIFS_PROMPT, 21, 'L', "Press any key to continue.");
			    while (keyboard_status () == -1){}
			    clear_window_line (WND_ZIFS_PROMPT, 20);
			    clear_window_line (WND_ZIFS_PROMPT, 21);
			}
		    }
		}
		break;
	    }
	    case KEY_SHIFT_F10:
	    {
		cpl_masks(current_masks);
		write_enable_regs(current_masks);
		udi_write_mem(ANALOG_CONTROL,GREEN_LED_OFF_BIT);
		cpl_masks(current_masks);
		write_enable_regs(current_masks);
		udi_write_mem(ANALOG_CONTROL,0);
		break;
	    }

	    case KEY_ESC:
	    {
		exit_flag = TRUE;
		break;
	    }
	}
    }
    copy_masks(log_masks,current_masks);

    return ();
}


move_cursor(key)
int key;
{
    switch(key)
    {
	case KEY_CURSOR_DOWN:
	{
	    undraw_cursor (zif_row, zif_col);
	    if (zif_row == 0)
	    {
		zif_row = 7;
	    }
	    else
	    {
		zif_row--;
	    }
	    draw_cursor (zif_row, zif_col);
	    break;
	}

	case KEY_CURSOR_UP:
	{
	    undraw_cursor (zif_row, zif_col);
	    if (zif_row == 7)
	    {
		zif_row = 0;
	    }
	    else
	    {
		zif_row++;
	    }
	    draw_cursor (zif_row, zif_col);
	    break;
	}

	case KEY_CURSOR_LEFT:
	{
	    undraw_cursor (zif_row, zif_col);
	    if (zif_col == 0)
	    {
		zif_col = 7;
	    }
	    else
	    {
		zif_col--;
	    }
	    draw_cursor (zif_row, zif_col);
	    break;
	}

	case KEY_CURSOR_RIGHT:
	{
	    undraw_cursor (zif_row, zif_col);
	    if (zif_col == 7)
	    {
		zif_col = 0;
	    }
	    else
	    {
		zif_col++;
	    }
	    draw_cursor (zif_row, zif_col);
	    break;
	}
    }
}


