;
;DUMP - INTERPRETED FILE DUMP
;
;INIT PRIVATE STACK
;
START:	ORG	100H
	LXI	H,0
	DAD	SP
	SHLD	STACK
	LXI	SP,STACK	
;
;MODIFY BDOS CALLS TO PROPER ADDRESSES
;
	LDA	2		;GET BIOS HIGH ORDER BYTE
	STA	NOTDON-1
	STA	NOTDON+2
	STA	CONCHR+2
	STA	WAIT+2
	STA	DISCRD+2
;
;IS THERE A FCB?
;
	LDA	FCB+1
	CPI	' '
	JZ	NOFCB
;
;OPEN FILE
;
	LXI	D,FCB
	MVI	C,OPEN
	CALL	BDOS
	INR	A		;OPEN OK ?
	JZ	OPNER
;
;READ SECTOR, DUMP IN HEX
;
RDLP:	LXI	D,FCB
	MVI	C,READ
	CALL	BDOS
	ORA	A		;READ OK?
	JNZ	RDER		;NO, CHECK EOF
;
;DUMP IN HEX
;
	LXI	D,80H		;POINT TO BUFFER
	LHLD	ADDR		;GET ADDR TO PRINT
	DAD	D		;BUMP BY 128
	SHLD	ADDR		;SAVE FOR NEXT SECTOR
LINE:	MOV	A,H		;GET ADDR
	CALL	XO		;TYPE IN HEX
	MOV	A,L		;GET LO ADDR
	CALL	XO		;2ND BYTE
	MVI	A,':'		;PRINT COLON
	CALL	TYPE		;
	CALL	SPACE		;THEN SPACE
	PUSH	D		;SAVE DATA ADDR FOR ASCII
CHAR:	LDAX	D		;GET CHAR
	INX	D		;POINT TO NEXT
	INX	H		;INCR ADDRESS
	CALL	XO		;PRINT IN HEX
	MOV	A,E		;GET ADDR
	ANI	1		;TIME TO SPACE ?
	CZ	SPACE		;..YES
	MOV	A,E		;GET ADDR AGAIN
	ANI	0FH		;END OF LINE?
	JNZ	CHAR
;
;END OF HEX, PRINT ASCII
;
	POP	D		;GET DATA ADDR
	CALL	SPACE		;PRINT IT
ASCIC:	LDAX	D		;GET CHAR
	CPI	' '		;PRINTABLE?
	JC	PER		;..NO
	CPI	7FH		;
	JNC	PER		;DON'T PRINT DELETES
	CPI	5FH		;
	JNZ	OK
PER:	MVI	A,'.'		;PRINT '.' FOR UNPRINTABLE
OK:	CALL	TYPE		;TYPE CHAR
	INX	D
	MOV	A,E		;END OF LINE?
	ANI	0FH
	JNZ	ASCIC		;MORE ASCII CHARS
	MVI	A,13		;GET ASCII C/R
	CALL	TYPE
	MVI	A,10		;GET ASCII L/F
	CALL	TYPE
;
;END OF SECTOR?
;
	MOV	A,E		;GET ADDR
	ANI	7FH		;ISOLATE
	JZ	RDLP		;READ SECTOR
	JMP	LINE
;
;SUBROUTINES
;
;TYPE CHAR IN A
;
TYPE:	PUSH	B
	PUSH	D
	PUSH	H
	MOV	C,A
	CALL	000CH
;
;CHECK FOR ABORT
;
NOTDON:	CALL	0006H		;
	ORA	A
	JZ	NOCHR
CONCHR:	CALL	0009H		;
	CPI	03H		;CONTROL C ?
	JZ	EXIT
	CPI	13H		;CONTROL-S
	JNZ	NOCHR
WAIT:	CALL	0006H
	JZ	WAIT
DISCRD:	CALL	0009H
NOCHR:	POP	H
	POP	D
	POP	B
	RET
;
;HEX OUTPUT
;
XO:	PUSH	PSW		;SAVE CHAR
	RAR
	RAR
	RAR
	RAR
	CALL	NIBBL
	POP	PSW
NIBBL:	ANI	0FH
	CPI	10
	JC	HEXNU
	ADI	7
HEXNU:	ADI	'0'
	JMP	TYPE
;
;HEX OUTPUT W/BLANK
;
XOB:	CALL	XO
;
;SPACE
;
SPACE:	MVI	A,' '
	JMP	TYPE
;
;READ ERROR - CHECK FOR EOF
;
RDER:	DCR	A		;WAS IT 1?
	JNZ	RDERR		;NO, GENUINE READ ERROR
;
;EOF REACHED
;
	JMP	EXIT
NOFCB:	LXI	D,MSG1
	JMP	ERXIT
OPNER:	LXI	D,MSG2
	JMP	ERXIT
RDERR:	LXI	D,MSG3		;READ ERROR 
	JMP	ERXIT
ERXIT:	MVI	C,PRINT
	CALL	BDOS		;PRINT MESSAGE
EXIT:	LHLD	STACK		;GET ORIGINAL STACK
	SPHL			;RESTORE IT
	RET			;--EXIT--
;
;
;
	DS	30		;STACK AREA
STACK	DS	2		;STACK POINTER
MSG1	DB	'NO FILE SPECIFIED IN COMMAND$'
MSG2	DB	'FILE NOT FOUND$'
MSG3	DB	'READ ERROR$'
ADDR	DW	-128		;ADDR PRINTED -128
;
; BDOS EQUATES (VERSION 2)
;
RDCON	EQU	1
WRCON	EQU	2
PRINT	EQU	9
CONST	EQU	11		;CONSOLE STATUS
OPEN	EQU	15		;0FFH=NOT FOUND
CLOSE	EQU	16		;   "	"
SRCHF	EQU	17		;   "	"
SRCHN	EQU	18		;   "	"
ERASE	EQU	19		;NO RET CODE
READ	EQU	20		;0=OK, 1=EOF
WRITE	EQU	21		;0=OK, 1=ERR, 2=?, 0FFH=NO DIR SPC
MAKE	EQU	22		;0FFH=BAD
REN	EQU	23		;0FFH=BAD
STDMA	EQU	26
BDOS	EQU	5
REIPL	EQU	0
FCB	EQU	5CH		;SYSTEM FCB
