#include "../h/stdio11.h"
#include "../h/vaxio.h"

#define YES     1
#define NO      0
#define NWRD    16              /* read by the vax */
#define NWWR    20              /* written by the vax */

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

int exch, wc, delay, code;
short obuf[NWRD], ibuf[NWWR];

struct drcdevice *dp = DRC_ADDR;

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

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

	for(i = 0; i < NWRD; ++i)
		obuf[i] = NWRD;

	exch = 0;
	onclk = send;

	printf("\nflip switch to Micro and type <return> to begin ");
	while (getchar() != '\n')
		;
	printf("\ntick delay ");
	scanf("%d%d", &tcc, &delay);

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

	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, wc, code);
	for (i = 0; i < NWWR; ++i) {
		printf("%d ", ibuf[i]);
	}
}

send()
{
	register int i;
	int spin;

	wc = dr11(ibuf, obuf, NWRD);
	if (wc != NWWR) {
		code = -2;
		tick = 0;
		return;
	}
	if (ibuf[0] == 0) {
		tick = 0;
		code = 0;
		return;
	}
	for(i=0; i < wc; ++i) {
		if (ibuf[i] != NWWR) {
			code = -3;
			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();
}
