	NAM	SWTPIO
********* VERSION 1.0 ************
* WRITTEN BY R.H. UITERWYK

DOS	EQU	$2600
DRVREG	EQU	$8014
COMREG	EQU	$8018
TRKREG	EQU	$8019
SECREG	EQU	$801A
DATREG	EQU	$801B

	ORG	$0000

TRACK	RMB	1
SECTOR	RMB	1
STATUS	RMB	1
ERRCNT	RMB	1
BMEMH	RMB	1
BMEML	RMB	1
EMEMH	RMB	1
EMEML	RMB	1
NOSECT	RMB	1
SAVEX	RMB	2
PROGX	RMB	2
NDRIVE	RMB	1
ATRACK	RMB	4
PRELEX	RMB	2

	ORG	$2400

START	JSR	BOOT
RESTRT	LDX	PROGX
	LDS	#$A049
	JSR	0,X
	BRA	RESTRT
BOOT	LDX	#BOOT
	STX	PROGX
	CLRA
	STAA	NDRIVE
	STAA	TRACK
	LDAA	#2
	STAA	SECTOR
	LDX	#$2FFF
	STX	EMEMH
	LDX	#DOS
	BSR	READ
	LDX	#DOS
	STX	PROGX
	JMP	0,X
READ	JSR	PRELIM
READ0	JSR	SEEK
READ2	STX	BMEMH
	CLR	ERRCNT
READ3	LDX	BMEMH
	LDAB	SECTOR
	STAB	SECREG
	JSR	RETURN
	LDAB	#$9C		* MULTIPLE READ W'LOAD
	STAB	COMREG
	JSR	RETURN		* DELAY 14 CYCLES
	JSR	RETURN
	JSR	RETURN
READ4	LDAB	COMREG
	BITB	#1		* BUSY FLAG
	BEQ	READ6
READ5	BITB	#2		* DRQ FLAG
	BEQ	READ4
	LDAA	DATREG
	STAA	0,X
	CPX	EMEMH
	BEQ	READ7
	INX
	BRA	READ4
READ6	BSR	CHKERR
	BNE	READ3
	JSR	INCSEC
	BRA	READ0
READ7	BSR	CHKERR
	BNE	READ3
	JSR	INCSEC
	RTS
CHKERR	LDAB	COMREG
	STAB	STATUS
	LDAA	#$D0		* FORCE INTERRUPT
	STAA	COMREG
	JSR	DONE
	LDAB	STATUS
	ANDB	#$1C
	BNE	CHKER1
RETURN	RTS
CHKER1	ANDB	#$10
	BEQ	CHKER3
	LDAB	SECREG
	CMPB	#10
	BNE	CHKER3
	CLRB
	RTS
CHKER3	LDAB	ERRCNT
	INCB
	STAB	ERRCNT
	CMPB	#6
	BNE	RETURN
	LDAB	SECREG
	STAB	SECTOR
	LDX	#TRACK
	BSR	OUT4HS
	BSR	OUT4HS
	LDX	#CRLF
	BSR	PDATA
	LDX	#CHKMSG
ERROR	BSR	PDATA
	LDX	#ERRMSG
	BSR	PDATA
	JMP	RESTRT

CHKMSG	FCB	$0D,$0A,$15
	FCC	'CHECK' 
	FCB	4
ERRMSG	FCC	' - DISK ERROR'
CRLF	FCB	$0D,$0A,$15,4
RDYMSG	FCB	$0D,$0A,$15
	FCC	'NOT READY'
	FCB	4
PROMSG	FCB	$0D,$0A,$15
	FCC	'PROTECTED' 
	FCB	4

PDATA	JMP	$E07E
OUT4HS	JMP	$E0C8
INCSEC	PSHB
	LDAB	SECREG
	CMPB	#10
	BNE	INSE2
	INC	TRACK
	JSR	SEEK
	CLRB
INSE2	STAB	SECTOR
	STAB	SECREG
	PULB
	RTS
SEEK	LDAB	TRACK
	STAB	DATREG
	JSR	RETURN
	LDAB	#$1B		* LOAD & SEEK
	STAB	COMREG
	JSR	DONE
	BITB	#$10
	BNE	SEEK
	RTS
PRELIM	PSHA
	LDAA	NDRIVE
	JSR	DRIVE
	PULA
	STX	PRELEX
	LDAB	COMREG
	LDX	#$4FFF
PREL25	INX
	DEX
	DEX
	BNE	PREL25
	LDAB	#$0B		* LOAD + RESTORE
	STAB	COMREG
	JSR	DONE
	LDX	#$4000
PREL35	LDAB	COMREG
	DEX
	BEQ	PREL4
	BITB	#$02
	BNE	PREL35
PREL36	LDAB	COMREG
	DEX
	BEQ	PREL4
	BITB	#$02
	BEQ	PREL36
	LDX	PRELEX
	RTS
PREL4	LDX	#RDYMSG
PREL45	JMP	ERROR
WRITE	BSR	PRELIM
WRITE0	BSR	SEEK
WRITE1	CLR	ERRCNT
	STX	BMEMH
WRITE2	LDX	BMEMH
	LDAB	SECTOR
	STAB	SECREG
	JSR	RETURN
	LDAB	#$BC		* MULTIPLE WRITE W'LOAD
	STAB	COMREG
	JSR	RETURN
	JSR	RETURN
	JSR	RETURN
WRITE3	LDAB	COMREG
	BITB	#1		* BUSY FLAG
	BEQ	WRITE6
WRITE4	BITB	#2		* DRQ FLAG
	BEQ	WRITE3
	LDAA	0,X
	STAA	DATREG
	CPX	EMEMH
	BEQ	WRITE7
	INX
	BRA	WRITE3
WRITE6	BITB	#$40
	BEQ	*+7
	LDX	#PROMSG
	BRA	PREL45
	JSR	INCSEC
	BRA	WRITE1
WRITE7	JSR	DONE
	JSR	INCSEC
	RTS
DRIVE	ANDA	#$03
	PSHA
	STX	PRELEX
	LDX	#ATRACK-1
	INCA
	TAB
	LDAA	NDRIVE
	INCA
DRIVE0	INX
	DECA
	BNE	DRIVE0
	LDAA	TRKREG
	STAA	0,X
	LDX	#ATRACK-1
DRIVE1	INX
	DECB
	BNE	DRIVE1
	LDAA	0,X
	STAA	TRKREG
	PULA
	STAA	NDRIVE
	STAA	DRVREG
	LDX	PRELEX
	RTS
DONE	JSR	RETURN
	JSR	RETURN
	LDAB	COMREG
	BITB	#1
	BNE	DONE
	RTS
	END
