/*
	Z0ECHO.C

	Port Z0 device echo program. This program demonstrates how to use port
	Z0 as an AASC device.
*/

#use aascz0.lib

main() {
	char rBuf[0x400];
	char wBuf[0x400];
	auto char localBuf[0x400];
	auto unsigned count;
	auto struct _Channel *chan;
	auto char *pCur;
	auto unsigned remaining;
	
	_GLOBAL_INIT();

	chan = aascOpen(DEV_Z0,
		0,
		ASCI_PARAM_8N1+ASCI_PARAM_1200*16,
		NULL);

	aascSetReadBuf(chan,rBuf,sizeof(rBuf));
	aascSetWriteBuf(chan,wBuf,sizeof(wBuf));
	aascTxSwitch(chan,1);
	aascRxSwitch(chan,1);
	
	//	flip the RS232 driver and CTS switches
	outport(0x4084,1);
	outport(0x4083,1);
	outport(0x4082,1);

	while (1) {
		hitwd();

		remaining = aascReadBlk(chan,localBuf,sizeof(localBuf),0);
		pCur = localBuf;

		while (remaining) {
			count = aascWriteBlk(chan, pCur, remaining, 1);
			pCur += count;
			remaining -= count;
			hitwd();
		}
	}
}
