.DEFINE EQU	=
.DEFINE DB	.BYTE
.DEFINE DW	.WORD
	.CODE
PISLOT	EQU	$00
;*
;* ACIA REGISTERS
;*
ACIADR	EQU	$C088+PISLOT*16
ACIASR	EQU	$C089+PISLOT*16
ACIACR	EQU	$C08A+PISLOT*16
ACIAMR	EQU	$C08B+PISLOT*16
;*
;* APPLE I/O LOCATIONS
;*
KEYBD	EQU	$C000
STROBE	EQU	$C010
;*
;* UTIL ROUTINES
;*
WAIT	EQU	$FCA8
COUT	EQU	$FDED
CROUT	EQU	$FD8E
PRBYTE	EQU	$FDDA
PRHEX	EQU	$FDE3
PRNTAX	EQU	$F941
RDKEY	EQU	$FD0C
RDCHAR	EQU	$FD35
GETLN	EQU	$FD6A
;*
;* ZERO PAGE PARAMETERS
;*
PDCMD	EQU	$42
PDUNIT	EQU	$43
PDBUFF	EQU	$44
PDBUFL	EQU	$44
PDBUFH	EQU	$45
PDBLKL	EQU	$46
PDBLKH	EQU	$47
;*
;* SLOT INDEX = SLOT # * 16
;*
SLOTIDX	EQU	$0300
;*
;* DRIVER SCRATCHPAD
;*
TMP	EQU	$0478+PISLOT
PAD0	EQU	$0478+PISLOT
PAD1	EQU	$04F8+PISLOT
PAD2	EQU	$0578+PISLOT
PAD3	EQU	$05F8+PISLOT
PAD4	EQU	$0678+PISLOT
PAD5	EQU	$06F8+PISLOT
PAD6	EQU	$0778+PISLOT
PAD7	EQU	$07F8+PISLOT
;*
;* PRODOS COMMANDS
;*
PDSTAT	EQU	0
PDREAD	EQU	1
PDWRITE	EQU	2
PDFORMT	EQU	3
;*
;* PRODOS ERRORS
;*
PDNOERR	EQU	$00
PDIOERR	EQU	$27
PDNODEV	EQU	$28
PDWRPRT	EQU	$2B
;*
;* PRODOS GLOBAL PAGE LOCATIONS
;*
CLKJMP	EQU	$BF06
DEV1L	EQU	$BF10
DEV1H	EQU	$BF11
DEV2L	EQU	$BF20
DEV2H	EQU	$BF21
DEVCNT	EQU	$BF31
DEVLST	EQU	$BF32
P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DX	#$02		; # OF PAGES TO XFER
	DEY			; CPY #PDREAD
	BEQ	RDBLK
	DEY			; CMP #PDWRITE
	BEQ	WRBLK
IOERR:	LDA	#PDIOERR
CMDERR:	PLP
	SEC
DOCLK:	RTS			; NO OP CLOCK ROUTINE
STATUS: JSR	RECVACC
	TAX
	JSR	RECVACC
	TAY
CMDEX:	JSR	RECVACC
	BNE	CMDERR
	PLP
	CLC
	RTS
RDBLK:	JSR	RECVACC
	STA	(PDBUFF),Y
	INY
	BNE	RDBLK
	INC	PDBUFH
	DEX
	BNE	RDBLK
	BEQ	CMDEX
WRBLK:	LDA	(PDBUFF),Y
	JSR	SENDACC
	INY
	BNE	WRBLK
	INC	PDBUFH
	DEX
	BNE	WRBLK
        BEQ	CMDEX
;*
;* ACIA I/O ROUTINES
;*
SENDACC:
	PHA
FIXUP1:
SENDWT:	LDA	ACIASR
	AND	#$10
	BEQ	SENDWT
	PLA
FIXUP2:	STA	ACIADR
	RTS
RECVACC:
FIXUP3:
RECVWT:	LDA	ACIASR
	AND	#$08
	BEQ	RECVWT
FIXUP4:	LDA	ACIADR
	RTS
	.ASSERT	* - DOCMD <= DRVRLEN, error, "Driver too big"
	.OUT	.SPRINTF("Driver size %i", * - DOCMD)
