	ORG	100H
;
;
	JMP	VECTOR
;************************************************************
CONIN	JMP	DUMYADR
CONOUT	JMP	DUMYADR
HOME:	JMP	DUMYADR
SELDSK:	JMP	DUMYADR
SETTRK:	JMP	DUMYADR
SETSEC1:	JMP	DUMYADR
SETDMA:	JMP	DUMYADR
READ:	JMP	DUMYADR
WRITE:	JMP	DUMYADR
;************************************************************
;EQUATES
CONTROLLER	EQU	0F800H
WRCONT		EQU	CONTROLLER+600H
RDSTAT	EQU	WRCONT
EXITCPM	EQU	0
SDLASTSEC	EQU	26
DDLASTSEC	EQU	52
BUFF		EQU	CONTROLLER+400H
DENBYTE		EQU	BUFF+1
CONTROLBYTE	EQU	BUFF+3
PRESDISK	EQU	BUFF+5
BSECTOR		EQU	BUFF+0AH
BDMA		EQU	BUFF+0BH
BTWOSIDE	EQU	BUFF+0FH
TRY1		EQU	BUFF+20H
RETRYCOUNT	EQU	BUFF+21H
TRACK		EQU	BUFF+4
DISKREAD	EQU	CONTROLLER+12H
DISKWRITE	EQU	CONTROLLER+15H
WRITEPROTECT	EQU	CONTROLLER+1EH
;************************************************************
;
DRIVE	DB	'  DRIVE $'
MESGA:	DB	0DH,0AH,'+ MEMORY COMPARE ERROR ON TRACK $'
ERM:	DB	0DH,0AH,'+ ERROR ON TRACK $'
MEM	DB	13,10,'MEMORY ADDRESS   $'
MESGB:	DB	'(HEX) SECTOR (HEX)$'
MESGC:	DB	13,10,'**********     PERMANENT      **********$'
MESGD:	DB	13,10,'+ READ ERROR  $'
MESGE:	DB	13,10,'+ WRITE ERROR  $'
MESGF:	DB	13,10,'WRITING  $'
MESGG:	DB	'      SECTOR RETRIES     $'
MESGH:	DB	13,10,'PASS  $'
MESGI:	DB	13,10,'SHOULD TEST REPEAT?  Y OR N $'
SINGLE	DB	13,10,'TEST DISK IS SINGLE DENSITY$'
DOUBLE	DB	13,10,'TEST DISK IS DOUBLE DENSITY$'
SIGNON	DB	13,10,'+OBJECT ON '
OBJECTMESS	DS	1
SINOFF:	DB	13,10,'+TYPE <RET>$'
DATAM	DB	'           DATA     $'
REPMES	DB	13,10,'TYPE RETURN (OR R TO REPEAT)$'
CRLF:	DB	13,10,'$'
INIT	 DB	13,10,'SINGLE AND DOUBLE DENSITY DISKTEST PROGRAM (F800 SYSTEM)$'
OBJECT	DB	13,10,'OBJECT DRIVE? A,B,C,D$'
;
PRTHEX:	PUSH	PSW	;SAVE FOR LSN
	RAR		;SHIFT MSNIBLE TO LSN
	RAR
	RAR
	RAR
	CALL	PRTNBL	;PRINT IT
	POP	PSW	;NOW FOR LSN
PRTNBL:	ANI	0FH
	ADI	30H	;SHIFT TO ASCII VALUE
	CPI	3AH	;OVER 9?
	JC	SML
	ADI	7	;SHIFT TO ALPHA
SML:
	MOV	C,A	;FOR BDOS CALL
	CALL	CONOUT
	RET
;
;
FILL	LXI	H,BUF0
FILL2	SHLD	BUFFY
	CALL	SECOUNT
	LHLD	LASTSEC128
	XCHG
	LHLD	BUFFY
	LDA	TESTBYTE
	MOV	C,A
FILLOOP	MOV	M,C
	INX	H
	DCX	D
	MOV	A,E
	ORA	D
	JNZ	FILLOOP
	RET
GETBYTE	LXI	H,BYTETABLE
	LDA	LASTIME
	INR	A
	ANI	07
	STA	LASTIME
	MOV	E,A
	MVI	D,0
	DAD	D
	MOV	A,M
	STA	TESTBYTE
	RET
BYTETABLE	DB	0,81H,7EH,0C3H,3CH,0FFH,55H,0E5H
MREAD:	CALL	CLEAR
RETRYREAD	DI
	CALL	DISKREAD
	EI
	MVI	A,0
	RZ
	CALL	ERROR
	JNZ	RETRYREAD
	MVI	A,01
	RET
MWRITE	CALL	WRITEPROTECT
	JNZ	SKIPG
	MVI	C,0AH
	CALL	CONOUT
	MVI	C,'P'
	CALL	CONOUT
LOOPR	CALL	WRITEPROTECT
	JZ	LOOPR
SKIPG	CALL	CLEAR
RETRYWRITE	DI
	CALL	DISKWRITE
	EI
	MVI	A,0
	RZ
	CALL	ERROR
	JNZ	RETRYWRITE
	MVI	A,01
	RET
CLEAR	XRA	A
	STA	TRY1
	STA	RETRYCOUNT
	RET
ERROR	JC	TRACKERROR
	LDA	RETRYS
	ORA	A
	INR	A
	DAA
	STA	RETRYS
	JNC	CONT
	LDA	RETRYS+1
	ORA	A
	INR	A
	DAA
	STA	RETRYS+1
	JNC	CONT
	LDA	RETRYS+2
	ORA	A
	INR	A
	DAA
	STA	RETRYS+2
CONT	LXI	H,RETRYCOUNT
	INR	M
	MOV	A,M
	CPI	20
	RET
TRACKERROR	LXI	H,TRY1
	INR	M
	MOV	A,M
	CPI	10
	RZ
	LDA	TRACK
	MOV	C,A
	PUSH	B
	CALL	HOME
	POP	B
	CALL	SETTRK
	ORI	0FFH
	RET
PRINTERRORS	LDA	PERROR
		ORA	A
		RZ
	LXI	D,MESGG
	CALL	PRINT
	LDA	RETRYS+2
	CALL	PRTHEX
	LDA	RETRYS+1
	CALL	PRTHEX
	LDA	RETRYS
	CALL	PRTHEX
	RET
COPY:	CALL	HOME
	MVI	A,10H
	STA	TWOSIDES
	STA	ERR1
	LDA	TRKSRT
	STA	TRK
RDLOOP	XRA	A
	STA	CMPERR
TRYAGAIN	CALL	FILL
	LDA	TRK
	MOV	C,A
	CALL	WRITET	;WRITE TRACK
	LDA	TRK
	MOV	C,A
	CALL	READT	;READ TRACK JUST WRITTEN
	CALL	COMPT
	JNZ	TRYAGAIN	;RETRY IF ERR
	LDA	DENMEM
	ORA	A
	JZ	SKIP
	LDA	SIDES
	ORA	A
	JNZ	SKIP
	LDA	TWOSIDES
	ORA	A
	JNZ	SIDE1
	MVI	A,10H
	STA	TWOSIDES
SKIP	LDA	TRK
	INR	A
	STA	TRK
	LXI	H,TRKSRT+1	;POINT TO LAST TRACK+1 STORAGE
	CMP	M	;ARE WE DONE?
	JNZ	RDLOOP	;LOOP IF NOT
	LDA	CONTROLBYTE
	ORI	80H
	STA	CONTROLBYTE
	STA	BTWOSIDE
	RET
SIDE1	XRA	A
	STA	TWOSIDES
	JMP	RDLOOP
;
SIDETEST	LDA	SIDES
		ORA	A
		RNZ
		LDA	DENMEM
		ORA	A
		RZ
		LDA	TWOSIDES
		ORA	A
		JNZ	SIDEZERO
		LDA	CONTROLBYTE
		ANI	7FH
		STA	CONTROLBYTE
		STA	BTWOSIDE
		RET
SIDEZERO	LDA	CONTROLBYTE
		ORI	80H
		STA	CONTROLBYTE
		STA	BTWOSIDE
		RET
COMPT:	LDA	ERR1
	ORA	A
	JZ	SKIP2
	LXI	H,BUF1
	CALL	FILL2	;REREAD INTO BUF1
	LHLD	LASTSEC128
	MOV	B,H
	MOV	C,L
	LXI	H,BUF0
	LXI	D,BUF1
CMPLP:	LDAX	D
	CMP	M
	JNZ	CERR
	INX	H
	INX	D
	DCX	B
	MOV	A,C
	ORA	B
	JNZ	CMPLP
	RET
CERR:	PUSH	H
	PUSH	D
	PUSH	B
	LXI	D,MESGA
	CALL	PRINT
	LDA	TRK
	CALL	PRTHEX
	LXI	D,MESGB
	CALL	PRINT
	POP	H
	DCX	H
	DAD	H
	LDA	LASTSEC
	SUB	H
	CALL	PRTHEX
	LXI	D,MEM
	CALL	PRINT
	POP	H
	MOV	A,M
	STA	DATA1
	PUSH	H
	MOV	A,H
	CALL	PRTHEX
	POP	H
	MOV	A,L
	CALL	PRTHEX
	MVI	C,','
	CALL	CONOUT
	POP	H
	MOV	A,M
	STA	DATA2
	PUSH	H
	MOV	A,H
	CALL	PRTHEX
	POP	H
	MOV	A,L
	CALL	PRTHEX
	LXI	D,DATAM
	CALL	PRINT
	LDA	DATA1
	CALL	PRTHEX
	MVI	C,','
	CALL	CONOUT
	LDA	DATA2
	CALL	PRTHEX
SKIP2	MVI	A,10H
	STA	ERR1
	LDA	CMPERR
	INR	A
	STA	CMPERR	;INCREMENT ERROR COUNT
	CPI	10
	RNZ
	LXI	D,MESGC
	CALL	PRINT
	XRA	A
	RET
;
DMASET:	PUSH	B
	MOV	C,L
	MOV	B,H
	CALL	SETDMA
	POP	B
	RET
SECOUNT	LDA	TRK
	MOV	C,A
	CALL	SETTRK
	LDA	DENBYTE
	ORA	A
	MVI	A,SDLASTSEC
	JZ	SET26
	MVI	A,DDLASTSEC
SET26	STA	LASTSEC
	CALL	SECSET
	RET
;
;
READT:	LXI	H,BUF0	;TRACK # IN C
RT2:	SHLD	DMAAD
	CALL	SETTRK
	CALL	SIDETEST
	MVI	C,0
RT3:	PUSH	B
	INR	C
	MOV	A,C
	STA	SECTOR
	STA	BSECTOR
	LHLD	DMAAD
	SHLD	BDMA
	LXI	D,128
	DAD	D
	SHLD	DMAAD
	CALL	MREAD
	RAR		;WAS A BIT 0 SET BY DISK ERROR?
	CC	FAILR
	POP	B
	INR	C
	LDA	LASTSEC
	CMP	C
	RZ
	JMP	RT3
;
;
WRITET:	LXI	H,BUF0	;TRACK # IN C
WT2:	SHLD	DMAAD
	CALL	SETTRK
	CALL	SIDETEST
	MVI	C,0
WT3:	PUSH	B
	CALL	WRITETRAN
	CALL	MWRITE
	RAR		;WAS A BIT 0 SET BY DISK ERROR?
	CC	FAILW
	POP	B
	INR	C
	LDA	LASTSEC
	CMP	C
	RZ
	JMP	WT3
TRKSRT	DW	0
CMPERR:	DB	0	;NUMBER OF COMPARE ERRORS
TESTBYTE:	DS	1
TRK:	DB	0
RETRYS	DB	0,0,0
LASTIME	DB	7
LASTSEC	DS	1
LASTSEC128	DS	2
BUFFY		DS	2
OBJECTDRIVE	DS	1
DENTEST		DS	1
DATA1	DS	1
DATA2	DS	1
DMAAD:	DS	2
PASSCOUNT	DB	0
CONTINUE	DB	0
SIDES	DS	1
PERROR	DB	0
TWOSIDES	DS	1
DENMEM	DS	1
ERR1	DS	1
SECTOR	DS	1
STK:	DS	64
STKTOP:	DB	0
;
SECSET	LDA	LASTSEC
	MOV	L,A
	MVI	H,0
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	SHLD	LASTSEC128
	RET
;
;
;
;************************************************************
VECTOR:
;SET UP JUMP VECTORS FOR  ACCESS TO BIOS PRIMITIVES
BASE	EQU	0	;FIND WBOOT JMP ADDRESS AT RST0
DUMYADR	EQU	0	;WHO CARES?
;
	LHLD	BASE+1	;GET WBOOT ADDRESS
	SPHL		;SAVE IT IN SP FOR DAD
	LXI	H,06
	DAD	SP
	SHLD	CONIN+1
	LXI	H,09H
	DAD	SP
	SHLD	CONOUT+1
;
	LXI	H,15H
	DAD	SP
	SHLD	HOME+1
;
	LXI	H,18H
	DAD	SP
	SHLD	SELDSK+1
;
	LXI	H,1BH
	DAD	SP
	SHLD	SETTRK+1
;
	LXI	H,1EH
	DAD	SP
	SHLD	SETSEC1+1
;
	LXI	H,21H
	DAD	SP
	SHLD	SETDMA+1
;
	LXI	H,24H
	DAD	SP
	SHLD	READ+1
;
	LXI	H,27H
	DAD	SP
	SHLD	WRITE+1
;
	LXI	SP,STKTOP
RUNTYP:	JMP	ALLSET
DONMSG:	DB	13,10,13,10,'********    TEST COMPLETE    ********$'
ALLSET	MVI	L,0		;FIRST TRACK TO TRANSFER
	MVI	H,77		;LAST TRACK PLUS ONE
PUTPAR:	SHLD	TRKSRT		;PUT PARAMETERS IN TRKSRT AND TRKSRT+1
	CALL	COMPTEST
;************************************************************
REPEAT	LXI	SP,STKTOP
	LXI	D,INIT
	CALL	PRINT
	LXI	D,OBJECT
	CALL	PRINT
OBJECTLOOP	CALL	CONIN
	CPI	'A'
	JZ	SETOBJECT
	CPI	'B'
	JZ	SETOBJECT
	CPI	'C'
	JZ	SETOBJECT
	CPI	'D'
	JZ	SETOBJECT
	JMP	OBJECTLOOP
SETOBJECT	STA	OBJECTMESS
	SBI	41H
	STA	OBJECTDRIVE
	LXI	D,SIGNON
	CALL	PRINT
AGIN	CALL	CONIN
	CPI	3
	JZ	EXITCPM
	CPI	13
	JNZ	AGIN
	CALL	TESTREPEAT
	LXI	D,CRLF
	CALL	PRINT
;************************************************************
	LDA	OBJECTDRIVE
	MOV	C,A
	CALL	SELDSK
	CALL	HOME
	MVI	C,2
	CALL	SETTRK
	LDA	RDSTAT
	ANI	40H
	STA	SIDES
	LDA	DENBYTE
	STA	DENMEM
TESTLOOP1	CALL	DENSITY
TESTLOOP	CALL	GETBYTE
	LXI	D,MESGF
	CALL	PRINT
	LDA	TESTBYTE
	CALL	PRTHEX
	CALL	PRINTERRORS
	CALL	COPY
	LDA	TESTBYTE
	CPI	0E5H
	JNZ	TESTLOOP
	LXI	D,MESGH
	CALL	PRINT
	LDA	PASSCOUNT
	ORA	A
	INR	A
	DAA
	STA	PASSCOUNT
	CALL	PRTHEX
	LDA	CONTINUE
	ORA	A
	JNZ	TESTLOOP1
	LXI	D,DONMSG	;SAY WE'RE DONE
	CALL	PRINT
	JMP	EXIT		;DONE. NOW GET OUT
TESTREPEAT	LXI	D,MESGI
	CALL	PRINT
	CALL	CONIN
	CPI	'Y'
	RNZ
	MVI	A,10H
	STA	CONTINUE
	RET
COMPTEST	LDA	82H
	CPI	'Z'
	RNZ
	MVI	A,55H
	STA	PERROR
	RET
FAILR:	LXI	D,MESGD
	JMP	DIE
FAILW:	LXI	D,MESGE
DIE	CALL	PRINT
	LXI	D,ERM
	CALL	PRINT
	LDA	TRK
	CALL	PRTHEX
	LXI	D,MESGB
	CALL	PRINT
	LDA	SECTOR
	CALL	PRTHEX
	LXI	D,DRIVE
	CALL	PRINT
	LDA	PRESDISK
	ADI	41H
	MOV	C,A
	CALL	CONOUT
	XRA	A
	STA	ERR1
	RET
DENSITY	LDA	DENBYTE
	ORA	A
	JZ	SDMESS
	LXI	D,DOUBLE
	CALL	PRINT
	RET
SDMESS	LXI	D,SINGLE
	CALL	PRINT
	RET
;************************************************************
EXIT:	LXI	D,REPMES
	CALL	PRINT
	CALL	CONIN
	CPI	'R'
	JZ	REPEAT
	CPI	13
	JNZ	EXIT
	MVI	C,0
	CALL	SELDSK
	JMP	EXITCPM
PRINT	LDAX	D
	CPI	'$'
	RZ
	PUSH	D
	MOV	C,A
	CALL	CONOUT
	POP	D
	INX	D
	JMP	PRINT
READTRAN	LDA	DENBYTE
	ORA	A
	JZ	SDSKEW
	LXI	H,READTABLE
	MVI	B,0
	DAD	B
	MOV	C,M
	PUSH	B
	CALL	SETSEC
	POP	B
	CALL	FINDDMA
	RET
SETSEC	MOV	A,C
	STA	SECTOR
	CALL	SETSEC1
	RET
WRITETRAN	LDA	DENBYTE
	ORA	A
	JZ	SDSKEW
	LXI	H,READTABLE
	MVI	B,0
	DAD	B
	MOV	C,M
	PUSH	B
	CALL	SETSEC
	POP	B
	CALL	FINDDMA
	RET
;SDSKEW	LXI	H,SDTABLE
	MVI	B,0
	DAD	B
	MOV	C,M
	PUSH	B
	CALL	SETSEC
	POP	B
	CALL	FINDDMA
	RET
SDSKEW	INR	C
	MOV	A,C
	STA	SECTOR
	STA	BSECTOR
	LHLD	DMAAD
	SHLD	BDMA
	LXI	D,128
	DAD	D
	SHLD	DMAAD
	RET
FINDDMA	MVI	H,0
	DCR	C
	MOV	L,C
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	DAD	H
	XCHG
	LHLD	DMAAD
	DAD	D
	CALL	DMASET
	RET
SDTABLE		DB	1,3,5,7,9,11,13,15,17,19,21,23,25
		DB	2,4,6,8,10,12,14,16,18,20,22,24,26
READTABLE	DB	1,3,5,7,9,11,13,15,17,19,21,23,25
		DB	27,29,31,33,35,37,39,41,43,45,47
		DB	49,51,2,4,6,8,10,12,14,16,18,20
		DB	22,24,26,28,30,32,34,36,38,40
		DB	42,44,46,48,50,52
WRITETABLE	DB	1,5,9,13,17,21,25,29,33,37,41
		DB	45,49,2,6,10,14,18,22,26,30,34
		DB	38,42,46,50,3,7,11,15,19,23,27
		DB	31,35,39,43,47,51,4,8,12,16,20
		DB	24,28,32,36,40,44,48,52
BUF0	DS	128*DDLASTSEC
BUF1	DS	128*DDLASTSEC
