;;;
;;; Exatron Stringy Floppy ROM code, from the "ESF Advanced Programmer's Guide".
;;; Believed to be in the public domain.
;;;
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE  1
CR	EQU	0DH
SYNC	EQU	16H
CEI	EQU	0FBH
BSRAM	EQU	401AH
BSINT	EQU	2B02H
BSLIN	EQU	40A2H
BSPRN	EQU	0FAFH
BSMEM	EQU	40B1H
BSERR	EQU	19A2H
BSCONT	EQU	1D1EH
BSFCE	EQU	1E4AH
BSFIND	EQU	1AF8H
BSHL	EQU	40E6H
BSIEXP	EQU	2B1CH
BSINTH	EQU	4012H
BSRST2	EQU	4004H
BSPRTC	EQU	032AH
BSCLR	EQU	1E83H
BSRDY	EQU	1A2BH
BSINC	EQU	1D78H
BSEXE	EQU	1D5BH
BSBGNP	EQU	40A4H
BSENDP	EQU	40F9H
BSMAXP	EQU	40A0H
BSSYNE	EQU	1997H
BSFIX	EQU	1AE8H
BSCSV	EQU	0ADH
BSCLD	EQU	0A7H
BSCNW	EQU	0BBH
BSKI	EQU	4016H
BSKS	EQU	4036H
BSKR	EQU	03FBH
BSDLY	EQU	0060H
BSSFT	EQU	3880H
BSKEY	EQU	3801H
BSBRK	EQU	3840H
;
; *** *** VECTORS *** ***
;
	ORG	3000H
	JP	REWIND
	JP	READX
	JP	WRITE
	JP	WEOFX
	JP	SAVEA
	JP	FBOF
	JP	SELECT
BASIC	LD	HL,(BSHL)
BS1	JP	BSCONT
	JP	FBOFX
	JP	WRITEX
	JP	WEOF
	JP	NEWA
	JP	WBEOF
	JP	ERROR
	JP	GETN
	JP	AUTO
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE  2
;
;*** *** >SYSTEM *** ***
;	 *? /1234N
;
;WHERE N=0 FOR @LOAD
;	 1 FOR @LOAD 1
;	 2 FOR @LOAD 2
;	 3 FOR @LOAD 3
;	 4 FOR @LOAD 4
;	 5 FOR NO LOAD
;	 6 FOR KEYBOUNCE
;
	NOP
START	INC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A
	RET
;*** ADDED BASIC COMMANDS ***
;
;... '@NEW', @NEW1', ETC. ...
;
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE  4
NEW	CALL	GETN
	LD	HL,NEWMSG
	CALL	PRTSTG
	LD	H,C
	CALL	NEWA
	PUSH	AF
	AND	0B7H
	JR	NZ,NW0
	CALL	BSPRN
	LD	HL,BYTEMSG
	CALL	PRTSTG
NW0	POP	AF
	JP	LR6
;
;... '@SAVE1', '@SAVE2', ECT. ...
;
SAVEB	CALL	GETN
SA0	JR	Z,SA1
	DEC	HL
	RST	10H
	JR	Z,SA2
	RST	08H
	DB	','
	CALL	INTGR
	PUSH	DE
	RST	08H
	DB	','
	CALL	INTGR
	PUSH	DE
	LD	DE,BASIC
	JR	Z,SA3
	RST	08H
	DB	','
	CALL	INTGR
	JR	Z,SA3
SA1	JP	BSSYNE
SA2	CALL	BSFIND
	INC	HL
	LD	DE,(BSBGNP)
	SBC	HL,DE
	PUSH	DE
	PUSH	HL
	LD	DE,0000H
SA3	LD	HL,WRITMSG
	CALL	PRTSTG
	LD	A,C
	POP	BC
	POP	HL
	CALL	SAVEA
	JP	LR1
INTGR	PUSH	BC
	CALL	BSINT
	DEC	HL
	RST	10H
	POP	BC
	LD	(BSHL),HL
	RET
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE  5
;
;... '@LOAD'. '@LOAD1', ETC. ...
;
LOAD	CALL	GETN
LOADA	LD	HL,READMSG
	CALL	PRTSTG
	LD	HL,(BSBGNP)
	LD	DE,0064H
	ADD	HL,DE
	LD	DE,(BSMAXP)
	SBC	HL,DE
	PUSH	HL
	LD	HL,(BSLIN)
	INC	HL
	LD	A,H
	OR	L
	JR	Z,LD1
	LD	HL,(BSBGNP)
	LD	DE,(BSENDP)
	SCF
	SBC	HL,DE
	EX	(SP),HL
LD1	POP	IY
	LD	HL,0000H
	PUSH	HL
	LD	H,C
	LD	L,0FFH
	LD	A,C
	OR	A
	JR	NZ,LD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	CALL	BSPRTC
	PUSH	IY
	POP	HL
	CALL	BSPRN
	JR	BFD
;
;*** SUBROUTINES FOR ASMB CALL ***
; VECTORED FROM 3000H ETC.
;
; ALL RETURN WITH Z FOR CORRECT
; NZ FOR ERROR WITH REG.A =
; BIT0 --- WRITE WITHOUT DECAL
; BIT1 --- USER HIT BREAK
; BIT2 --- WRITE PASS EOT
; BIT3 --- READ PARTY ERROR
; BIT4 --- READ CHECKSUM ERROR
; BIT5 --- RECORD TOO LONG
; BIT6 --- VERIFY ERROR
; BIT7 --- EOF READ
;
;...WIND TAPE TO BOT ...
;
REWIND	PUSH	BC
	PUSH	DE
	CALL	MTON
	CALL	DELAY
RWDL	LD	A,(BSBRK)
	AND	04H
	RRCA
	JR	NZ,RWDF
	IN	A,(C)
	AND	04H
	JR	Z,RWDL
	XOR	A
RWDF	CALL	MTOFF
	POP	DE
	POP	BC
	RET
;
;... ERASE TAPE ...
; H = STARTING FILE NUMBER
; HL RETURNS NUMBER OF BYTES
;
NEWA	PUSH	BC
	PUSH	DE
	CALL	MTONW
	AND	01H
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE  8
	JR	NZ,NW11
	LD	A,H
	CP	02H
	JR	NC,NW2
	CALL	REWIND
	JR	NZ,NW11
	LD	A,85H
	LD	(BSRAM),A
	OUT	(C),A
	CALL	DELAY
	LD	L,0FFH
	CALL	WRPRE
	JR	NZ,NW11
	JR	NW3
NW2	CALL	RPREAM
	JR	NZ,NW11
	LD	A,D
	ADD	A,H
	JR	NZ,NW2
NW3	LD	B,10H
NW4	DJNZ	NW4
	LD	L,80H
	CALL	WPREAM
	JP	NZ,NW11
	LD	E,5AH
NW6	LD	H,0FFH
	LD	D,H
	CALL	WRBIT
	JP	Z,NW6
NW7	LD	B,01H
	CALL	WRBIT
	DEC	E
	JP	NZ,NW7
	LD	B,01H
	OUT	(C),B
NW8	CALL	RPREAM
	JR	NZ,NW11
	LD	A,D
	CP	L
	JR	NZ,NW8
	LD	HL,0FFFFH
NW9	INC	HL
	IN	A,(C)
	AND	04H
	JR	NZ,NW10
	CALL	RDBYTE
	JP	NZ,NW10
	PUSH	AF
	POP	AF
	INC	D
	LD	A,08H
	JR	Z,NW9
NW10	AND	0FBH
NW11	CALL	MTOFF
	POP	DE
	POP	BC
	RET
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE  9
;
;... READ DATA BLOCK ...
; HL --> BUFFER
; BC = BUFFER LENGTH AT ENTRY
; RECORD LENGTH AT RETURN
;
READX	PUSH	IX
	PUSH	DE
	PUSH	HL
	POP	IX
	PUSH	HL
	PUSH	IY
	PUSH	BC
	LD	A,C
	CPL
	LD	C,A
	LD	A,B
	CPL
	LD	B,A
	PUSH	BC
	POP	IY
	CALL	MTON
RLP	CALL	RPREAM
	JR	NZ,RRTN
	LD	A,D
	OR	A
	LD	A,04H
	JR	NZ,RRTN
	LD	A,(IX)
	CALL	RDTWO
	JR	NZ,RRTN
	LD	H,D
	EX	DE,HL
	ADD	IY,DE
	EX	DE,HL
	JR	C,BUFE
	RET	C
	NOP
	PUSH	HL
	POP	HL
	CALL	RBLOCK
	JR	NZ,RRTN
	POP	DE
	ADD	IY,DE
	INC	IY
	PUSH	IY
RRTN	CALL	MTOFF
	POP	BC
	POP	IY
	POP	HL
	POP	DE
	POP	IX
	RET
BUFE	LD	A,20H
	OR	A
	JR	RRTN
;
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 10
;... WRITE DATA RECORD ...
; HL --> BUFFER
; BC = BUFFER LENGTH
; D = DELAY TIME
;
WRITEX	PUSH	IX
	PUSH	HL
	POP	IX
	PUSH	HL
	PUSH	DE
	PUSH	BC
	CALL	MTONW
	JR	NZ,WRTN
	LD	L,00H
	CALL	WPREAM
	JR	NZ,WRTN
	POP	HL
	PUSH	HL
	LD	A,(IX+00H)
	CALL	WRTWO
	JR	NZ,WRTN
	NOP
	POP	HL
	PUSH	HL
	CALL	WBLOCK
WRTN	CALL	MTOFF
	POP	HL
	POP	DE
	PUSH	HL
	CALL	Z,ONDLY
	POP	BC
	POP	HL
	POP	IX
	RET
WRITE	PUSH	DE
	LD	D,3DH
	CALL	WRITEX
	POP	DE
	RET
;
;...WRITE BUFFER & EOF ...
; HL --> BUFFER
; BC = BUFFER LENGTH
; A = FILL NUMBER
;
WBEOF	PUSH	AF
	LD	A,B
	OR	C
	CALL	NZ,WRITE
	POP	AF
	JR	WEOF
;... WRITE EOF MARK ...
; A= FILE NUMBER
;
WEOFX	DEC	A
WEOF	PUSH	BC
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 11
	PUSH	DE
	PUSH	HL
	CPL
	OR	80H
	LD	L,A
	CALL	MTONW
	CALL	Z,WRPRE
	CALL	MTOFF
	POP	HL
	POP	DE
	POP	BC
	RET
;
;... WRITE PROGRAM FILE ...
; HL = LOAD ADDRESS
; DE = START ADDRESS
; BC = LENGTH
; A = FILE # (1 THRU 9)
;
SAVEA	PUSH	IX
	PUSH	IY
	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	HL
	POP	IX
	PUSH	HL
	LD	L,A
	PUSH	HL
	PUSH	DE
	POP	IY
	PUSH	BC
	CALL	MTONW
	JR	NZ,SVEJ
SV1	CALL	RPREAM
	JR	NZ,SVEJ
	LD	A,D
	ADD	A,L
	JR	NZ,SV1
	LD	B,10H
SV2	DJNZ	SV2
	CALL	WPREAM
	JR	NZ,SVEJ
	ADC	HL,DE
	PUSH	IX
	POP	HL
	CALL	WRTWO
	JR	NZ,SVEJ
	ADC	HL,DE
	PUSH	IY
	POP	HL
	CALL	WRTWO
SVEJ	JR	NZ,SVE
	ADC	HL,DE
	ADC	HL,DE
	POP	HL
	CALL	WRTWO
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 12
	JR	NZ,SVE1
	PUSH	HL
	POP	HL
	NOP
	CALL	WBLOCK
	JR	NZ,SVE1
	POP	HL
	POP	IX
	LD	A,L
	CPL
	LD	L,A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RMWARE FOR TRS-80	       PAGE 14
	POP	DE
	POP	DE
	CALL	Z,ONDLY
	POP	BC
	RET
FBOF	PUSH	DE
	LD	D,61
	CALL	FBOFX
	POP	DE
	RET
;
;... SELECT DRIVE ...
; AF = DRIVE NUMBER
;
SELECT	PUSH	BC
	OR	0F0H
	LD	C,A
	IN	A,(C)
	AND	8
	LD	A,C
	POP	BC
	RET	NZ
	PUSH	HL
	LD	HL,(BSMEM)
	INC	HL
	LD	(HL),A
	POP	HL
	RET
;
;...PRINT ERROR MESSAGE ...
; A = ERROR CODE
;
ERROR	PUSH	HL
	PUSH	AF
	CALL	PRTERR
	POP	AF
	POP	HL
	RET
;
;*** *** STRINGS *** ***
;
HEAD	DB	'EXAT'
	DB	'RON '
	DB	'STRI'
	DB	'NGY '
	DB	'FLOP'
	DB	'PY V'
	DB	'ERSI'
	DB	'ON 4'
	DB	'.1',CR+80H
NEWMSG	DB	'ERAS'
	DB	'ING.'
	DB	'.'+80H
BYTEMSG DB	' BYT'
	DB	'ES.','.'+80H
WRITMSG DB	'WRIT'
	DB	'ING.'
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 15
	DB	'.'+80H
READMSG DB	'READ'
	DB	'ING.'
	DB	'.'+80H
VRFMSG	DB	'VERI'
	DB	'F','Y'+80H
PERRMSG DB	'PARI'
	DB	'T','Y'+80H
CERRMSG DB	'CHEC'
	DB	'KSU','M'+80H
OMMSG	DB	'OUT '
	DB	'OF M'
	DB	'EMOR'
	DB	'Y'+80H
TTSMSG	DB	'TAPE'
	DB	' TOO'
	DB	' SHO'
	DB	'R','T'+80H
WPMSG	DB	'WRIT'
	DB	'E-PR'
	DB	'OTEC'
	DB	'TE','D',+80H ;;; minor bug in original: should be 'TE','D'+80H
EOFMSG	DB	'EO','F'+80H
ERRMSG	DB	' ERR'
	DB	'OR',CR+80H
BRKMSG	DB	'BREA'
	DB	'K',CR+80H
DONEMSG DB	'DONE'
	DB	CR+80H
;
;*** *** SUB-SUBROUTINES *** ***
;
;PRINTER ERROR MESSAGE
;
PRTERR	BIT	0,A
	LD	HL,WPMSG
	JR	NZ,PE1
	BIT	1,A
	LD	HL,BRKMSG
	JR	NZ,PRTSTG
	BIT	2,A
	LD	HL,TTSMSG
	JR	NZ,PE1
	BIT	3,A
	LD	HL,PERRMSG
	JR	NZ,PE1
	BIT	4,A
	LD	HL,CERRMSG
	JR	NZ,PE1
	BIT	5,A
	LD	HL,OMMSG
	JR	NZ,PE1
	BIT	6,A
	LD	HL,VRFMSG
	JR	NZ,PE1
	LD	HL,EOFMSG
	BIT	7,A
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 16
	RET	Z
PE1	CALL	PRTSTG
	LD	HL,ERRMSG
;
;PRINT STRING POINTED BY HL
;
PRTSTG	LD	A,(HL)
	INC	HL
	PUSH	AF
	AND	7FH
	CALL	BSPRTC
	POP	AF
	RLA
	JR	NC,PRTSTG
	RET
;
;WRITE PREAMBLE, SYNC
; BYTE IN L AND TWO MORE
WRPRE	CALL	WPREAM
	RET	NZ
	RET	NZ
	LD	B,04H
WEWE	DJNZ	WEWE
	JP	WRTWO
;
;WRITE PREAMBLE, SYNS
; AND BYTE IN L
;*** 14 ***
WPREAM	LD	A,85H
	LD	(BSRAM),A
	OUT	(C),A
	CALL	DELAY
	RET	NZ
	CALL	WRBITS
	RET	NZ
	NOP
	JR	WRBLK
WRBLK	CALL	WRBIT
	RET	NZ
	RET	NZ
	LD	B,04H
WRKWT2	DJNZ	WRKWT2
	LD	A,(BSRAM)
	AND	7FH
	OUT	(C),A
	NOP
	NOP
	LD	B,05H
	LD	D,SYNC
	CALL	WRBYTE
	RET	NZ
	LD	D,L
	LD	E,B
	LD	B,04H
	JP	WRBY
WRLOP	NOP
	NOP
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 17
;
;WRITE BLOCK OF HL
;POINTED BY IX
;***	78/14	***
WBLOCK	LD	D,(IX+00H)
	CALL	WRBYT
	RET	NZ
	INC	IX
	DEC	HL
	LD	A,H
	OR	L
	JR	NZ,WRLOP
	RET	NZ
	LD	B,01H
	SUB	A
	SUB	E
	LD	D,A
	CALL	WRBYTE
	RET	NZ
	RET	NZ
	NOP
	LD	B,04H
	CALL	WRBYTE
	RET	NZ
	RET	NZ
	LD	B,05H
WRBY	NOP
	NOP
;
;WRITE BYTE IN D
;*** 13B+37/14 ***
;*** 42/14 ***
WRBYTE	DJNZ	WRBYTE
WRBYT	LD	A,(BSRAM)
	OUT	(C),A
	LD	A,D
	ADD	A,E
	LD	E,A
	LD	B,08H
	INC	HL
	CALL	WRBITS
	DEC	HL
	RET	NZ
	RET	NZ
	LD	B,02H
WRTWT	DJNZ	WRTWT
	LD	A,(BSRAM)
	OR	80H
	LD	(BSRAM),A
	AND	7FH
	OUT	(C),A
	XOR	A
	RET
;
;WRITE B BYTES IN D
;*** 116,99,75/28 ***
WRBITL	LD	A,(IX+00H)
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 18
WRBITS	IN	A,(C)
	AND	05H
	RET	NZ
WRBIT	PUSH	BC
	LD	A,(BSRAM)
	RRC	D
	JR	NC,WRZRO
	XOR	80H
	OUT	(C),A
WRCLK	XOR	80H
	NOP
	NOP
	CP	A
	LD	B,06H
WRBWT	DJNZ	WRBWT
	LD	(BSRAM),A
	OUT	(C),A
	POP	BC
	DJNZ	WRBITL
	RET
WRZRO	NOP
	JP	WRCLK
;
;WRITE TWO BYTES IN HL
;*** 63/14 ***
WRTWO	LD	D,L
	CALL	WRBYT
	RET	NZ
	NOP
	NOP
	NOP
	LD	D,H
	LD	B,04H
	JP	WRBYTE
;
;SEARCH PREAMBLE AND
; SYNC, READ BYTE IN D
;*** 81 ***
RPREAM	CALL	PR1
	RET	NZ
	LD	A,D
	SUB	SYNC
	JP	NZ,RPREAM
	INC	A
	CALL	WAIT
	CALL	RDBYTE
	LD	(BSRAM),A
	RET
PR1	LD	B,14H
PR2	CALL	CHKBRK
	IN	A,(C)
	JP	P,PR2
PR3	CALL	CHKBRK
	IN	A,(C)
	JP	M,PR3
PR4	IN	A,(C)
	JP	M,PR1
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 19
PR5	IN	A,(C)
	JP	P,PR5
	LD	A,06H
	LD	A,06H
	CALL	WAIT
	IN	A,(C)
	JP	P,PR1
PR6	IN	A,(C)
	JP	M,PR6
	JP	PR7
PR7	LD	A,05H
	CALL	WAIT
	DJNZ	PR4
	INC	B
	IN	A,(C)
	JP	P,PR5
PR8	IN	E,(C)
	JP	M,PR8
	LD	A,00H
	JP	RB1
;
;READ BLOCK OF HL
;POINTED BY IX
;*** 46/91 ***
RBLOCK	CALL	RDBYTE
	RET	NZ
	RET	NZ
	LD	(IX),D
	INC	IX
	DEC	HL
	NOP
	PUSH	HL
	POP	HL
	LD	A,H
	OR	L
	JP	NZ,RBLOCK
	CALL	RDBYTE
	RET	NZ
	LD	A,02H
	CALL	WAIT
	CALL	RDBYTE
	RET	NZ
	LD	A,(BSRAM)
	OR	A
	RET	Z
	LD	A,10H
	RET
;
;READ BYTE IN D
;*** 29/58 ***
RDBYTE	IN	A,(C)
	JP	M,RB0
	LD	A,08H
	OR	A
	RET
RB0	IN	E,(C)
	JP	M,RB0
; EXATRON STRINGY FLOPPY FIRMWARE FOR TRS-80	       PAGE 20
	LD	A,(BSRAM)
	ADD	A,D
RB1	LD	(BSRAM),A
	LD	B,8
	LD	B,8
RB2	NOP
	LD	A,3
	CALL	WAIT
	LD	A,E
	CPL
	IN	E,(C)
	JP	M,RB5
RB4	IN	E,(C)
	JP	P,RB4
	JP	RB6
RB5	IN	E,(C)
	JP	M,RB5
	JP	RB6
RB6	XOR	E
	RLCA
	RR	D
	XOR	A
	DJNZ	RB2
	RET
;
;READ TWO BYTES INTO
; L AND D
;***	46/58	***
RDTWO	CALL	RDBYTE
	RET	NZ
	LD	L,D
	NOP
	LD	A,2
	CALL	WAIT
	JP	RDBYTE
;
;*** 16A+43 ***
;*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