next up previous contents
Next: D.5 string.c Up: D C Source Code Previous: D.3 download.c

D.4 console.c

 

/*****************************************************************
** C O N S O L E . C
** Address Tracing System, Scott Harrington, Fall 94
**
** Command line options:
** [-p port]     port #(200,220,240,...,3E0) default 300
** [-s segment]  segment D or E (default D)
*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>

#define STAT_Empty0 0x01
#define STAT_Empty1 0x02
#define STAT_Empty2 0x04
#define STAT_Empty3 0x08
#define STAT_nExtract 0x10
#define STAT_nProgram 0x20
#define STAT_nInit 0x40
#define STAT_SACK 0x80

enum {OUT_Config=0, OUT_NOP1, OUT_StartWrite, OUT_StartRead,
    OUT_Remote, OUT_Local, OUT_E000, OUT_D000,
    OUT_Sel0, OUT_Sel1, OUT_Sel2, OUT_Sel3,
    OUT_UnSelAll, OUT_SelAll, OUT_NOP14, OUT_Reset};

unsigned int ioPort = 0x300;
int page_E = 0;
int Extract = 0;
int Use8bit = 1;

void writeMem8(unsigned char val);
void writeMem16(unsigned int val);
unsigned char readMem8(void);
unsigned int readMem16(void);
int showStatLoop(void);
void ShowCommands(void);

void main(int argc, char *argv[])
    {
    int i, ch;
    long c, cw;
    unsigned int val;
    char *parmstr, *endptr;

    for (i=1; i<argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
            case 'p':
                parmstr = (argv[i][2]) ? &argv[i][2] : argv[++i];
                ioPort = (unsigned int)strtoul(parmstr, &endptr, 0);
                break;
            case 's':
                parmstr = (argv[i][2]) ? &argv[i][2] : argv[++i];
                page_E = (parmstr[0] == 'E' || parmstr[0] == 'e');
                break;
            default:
                printf("Usage: %s [-p port] [-s D000|E000]\n", argv[0]);
                exit(-1);
                }
            }
        }

    if (ioPort & ~0x3E0) {
        printf("Valid ports are 0x200, 220, 240, ..., 3E0.\n");
        exit(-1);
        }

    /* Console Loop: accept commands from keyboard */

    printf("Bullwinkle CONSOLE\n");
    ShowCommands();

    while ((ch = toupper(showStatLoop())) != 'Q')
        {
        switch (ch)
            {
        case 'W': outportb(ioPort+OUT_StartWrite, 0); break;
        case 'R': outportb(ioPort+OUT_StartRead, 0); break;
        case 'M': outportb(ioPort+OUT_Remote, 0); break;
        case 'L': outportb(ioPort+OUT_Local, 0); break;
        case '0': outportb(ioPort+OUT_Sel0, 0); break;
        case '1': outportb(ioPort+OUT_Sel1, 0); break;
        case '2': outportb(ioPort+OUT_Sel2, 0); break;
        case '3': outportb(ioPort+OUT_Sel3, 0); break;
        case 'N': outportb(ioPort+OUT_UnSelAll, 0); break;
        case 'A': outportb(ioPort+OUT_SelAll, 0); break;
        case 'S': outportb(ioPort+OUT_Reset, 0); break;

        case '8':
            Use8bit = 1-Use8bit;
            printf("Now using %d bit reads and writes.\n",
                Use8bit? 8:16);
            break;

        case 'T':
        case ' ':
            if (Extract)
                {
                printf("READ VALUE: %u\n", Use8bit? readMem8():readMem16());
                }
            else
                {
                printf("Enter value to write: ");
                scanf("%u", &val);
                if (Use8bit)
                    writeMem8(val);
                else
                    writeMem16(val);
                }
            break;

        case 'C':
        default:
            ShowCommands();
            break;
            }
        }
    }


int showStatLoop() {
    unsigned int d, d0=256;
    while (!kbhit()) {
        d = inportb(ioPort);
        if (d != d0) {
            printf("%s %s %s %s   Empty:%d%d%d%d\n",
                (d & STAT_SACK) ? "SACK":"sack",
                (d & STAT_nInit) ? "init":"INIT",
                (d & STAT_nProgram) ? "pgm":"PGM",
                (d & STAT_nExtract) ? "ext":"EXT",
                (d & STAT_Empty0) ? 1:0,
                (d & STAT_Empty1) ? 1:0,
                (d & STAT_Empty2) ? 1:0,
                (d & STAT_Empty3) ? 1:0);
            d0 = d;
            }
        Extract = !(d & STAT_nExtract);
        }
    return getch();
    }

void ShowCommands()
    {
    printf("Commands: startWrite, startRead, reMote, Local,\n");
    printf("          sel0, sel1, sel2, sel3, selNone, selAll,\n");
    printf("          Trigger, reSet, Quit, 8bit, showCommands\n");
    }

/* Write a 16-bit word somewhere in the selected shared memory area
** (either D000 or E000 segment)
*/
void writeMem16(unsigned int val)
    {
    static unsigned int offset;
    unsigned int far *memptr;

    if (offset&1)
        offset = 0;
    memptr = MK_FP(page_E ? 0xE000:0xD000, offset);
    *memptr = val;

    offset += 2;        /* 16-bit int causes segment wraparound */
    }

void writeMem8(unsigned char val)
    {
    static unsigned int offset;
    unsigned char far *memptr;

    memptr = MK_FP(page_E ? 0xE000:0xD000, offset);
    *memptr = val;

    offset++;       /* 16-bit int causes segment wraparound */
    }


unsigned int readMem16()
    {
    static unsigned int offset;
    unsigned int far *memptr;

    /* ensure word-alignment */
    if (offset&1)
        offset = 0;

    memptr = MK_FP(page_E ? 0xE000:0xD000, offset);
    offset += 2;        /* 16-bit int causes segment wraparound */

    return *memptr;
    }

unsigned char readMem8()
    {
    static unsigned int offset;
    unsigned char far *memptr;
    memptr = MK_FP(page_E ? 0xE000:0xD000, offset);
    offset++;
    return *memptr;
    }


Scott E. Harrington
Sat Apr 29 18:56:25 EDT 1995