/****************************************
 ** step.c ::: Jed Reynolds ::: Nov 13, 1995
 **
 ** A small program to bounce up and down on the
 ** step/free and step clock signals.
 **
 *****************************************/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

#define DEMO 1
#undef DEMO

/* ISA BOARD ADDRESS */
/* used for reset only */
#define BASE_ADDRESS 0x220 

/* write id of device to configure */
#define DEV_ID 0x222

/* id of INTERFACE CHIP */
#define INTERFACE_CHIP 0

/* address of CONFIG REGISTER on interface chip */
#define CONFIG_REG 0x23A

/* id of IFID chip */
#define IFID_CHIP 1

/* ADDRESS_PORT */
#define ADDRESS 0x230

/* address of program counter on IFID_CHIP */
#define PC_REG 0x23C


/* addresses of INSTRUCTION MEMORY */
/* 
	for our IF/ID stage, we use these addresses
	for reading our IR_LO and our IR_HI
	because we are not directly looking at our
	instruction memory. (so I'll doubly redefine
	them for documentation's sake.
*/
#define FLEXMEM_DATA_LO 0x236
#define FLEXMEM_DATA_HI 0x238
#define IR_LO FLEXMEM_DATA_LO
#define IR_HI FLEXMEM_DATA_HI


/*
 * define step/free type instructions
 */
#define FREERUN_MODE 0x0000
#define SSTEP_MODE 0x0001
#define STEPUP 0x0003
#define STEPDOWN 0x0001

/*************************************************************
 **     MAIN
 **
 ** 	Outline of program loop:
 **	1) pulse the debug clock
 **	2) read the PC
 **	3) read the IR (hi, lo);
 **	4) interate
 *************************************************************/
void main()
{
    unsigned int address, address_return, im_addr;
    unsigned int data_hi, data_lo;
    int stepkey;

    printf("Go for a step? Hit q to quit or enter to go on.\n");

    /*
     * Loop with the enter key
     */

    im_addr = 0;
 
    while ( (stepkey = getchar()) != EOF ) {
        if (stepkey == 'q') break;
        printf ("One step at a time; q to quit.\n");

	/*
	 * Jump up and down on the SINGLE STEP CLOCK
	 */

	/* assert the address for the interface chip */
        outport( DEV_ID, INTERFACE_CHIP );

	/* assert single step mode */
        outport(CONFIG_REG, SSTEP_MODE);

	/* single step up */
        outport(CONFIG_REG, STEPUP);
	address = inport(CONFIG_REG);

        printf ("  UP: %4x, ", address);
	fflush(stdout);

	/* single step down */
        outport(CONFIG_REG, STEPDOWN);
	address = inport(CONFIG_REG);
	
        printf ("  DOWN: %4x, ", address);
	fflush(stdout);

	/*
	 * Read the PC
	 */

	/* assert address of IFID chip */
	outport ( DEV_ID, 0x1 );

	/* read PC */
	address = inport (PC_REG);	

	/*
	 * Read the instruction memory with the value obtained
	 * from the PC because we can't access the IR
	 */

	/* assert the address to lookit in the instruction mem */
        /* outport (ADDRESS, address); */
        address_return = inport(ADDRESS);
        printf("PC: %4x is %4x: %4x %4x (hi,lo)\n", 
		address,
		address_return,
		inport(IR_HI),
		inport(IR_LO));
		
        /* try and force some address values around */
        /* outport(ADDRESS, im_addr); */
        printf("Inst mem @ %4x should be: %4x %4x(hi,lo)\n",
                im_addr,
                inport(IR_HI),
                inport(IR_LO));

        im_addr = im_addr + 4;
        
    } /* end of while */
    
    return;
} /* eof */
