/* 529.cpp - C program that allows control of the */
/* 529int.tdf interface from the keyboard. */


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


#define RS_ADDRESS 0x305;
#define CSTEP_ADDRESS 0x306;
#define TEST_RESET_ADDRESS 0x307;
#define CSTEP_CNTA_ADDRESS 0x308
#define CSTEP_CNTB_ADDRESS 0x309
#define CSTEP_CNTENABLE_ADDRESS 0x30A


void main() {

	int c;
	int toggle1,toggle2,toggle3;
	toggle1 = 0;
	toggle2 = 0;
	toggle3 = 0;
	unsigned long state;
	unsigned long steps;
	unsigned long tempval;
	unsigned char tempout;

	clrscr();
	printf("\n\nRIPP10 Application Tester\n");
	printf("\n************************\n");
	printf("Hit <S> to toggle the system clock\n");
	printf("Hit <C> to step for a given number of step\n");
	printf("Hit <R> to read the current system state (global00-31)\n");
	printf("Hit <T> to toggle the system reset\n");
	printf("Hit <E> to enable/disable the system clock\n");
	printf("Hit <?> to see this menu again\n");
	printf("Hit <Q> to quit\n\n");

	do {

	c = getch();
	switch(c) {


	case 't':case 'T':
	  if (toggle1 == 0) {
		toggle1 = 1;
		outp(0x307,0x01);
		printf("Reset line is now %i\n",toggle1);
	  }
	  else {
		toggle1 = 0;
		outp(0x307,0x00);
		printf("Reset line is now %i\n",toggle1);
	  }
	break;
	case 'C': case 'c':
		outp(0x30A,0x00); // Disable the counter
		outp(0x306,0x00);  // Make sure single step clock is low
		toggle2 = 0;  // Set clock toggle to zero
		printf("Enter the number of steps to complete (0-1048576): ");
		scanf("%li",&steps);
		steps = steps - 1;
		tempout = steps & 0xff;
		outp(0x308,tempout); // Write lowest 8 bits
		tempout = (steps >> 8) & 0xff; // Write middle 8 bits
		outp(0x309,tempout);
		tempout = (steps >> 16) & 0xf; // Write top 4 bits;
		outp(0x30E,tempout); // Write top 4 bits
		outp(0x30A,0x01); // Enable the counter
		printf("done\n");
	break;

	case 's':case 'S':
	  if (toggle2 == 0) {
		toggle2 = 1;
		outp(0x306,0x01);
		printf("System clock is now %i\n",toggle2);
	  }
	  else {
		toggle2 = 0;
		outp(0x306,0x00);
		printf("System clock is now %i\n",toggle2);
	  }
	break;

	case 'R':case 'r':
   readstate:		state = 0;
		tempval = 0;
		tempval= inp(0x305);// Fetch first byte
		state = state | tempval;
		tempval = 0;
		tempval = inp(0x30B);// Fetch second byte
		state = state | (tempval << 8);
		tempval = 0;
		tempval = inp(0x30C);// Fetch third byte
		state = state | (tempval << 16);
		tempval = 0;
		tempval = inp(0x30D); // Fetch fourth byte
		state = state | (tempval << 24);
		printf("System state gl(31..0) = %8lx\n",state);
	break;
	case 'E':case 'e':
		if (toggle3 == 0) {
			toggle3 = 1;
			outp(0x30F,0x01);
			printf("System clock enable is now enabled\n");
		}
		else {
			toggle3 = 0;
			outp(0x30F,0x00);
			printf("System clock is now disabled\n");
		}
	break;

	case '?':
		clrscr();
		printf("\n\nRIPP10 Application Tester\n");
		printf("\n************************\n");
		printf("Hit <S> to toggle the system clock\n");
		printf("Hit <C> to step for a given number of step\n");
		printf("Hit <R> to read the current system state (global00-31)\n");
		printf("Hit <T> to toggle the system reset\n");
		printf("Hit <E> to enable/disable the system clock\n");
		printf("Hit <?> to see this menu again\n");
		printf("Hit <Q> to quit\n\n");
	break;

	case 'q':case 'Q':
		exit(0);
	break;
	}

  } while(1);
}