/*     Title   All-Pro Diagnostics Editor */
/******************************************************************************/
/*                                   Dedit                                    */
/*                             All-pro Diagnostic Editor                      */
/*                                                                            */
/* Module ...................... DEDIT                                        */
/* Title ....................... All-pro Diagnostic Editor                    */
/* Developing Engineer ......... Steven T. Harr                               */
/* Company ..................... Logical Devices                              */
/* Revision Level .............. 1.00                                         */
/* Date of Last Revision ....... 25-Nov-86                                    */
/* 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 Diag2                                      */
/*                                                                            */
/* Interpreter ................ none                                          */
/*                                                                            */
/* Synopsis ................... This module contains the Diagnostic Editor    */
/*                              commands and the Program execution control.   */
/*                                                                            */
/* Revision History:                                                          */
/*  v1.00   25-Nov-86           Original version - STH                        */
/*                                                                            */
/******************************************************************************/
/*     Subttl  Includes*/
#include <stdio.h>
#include <ctype.h>
#include "cmdfile.h"
#include "keybrd.h"
#include "diag2.h"

/*     Subttl  Defines*/
#define TRUE                            1
#define FALSE                           0


#define END_LIST    -1
#define MAX_ROWS    10
#define MAX_PROG_ROWS 30
#define MAX_COLS    6
#define COMMAND     1
#define PARAMETER   2
#define ON          1
#define OFF         0
#define TOP_ROW     3
#define MAX_PARMS   10

#define COMMAND_WINDOW_COL 69

#define TAG_NULL                0x00
#define TAG_EMPTY               0xff

int func_print ();



/*     Subttl  Structures*/
struct parameter
{
    int col;
    char strng [9];
};


struct cmd_struc
{
    char *cmd_tag;          /* pointer to the ascii tag string */
    int (*func) ();         /* pointer to function to call */
    struct parameter parm [MAX_PARMS] ;         /* pointer to a table of parameters */
};


struct cmd_struc commands [] =
{
    {"        ", *func_print,
      0, "      " ,
      1, "      " ,
      2, "      " ,
      3, "      " ,
      4, "      " ,
      5, "      " ,
      END_LIST },

    {"PRINT   ", *func_print,
      0, "______" ,
      1, "ASCII ",
      END_LIST, "" },

    {"WRDAC   ", *func_print,
      0, "______" ,
      1, "THRESH" ,
      1, "OVC   " ,
      1, "SUPPLY" ,
      2, "VALUE " ,
      END_LIST, "" },

    {"WRPINC  ", *func_print,
      0, "______" ,
      1, "PIN # " ,
      2, "STATE " ,
      END_LIST, "" },

    {"WRPIND  ", *func_print,
      0, "______" ,
      1, "PIN # " ,
      2, "VOLTAG" ,
      END_LIST, "" },

    {"        ", *func_print ,
      END_LIST },
    {"        ", *func_print ,
      END_LIST },
    {"        ", *func_print ,
      END_LIST },
    {"        ", *func_print ,
      END_LIST },
    {"        ", *func_print ,
      END_LIST },
    { NULL }
};


/*     Subttl  Globals */
unsigned int command_buffer [MAX_PROG_ROWS] [MAX_COLS];
unsigned char command_tag [MAX_PROG_ROWS] [MAX_COLS];

int cold_start = TRUE;
int page_top_row;


int tab_stop [] =
{
    5, 14, 23, 32, 41, 50, 59
};



/*     Subttl  Modify User Program */
modify_program ()
{
    int i;
    int ctr;
    int row;
    int col;
    int pick;
    int max_pick = 9;
    int break_flag;
    int advance_flag;
    int key_char;
    int command;
    int screen_update_flag;

    row = 0;
    page_top_row = 0;
    col = 0;
    print_edit_screen ();
    command = command_buffer [0] [0];

    break_flag = FALSE;
    while ( break_flag == FALSE )
    {
        if ( col == 0 )
        {
            for ( i = 0; commands [i].cmd_tag != NULL; i++ )
            {
                crt_pos_cursor ( i + TOP_ROW, COMMAND_WINDOW_COL, 0 );
                printf ( "%1d %s", i, commands [i].cmd_tag );
            }
        }
        else
        {
            print_pick_list ( command, col );
        }

        advance_flag = FALSE;
        screen_update_flag = FALSE;
        pick = 0;
        while ( screen_update_flag == FALSE && break_flag == FALSE )
        {
            crt_pos_cursor ( TOP_ROW + row - page_top_row, tab_stop [col], 0 );
            key_char = keybrd_getc ();
            switch ( key_char )
            {

                case KEY_PAGE_UP:
                {
                    if ((col == 0) && (page_top_row > 0))
                    {
                        if ((row -= 8) < 0)
                        {
                            row = 0;
                        }
                        if ((page_top_row -= 8) < 0)
                        {
                            page_top_row = 0;
                        }
                        command = command_buffer [row] [0];
                        print_main_window (page_top_row);
                    }
                }
                break;

                case KEY_PAGE_DOWN:
                {
                    if ((col == 0) && (page_top_row < (MAX_PROG_ROWS - MAX_ROWS)))
                    {
                        if ((row += 8) > MAX_PROG_ROWS)
                        {
                            row = MAX_PROG_ROWS - 1;
                        }
                        if ((page_top_row += 8) > (MAX_PROG_ROWS - MAX_ROWS))
                        {
                            page_top_row = MAX_PROG_ROWS - MAX_ROWS;
                        }
                        command = command_buffer [row] [0];
                        print_main_window (page_top_row);
                    }
                }
                break;

                case KEY_CURSOR_UP:
                {
                    if (col == 0)
                    {
                        if (row > page_top_row)
                        {
                            row--;
                        }
                        else if (page_top_row > 0)
                        {
                            row--;
                            page_top_row--;
                            print_main_window (page_top_row);
                        }
                        command = command_buffer [row] [0];
                    }
                }
                break;

                case KEY_CURSOR_DOWN:
                {
                    if (col == 0)
                    {
                        if (row < (page_top_row + MAX_ROWS - 1))
                        {
                            row++;
                        }
                        else if (page_top_row < (MAX_PROG_ROWS - MAX_ROWS - 1))
                        {
                            row++;
                            page_top_row++;
                            print_main_window (page_top_row);
                        }
                        command = command_buffer [row] [0];
                    }
                }
                break;

                case KEY_CURSOR_LEFT:
                {
                    if ( col > 0 )
                    {
                        col--;
                        screen_update_flag = TRUE;
                    }
                }
                break;

                case KEY_CURSOR_RIGHT:
                {
                    if ( col < MAX_COLS )
                    {
                        if ( find_param (command, 0, col + 1) != -1)
                        {
                            col++;
                            screen_update_flag = TRUE;
                        }
                    }
                }
                break;

                case KEY_CR:
                {
                    print_main_window (page_top_row);
                    screen_update_flag = TRUE;
                }
                break;

                case KEY_ESC:
                {
                    break_flag = TRUE;
                }
                break;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                {
                    pick = ( key_char & 0xff ) - 0x30;

                    if (col == 0)
                    {
                        if ( pick > max_pick )
                        {
                            pick = 0;
                        }
                        if (command_buffer [row] [0] != pick)
                        {
                            delete_line (row);
                            insert_line (row);
                        }
                        command_buffer [row] [0] = command = pick;
                        for (ctr = get_parameter_size (command); ctr > 0; ctr--)
                        {
                            command_buffer [row] [ctr] = TAG_EMPTY;
                        }
                        print_line (row);
                    }
                    else
                    {
                        if ((pick = find_param (command, pick, col)) != -1)
                        {
                            printf ("%s", commands [command].parm [pick].strng);
                            command_tag [row] [col] = TRUE;
                        }
                    }
                }
                break;

            }
        }

        /* advance to next screen position */
    }
}


insert_line (line_num)
int line_num;
{
    int i;
    int j;

    for (i = line_num + 1; i < (MAX_PROG_ROWS - 1); i++)
    {
        for (j = 0; j < MAX_COLS; j++)
        {
            command_buffer [i] [j] = command_buffer [line_num] [j];
        }
    }
    for (j = 0; j < MAX_COLS; j++)
    {
        command_buffer [line_num] [j] = TAG_NULL;
    }
}



delete_line (line_num)
int line_num;
{
    int i;
    int j;

    for (i = line_num + 1; i < (MAX_PROG_ROWS - 1); i++)
    {
        for (j = 0; j < MAX_COLS; j++)
        {
            command_buffer [line_num] [j] = command_buffer [i] [j];
        }
    }
    for (j = 0; j < MAX_COLS; j++)
    {
        command_buffer [MAX_PROG_ROWS - 1] [j] = TAG_NULL;
    }
}



find_param (cmnd, row, field)
int cmnd;
int row;
int field;
{
    int i;

    i = 0;
    do
    {
        if (commands [cmnd].parm [i].col == END_LIST)
        {
            return (-1);
        }
        else if (commands [cmnd].parm [i].col == field)
        {
            row--;
        }
        i++;
    }
    while (row != -1);
    return (i - 1);
}

get_parameter_size (command)
int command;
{
    int i;

    for (i = 0; (i < MAX_PARMS) && (find_param (command, 0, i) != -1); i++);
    return (i);
}



print_pick_list ( cmnd, field )
int cmnd;
int field;
{
    int row;
    int pos;

    for ( row = 0; row < MAX_ROWS; row++ )
    {

        if ((pos = find_param (cmnd, row, field)) != -1)
        {
            crt_pos_cursor ( TOP_ROW + row, COMMAND_WINDOW_COL, 0 );
            printf ( "%1d %s", row, commands [cmnd].parm [pos].strng );
        }
        else
        {
            crt_pos_cursor ( TOP_ROW + row, COMMAND_WINDOW_COL, 0 );
            printf ("        ");
        }
    }
}

print_line (line_num)
int line_num;
{
    int i;
    int j;
    int row;
    unsigned char tag;

    row = line_num - page_top_row;
    j = command_buffer [line_num] [0];
    crt_pos_cursor (TOP_ROW + row, tab_stop [0], 0);
    printf ("%s", commands [j].cmd_tag);
    for (i = 1; i < MAX_COLS; i++)
    {
        crt_pos_cursor (TOP_ROW + row, tab_stop [i], 0);
        tag = command_tag [line_num] [i];

        if (tag == TAG_NULL)
        {
            printf ("        ");
        }
        else if (tag == TAG_EMPTY)
        {
            printf ("________");
        }
        else
        {
            printf ("%s", commands [j].parm [command_buffer [line_num] [i]].strng);
        }
    }
}





print_edit_screen ()
{
    int i;
    int row;
    int col;

    if (cold_start)
    {
        for ( row = 0; row < MAX_ROWS; row++ )
        {
            for ( col = 0; col < MAX_COLS; col++ )
            {
                command_tag [row] [col] = 0;
                command_buffer [row] [col] = 0;
            }
        }
        cold_start = FALSE;
    }

    video_clear_screen ();
    crt_pos_cursor ( 1, 28, 0 );
    printf ("ALLPRO DIAGNOSTIC EDITOR\n");
    for ( i = 0; i < 80; i++ )
    {
        printf ("-");
    }
    crt_pos_cursor ( TOP_ROW + MAX_ROWS, 0, 0 );
    for ( i = 0; i < 80; i++ )
    {
        printf ("-");
    }
    for ( i = TOP_ROW; i < TOP_ROW + MAX_ROWS; i++ )
    {
        crt_pos_cursor ( i, COMMAND_WINDOW_COL -1, 0 );
        printf ( "|" );
    }

    for ( i = 0; i < MAX_ROWS; i++ )
    {
        crt_pos_cursor ( i + TOP_ROW, 0, 0 );
        printf ( "%04d", i );
    }
    print_main_window (0);
}



print_main_window (page_row)
int page_row;
{
    int i;
    int j;

    for ( i = 0; i < MAX_ROWS; i++ )
    {
        crt_pos_cursor ( i + TOP_ROW, 0, 0 );
        printf ( "%04d", i + page_row);
    }
    for (i = 0; i < MAX_ROWS; i++)
    {
        crt_pos_cursor (TOP_ROW + i, tab_stop [0], 0);
        printf ("%s", commands [command_buffer [i + page_row] [0] ].cmd_tag);
    }
    for (i = 0; i < MAX_ROWS; i++)
    {
        for (j = 1; j < MAX_COLS - 1; j++)
        {
            crt_pos_cursor (TOP_ROW + i, tab_stop [j], 0);
            printf ("%s", commands [command_buffer [i + page_row] [0] ].parm [command_buffer [i + page_row] [j] ].strng);
        }
    }
}


func_print ()
{

}



/*      crt_get_cur_pos (page) ret row * 256 + col position                     */
/*      crt_rd_char_att (page) ret attr * 256 + character                       */
/*      crt_wr_char_att (page, char_cnt, char, att)                             */
/*                                                                              */
/*                                                                              */
/*                                                                              */
/*                                                                              */


