/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

extern short hostError;
extern short hostSize;
extern unsigned short *hostAddr;

#define NULL 0

#include "lsi11_73.h"

listenHandler(n)
int n;
{
	printf ("listen handler\n", n);
	if (hostAttach () < 0)
	 { printf ("host attach reports error %d\n", hostError);
	 }
}

handler(n)
int n;
{
	printf ("caught signal, code %d\n", n);
}

#define BUFSIZE 128
unsigned short rbuf[BUFSIZE];
unsigned short wbuf[BUFSIZE];
int cycle;
int count;
union _pack {
	unsigned char b[2];
  	unsigned short s;
  };

readWriteHandler(n)
int n;
{
	int i;
	int n;
	unsigned short testData = (cycle%2) ? 0xaaaa : 0x5555;

	for (i=0; i<count; i++)
	 { wbuf[i] = testData;
	 }
	if (hostWrite (wbuf, count) < 0)
	 { printf ("host write reports error %d\n", hostError);
	   return;
	 }
	if ((n = hostRead (rbuf, BUFSIZE)) < 0)
	 { printf ("host read reports error %d\n", hostError);
	   return;
	 }
	for (i=0; i<count; i++)
	 { if (rbuf[i] != wbuf[i])
	    { printf ("Error: rbuf[%d] != wbuf[%d] (0x%x vs. 0x%x)\n",
		       i, i, rbuf[i], wbuf[i]);
	      return;
	    }
	 }
	hostOnSig (1, readWriteHandler, 0);	
}

main()
{
	char data[20];
	char cmd[2];
	
	hostDevice (0xeff8, 0xa0);
	
	printf ("Driver testbed. Type '?' for menu\n\n");
	
	do
	 { printf ("DT> ");
	   scanf ("%1s", cmd);
	   switch (cmd[0])
	    { case '?':
	       { printf (" 'q' : quits program\n");
		 printf (" 'l' : call listen routine\n");
		 printf (" 'a' : call attach routine\n");
		 printf (" 'd' : call detach routine\n");
		 printf (" 'r' : call read routine\n");
		 printf (" 'w' : call write routine\n");
		 printf (" 's' : call signal routine\n");
		 printf (" 'o' : call onSig routine\n");
		 printf (" 'e' : examine registers (peek)\n");
		 printf (" 'p' : put something into registers (poke)\n");
		 printf (" 'x' : examine arbitrary memory location\n");
		 printf (" 'X' : poke an arbitrary memory location\n");
		 printf (" 'D' : call Debug routine \n");
		 printf (" 'c' : set up read-write cycles\n");
		 printf (" 't' : terminate read-write cycles\n");
		 break;
	       }
	      case 'o':
	       { int n;
		 printf ("signal num> ");
		 if (scanf ("%d", &n) != 1)
		  { printf ("Error reading number\n");
		    break;
		  }
		 hostOnSig (n, handler, n);
		 break;
	       }
	      case 'l':
	       { hostListen (listenHandler, 10);
		 break;
	       }
	      case 'a':
	       { int k;
		 if ((k = hostAttach ()) < 0)
		  { printf ("host attach reports error %d\n");
		    break;
		  }
		 printf ("%d\n", k);
		 break;
	       }
	      case 'd':
	       { hostDetach();
		 break;
	       }
	      case 'r':
	       { int size, i;
		 if ((size = hostRead (rbuf, BUFSIZE)) < 0)
		  { printf ("read report error %d\n", hostError);
		  }
		 else
		  { printf (" %d words read\n", size);
		    if (size <= 20)
		     { union _pack pack;
		       for (i=0; i<size; i++)
			{ pack.s = rbuf[i];
			  printf ("%2d  %02x %02x\n", 2*i,
				   pack.b[1], pack.b[0]);
			}
		     }
		    else
		     { printf ("number of words is too large to print\n");
		     }
		  }
		 break;
	       }
	      case 'w':
	       { int i, size, numwords;
		 union _pack pack;
		 printf ("number of words to send> ");
		 if (scanf ("%d", &numwords) != 1)
		  { printf ("error reading value\n");
		    break;
		  }
		 if (numwords > BUFSIZE)
		  { printf ("error: buffer can only hold %d words\n",BUFSIZE);
		    break;
		  }
		 for (i=0; i<numwords; i++)
		  { pack.b[0] = i*2;
		    pack.b[1] = i*2+1;
		    wbuf[i] = pack.s;
		  }
		 size = hostWrite(wbuf, numwords);
		 printf ("write returns %d\n", size);
		 if (size < 0)
		  { printf ("write reports error %d\n", hostError);
		  }
		 break;
	       }
	      case 's':
	       { hostSignal();
		 break;
	       }
	      case 'e':
	       { int i;
		 for (i=0; i<hostSize/2; i++)
		  { printf ("%2d  0x%04x\n", i*2, hostAddr[i]);
		  }
		 break;
	       }
	      case 'p':
	       { int value, index;
		 printf ("Enter (hex) value and byte offset> ");
		 if (scanf ("%x%d", &value, &index) != 2)
		  { printf ("error reading values\n");
		    break;
		  }
		 if (index % 2)
		  { printf ("error - byte offset must be even\n");
		    break;
		  }
		 if (index > (hostSize-2))
		  { printf ("error - max byte offset is %d\n", (hostSize-2));
		    break;
		  }
		 hostAddr[index/2] = value;
		 break;
	       }
	      case 'x':
	       { short *address;
		     unsigned int temp;
		 printf ("address (hex) > ");
		 if (scanf ("%x", &address) != 1)
		  { printf ("error reading value\n");
		    break;
		  }
		 temp = (0xffff) & *address;
		 printf ("0x%04x  %04x\n", address, temp);
		 break;
	       }
	      case 'X':
	       { short *address;
		     unsigned int value;
		 printf ("address and value (hex) > ");
		 if (scanf ("%x%x", &address, &value) != 2)
		  { printf ("error reading values\n");
		    break;
		  }
		 *address = value;
	 	 printf ("0x%04x  %04x\n", address,value);
		 break;
	       }
	      case 'c':
	       { printf ("word count> ");
		 if (scanf ("%d", &count) != 2)
		  { printf ("error reading count\n");
		    break;
		  }
		 hostOnSig (1, readWriteHandler, 0);
		 break;
	       }
	      case 't':
	       { hostOnSig (1, NULL, 0);
		 break;
	       }
		 
          case 'D':
           { break;
           }
		 }
	 }
	while (cmd[0] != 'q');

	zexit(0);
}
