/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by John E. Lloyd.				    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Exercising/test program for the XYCOM XVME 203 clock timer board */

#include "../h/ctree.h"
#include "../h/rci.h"
#include "xvme203.h"

char key_buf[256];
RCI_DESC *td;
XC_BLK *xcb;

main()
{
	char *keytree;
	int counter;
	union {
	  unsigned short s;
	  unsigned char c[2];
	  } pack;
	int value;
	int mask;
	int retval;
	int doFxn();
	int bitnum;

	keytree = tree_match_parse("					\
(	quit								\
	setBit								\
	 ([imr irr]							\
	   (%d", &bitnum, ",0,7,		\"bit number (0 to 7)\")) \
	clearBit							\
	 ([imr irr]							\
	   (%d", &bitnum, ",0,7,		\"bit number (0 to 7)\")) \
	set								\
	 ([load mode]							\
	   (%d", &counter, ",1,5,		\"counter (1 to 5)\"	\
	     (%x", &value, ",,,			\"value (hex)\"))	\
	  [intrMode masterMode]						\
	   (%x", &value, ",,,			\"value (hex)\")	\
	  acr								\
	   (%x", &value, ",,,			\"value (hex)\")	\
	  vector							\
	   (%d", &bitnum, ",0,7,		\"IREQ line (0-7)\"	\
	     (%x", &value, ",,,			\"vector num (hex)\"))	\
	  csr								\
	   (%x", &value, ",,,			\"value (hex)\")	\
	 )								\
	get								\
	 ([load mode hold]						\
	   (%d", &counter, ",1,5,		\"counter (1 to 5)\")	\
	  masterMode							\
	  isr								\
	  imr								\
	  irr								\
	  acr								\
	  status							\
	  cntrStatus							\
	  csr								\
	 )								\
	[save arm load disarm loadAndArm]				\
	 (%x", &mask, ",,,			\"mask value (hex)\")	\
	reset								\
	read								\
	 (%d", &counter, ",1,5,			\"counter (1 to 5)\")	\
	enable								\
	disable								\
)",0);
	if (keytree == 0)
	 { printf ("ERROR/Parse error at %.30s\n", tree_match_err_at());
	   exit (-1);
	 }
	rciSetOptions (EXIT_ON_ERROR);
	xcb = (XC_BLK*)allocMem (NULL, sizeof(XC_BLK), UNDEF);

	td = rciCreate ("foo", 0x1);

	do
	 { tree_match (keytree, "XC> ", key_buf);

	   if (COMMAND("set load"))
	    { xcb->addr = 8 + counter;
	      xcb->command = SET_CTRS;
	      pack.s = value;
	      xcb->value.c[0] = pack.c[1];
	      xcb->value.c[1] = pack.c[0];
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("set mode"))
	    { xcb->addr = counter;
	      xcb->command = SET_CTRS;
	      pack.s = value;
	      xcb->value.c[0] = pack.c[1];
	      xcb->value.c[1] = pack.c[0];
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("set masterMode"))
	    { xcb->addr = 0x17;
	      xcb->command = SET_CTRS;
	      pack.s = value;
	      xcb->value.c[0] = pack.c[1];
	      xcb->value.c[1] = pack.c[0];
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("get mode"))
	    { xcb->addr = counter;
	      xcb->command = GET_CTRS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      pack.c[0] = xcb->value.c[1];
	      pack.c[1] = xcb->value.c[0];
	      value = pack.s;
	      printf ("0x%x\n", value);
	    }
	   else if (COMMAND("get load"))
	    { xcb->addr = 8 + counter;
	      xcb->command = GET_CTRS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      pack.c[0] = xcb->value.c[1];
	      pack.c[1] = xcb->value.c[0];
	      value = pack.s;
	      printf ("0x%x\n", value);
	    }
	   else if (COMMAND("get hold"))
	    { xcb->addr = 16 + counter;
	      xcb->command = GET_CTRS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      pack.c[0] = xcb->value.c[1];
	      pack.c[1] = xcb->value.c[0];
	      value = pack.s;
	      printf ("0x%x\n", value);
	    }
	   else if (COMMAND("get masterMode"))
	    { xcb->addr = 0x17;
	      xcb->command = GET_CTRS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      pack.c[0] = xcb->value.c[1];
	      pack.c[1] = xcb->value.c[0];
	      value = pack.s;
	      printf ("0x%x\n", value);
	    }
	   else if (COMMAND("get cntrStatus"))
	    { xcb->command = GET_CTRS_STATUS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	   else if (COMMAND("loadAndArm"))
	    { xcb->command = LOAD_AND_ARM_CTRS;
	      xcb->mask = 0x1f & mask;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }	      
	   else if (COMMAND("load"))
	    { xcb->command = LOAD_CTRS;
	      xcb->mask = 0x1f & mask;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }	      
	   else if (COMMAND("arm"))
	    { xcb->command = ARM_CTRS;
	      xcb->mask = 0x1f & mask;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }	      
	   else if (COMMAND("save"))
	    { xcb->command = SAVE_CTRS;
	      xcb->mask = 0x1f & mask;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }	      
	   else if (COMMAND("disarm"))
	    { xcb->command = DISARM_CTRS;
	      xcb->mask = 0x1f & mask;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }	      
	   else if (COMMAND("reset"))
	    { xcb->command = RESET_CTRS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      xcb->command = RESET_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }	      
	   else if (COMMAND("read"))
	    { xcb->counter = counter;
	      xcb->addr = 16 + counter;
	      xcb->command = READ_CTRS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      pack.c[0] = xcb->value.c[1];
	      pack.c[1] = xcb->value.c[0];
	      value = pack.s;
	      printf ("0x%x\n", value);
	    }
	   else if (COMMAND("enable"))
	    { xcb->command = ENABLE_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("disable"))
	    { xcb->command = DISABLE_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("get isr"))
	    { xcb->addr = 0;
	      xcb->command = GET_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	   else if (COMMAND("get imr"))
	    { xcb->addr = 1;
	      xcb->command = GET_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	   else if (COMMAND("get irr"))
	    { xcb->addr = 2;
	      xcb->command = GET_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	   else if (COMMAND("get acr"))
	    { xcb->addr = 3;
	      xcb->command = GET_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	   else if (COMMAND("get status"))
	    { xcb->command = GET_ICTL_STATUS;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	   else if (COMMAND("set acr"))
	    { xcb->value.c[0] = value;
	      xcb->command = SET_ACR_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("setBit imr"))
	    { xcb->value.c[0] = bitnum;
	      xcb->command = SET_IMR_BIT;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("clearBit imr"))
	    { xcb->value.c[0] = bitnum;
	      xcb->command = CLEAR_IMR_BIT;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("setBit irr"))
	    { xcb->value.c[0] = bitnum;
	      xcb->command = SET_IRR_BIT;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("clearBit irr"))
	    { xcb->value.c[0] = bitnum;
	      xcb->command = CLEAR_IRR_BIT;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("set vector"))
	    { xcb->addr = bitnum;
	      xcb->value.c[0] = value;
	      xcb->command = SET_VECTOR;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("set intrMode"))
	    { xcb->value.c[0] = value;
	      xcb->command = SET_MODE_ICTL;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("set csr"))
	    { xcb->value.c[0] = value;
	      xcb->command = SET_CSR;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	    }
	   else if (COMMAND("get csr"))
	    { xcb->command = GET_CSR;
	      rciExecute (td, doFxn, (int)xcb, &retval);
	      printf ("0x%x\n", xcb->value.c[0]);
	    }
	 }
	while (!(COMMAND("quit")));
}
