/*
 * MAC  Version 2.0           Author :  Vincent Hayward
 *                                      School of Electrical Engineering
 *                                      Purdue University
 *      Dir     : sys
 *      File    : lsitst.c
 *      Remarks : fifo test program
 *      Usage   : make make test; use with vaxtst.c
 */

#include "../h/stdio11.h"
#include "../h/fifoio.h"

#define YES     1
#define NO      0

extern int (* onclk)();
extern int tick;

int exch, wc, delay, code;
struct fifobuf obuf, ibuf;

struct drcdevice *dp = DRC_ADDR;

main()
{
	int send();
	int i, tcc;

	setbuf(stdin,NULL);
	setbuf(stdout,NULL);

	for(i = 0; i < FIFOBUFS; ++i)
		obuf.data[i] = ID;
	obuf.wc = FIFOBUFS;

	exch = 0;
	onclk = send;

	printf("\nflip switch to Micro and type <return> to begin ");
	while (getchar() != '\n')
		;

	dp->drccsr = 0;
	while ((dp->drccsr & FIFO_EMPTY) == 0)
		i = dp->drcbuf;

	printf("\ntick delay ");
	scanf("%d%d", &tcc, &delay);

	tick = tcc;             /* from now the clock starts */

	while (tick)            /* do something in the background */
		wait();
	printf("exit ... exch = %d   wc = %d   code = %d\n", exch, ibuf.wc, code);
	for (i = 0; i < FIFOBUFS; ++i) {
		printf("%o ", ibuf.data[i]);
	}
}

send()
{
	register int i;
	int spin;

	i = dr11(&ibuf, &obuf);
	if (i < 0) {
		code = i;
		tick = 0;
		return;
	}
	if (ibuf.wc != FIFOBUFS) {
		code = -9;
		tick = 0;
		return;
	}
	if (ibuf.data[0] == 0) {
		tick = 0;
		code = 0;
		return;
	}
	for(i=0; i < wc; ++i) {
		if (ibuf.data[i] != OD) {
			code = -8;
			tick = 0;
			return;
		}
	}
	spin = delay;
	while(spin--)
		;
	++exch;
}


zexit(n)
int n;
{
	register struct drcdevice *dp = DRC_ADDR;

	tick = 0;
	dp->drccsr = 0;
	printf("\nexit %d\n", n);
	halt();
}
