	IFUND	ACIAINTINBUFSIZE
	IF	MEMSIZE<=32
ACIAINTINBUFSIZE	EQU	40
	ELSE
ACIAINTINBUFSIZE	EQU	80
	FIN	MEMSIZE<=32
	FIN	ACIAINTINBUFSIZE
	IFUND	ACIAINTOUTBUFSIZE
	IF	MEMSIZE<=32
ACIAINTOUTBUFSIZE	EQU	40
	ELSE
ACIAINTOUTBUFSIZE	EQU	100
	FIN	MEMSIZE<=32
	FIN	ACIAINTOUTBUFSIZE
	IFUND	ACIAINTERRUPT
ACIAINTERRUPT	EQU	1
	FIN	ACIAINTERRUPT
	IFUND	ACIAUPPERLIMIT
ACIAUPPERLIMIT	EQU	ACIAINTINBUFSIZE*7/10
	FIN	ACIAUPPERLIMIT
	IFUND	ACIALOWERLIMIT
ACIALOWERLIMIT	EQU	ACIAINTINBUFSIZE*3/10
	FIN	ACIALOWERLIMIT
::	SET	*
	ORG	DCB:SIZE
*
*	ACIA DCB DISPLACEMENTS
*
NACIATABS	EQU	16
ACIATABS	RMB	NACIATABS
	RMB	1
	IF	ACIAINTERRUPT
ACIAINTINMPT	RMB	2	EMPTY POINTER
ACIAINTINFILL	RMB	2	FILL POINTER
ACIAINTINUSED	RMB	1	NUMBER OF BUSY SLOTS
ACIAHOSTISWAITING	RMB	1	<> 0 --> HOST WAS SENT XOFF
ACIAINTOUTMPT	RMB	2	EMPTY POINTER
ACIAINTOUTFILL	RMB	2	FILL POINTER
ACIAINTOUTFREE	RMB	1	NUMBER OF FREE SLOTS
ACIAINTOUTEXPECTED	RMB	1	<> 0 --> I'M EXPECTING (NO, AN INTERRUPT!)
ACIAOUTFORCECHAR	RMB	1
ACIAINTINBUF	RMB	ACIAINTINBUFSIZE
ACIAINTOUTBUF	RMB	ACIAINTOUTBUFSIZE
ACIAXONWAIT	RMB	1
ACIAONWAITPTR	RMB	2
ACIACOLCNT	RMB	1
ACIAIDLES	RMB	1
ACIABINORASC	RMB	1	; BINARY OR ASCII READ FLAG
ACIAEOF	RMB	1	; EOF SEEN FLAG
	FIN	ACIAOUTINTERRUPT
ACIADCBSIZE	EQU	*
	ORG	::
	PAGE	ACIA DCB
ACIADCB	FCB	1	DONE
	FDB	0
	FDB	ACIASTR
	FDB	NEXTDEVICEDCB
	FDB	ACIADRIVER
	RPT	NACIATABS
	FCB	(*-ACIADCB-ACIATABS+1)*8
	FCB	0
	IF	ACIAINTERRUPT
	FDB	ACIAINTINBUF+ACIADCB	ACIAINTINMPT
	FDB	ACIAINTINBUF+ACIADCB	ACIAINTINFILL
	FCB	0	ACIAINTINUSED
	FCB	0	ACIAHOSTISWAITING
	FDB	ACIAINTOUTBUF+ACIADCB	ACIAINTOUTMPT
	FDB	ACIAINTOUTBUF+ACIADCB	ACIAINTOUTFILL
	FCB	ACIAINTOUTBUFSIZE	ACIAINTOUTFREE
	FCB	0	ACIAINTOUTEXPECTED
	FCB	0	ACIAOUTFORCECHAR
	RPT	ACIADCBSIZE-(*-ACIADCB)
	FCB	0
	FIN	ACIAINTERRUPT
ACIASTR	FCC	'ACIA:'
	FCB	0
*
NEXTDEVICEDCB	SET	ACIADCB
	PAGE	ACIADRIVER
ACIADRIVER	FDB	ACIAOPEN
	FDB	ACIACLOSE
	FDB	ACIAREADA
	FDB	ACIAWRITEA
	FDB	ACIAREADB
	FDB	ACIAWRITEB
	FDB	ACIACREATE
	FDB	ILLDEVICEOP	RENAME
	FDB	ILLDEVICEOP	DELETE
	FDB	ACIACONTROL
	FDB	ACIASTATUS
	FDB	ACIARESET
	FDB	ACIAPFRESTART
*
ACIACREATE	EQU	*
ACIAOPEN	EQU	*
	CLR	ACIADCB+ACIACOLCNT
	CLR	ACIADCB+ACIAEOF
	LDAA	#$11	MARK THAT WE GOT XON
	STAA	ACIADCB+ACIAXONWAIT
	LDAA	#$95	SET UP ACIA
	STAA	ACIAACIA	JUST IN CASE SOMEONE CHANGED THE PLUG
	LDAA	#XON	TELL THE HOST IT CAN START TO SEND
	JSR	ACIAOUTFORCE
ACIAPFRESTART	EQU	*
ACIACLOSE	LDAA	#XOFF	TELL THE HOST TO SHUT UP
	JSR	ACIAOUTFORCE
	OKRTS


ACIACONTROL	EQU	*
	LDX	SDOS+SDOS:IOBLOCKPTR
	CMPA	#CC:IDLES	; SET NULLS AFTER <CR><LF>
	BEQ	SETACIAIDLES
	CMPA	#CC:TABS
	BEQ	SETACIATABS	; SET TAB STOPS FOR ACIA
	JMP	ILLDEVICEOP	; NO GOOD KID!

SC:GETCHARREADY	EQU	0
ACIASTATUS	EQU	*
	LDX	SDOS+SDOS:IOBLOCKPTR
	CMPA	#SC:GETEOF
	BEQ	ACIAGETEOF
	CMPA	#SC:GETCHARREADY
	BEQ	ACIAGCREADY
	JMP	ILLDEVICEOP

SETACIAIDLES	EQU	*
	JSR	SDOS+SDOS:CHECKWRLEN
	FDB	1
	LDX	SCBLK:WRBUF,X
	LDAA	CONTROL:DATA,X
	STAA	ACIAIDLES+TTYDCB
	OKRTS

SETACIATABS	EQU	*
	JSR	SDOS+SDOS:CHECKWRLEN
	FDB	0
	LDAA	SCBLK:WRLEN,X
	BNE	SETACIATABS5
	CLR	ACIATABS+ACIADCB
	LDAB	SCBLK:WRLEN,X
	BEQ	SETACIATABS4
	CMPB	#NACIATABS
	BLS	SETACIATABS1
	LDAB	#NACIATABS
SETACIATABS1	STAB	TEMPB
	LDX	SDOS+SDOS:IOBLOCKPTR
	LDX	SCBLK:WRBUF,X
SETACIATABS2	LDAA	CONTROL:DATA,X
	INX
	PSHA
	DECB
	BNE	SETACIATABS2	; MORE TO DO
	LDX	DCBPOINTER
	CLR	ACIATABS+NTTYTABS,X
SETACIATABS3	PULA
	STAA	ACIATABS+NACIATABS-1,X
	DEX
	DEC	TEMPB
	BNE	SETACIATABS3
SETACIATABS4	OKRTS
SETACIATABS5	JMP	ILLDEVICEOP


ACIAGETEOF	JSR	SDOS+SDOS:CHECKRDLEN
	FDB	1
	LDX	SCBLK:RDBUF,X
	LDAA	ACIAEOF+ACIADCB
	STAA	0,X
	OKRTS

ACIAGCREADY	EQU	*
	JSR	SDOS+SDOS:CHECKRDLEN
	FDB	1
	LDX	SCBLK:RDBUF,X
	LDAA	ACIAINTINUSED+ACIADCB
	STAA	0,X
	OKRTS

	PAGE
ACIAWRITEB	JSR	ACIASTARTIO
	STX	COUNT
	BEQ	ACIAWRITEB4
ACIAWRITEB1	LDX	BUFFERPOINTER
	LDAA	0,X
	INX
	STX	BUFFERPOINTER
	JSR	ACIAOUT
	LDX	COUNT
	DEX
	STX	COUNT
	BNE	ACIAWRITEB1	B/ MORE TO DO
ACIAWRITEB3	EQU	*
ACIAWRITEB4	OKRTS

* SIMPLE WRITE ASCII

ACIAWRITEA	EQU	*
	JSR	ACIASTARTIO	; SETUP TRANSFER PARAMETER
	BEQ	ACIAWRITEA6	; NOTHING TO DO BUT RETURN
ACIAWRITEA1	EQU	*
	LDX	DCBPOINTER	; LOOP FOR WRITING
	LDAA	ACIAXONWAIT
	BNE	ACIAWRITEA2	; B/ NO XOFF
	LDX	ACIAONWAITPTR,X	; WAIT FOR XON
	JSR	SDOS+SDOS:WAITEVENT
ACIAWRITEA2	EQU	*
	LDX	BUFFERPOINTER
	LDAA	0,X
	INX
	STX	BUFFERPOINTER
	ANDA	#$7F	; REMOVE PARITY
	CMPA	#$1F
	BGT	ACIAWRITEA4	; B/NOT A CONTROL CHARACTER
	CMPA	#CR
	BEQ	ACIAWRITEA4	; CR OK
	CMPA	#XON
	BEQ	ACIAWRITEA4
	CMPA	#XOFF
	BEQ	ACIAWRITEA4
	CMPA	#EOF
	BEQ	ACIAWRITEA4
	CMPA	#TAB
	BNE	ACIAWRITEA5	; DONOT WRITE ILLEGAL CONTROL CODE IGNORE!
	LDX	#ACIATABS+ACIADCB
	LDAB	ACIACOLCNT+ACIADCB
	JSR	EXPANDTABS0
	TAB
	LDX	DCBPOINTER
	ADDA	ACIACOLCNT