;
; This Boot Strap program is intended to be used in the Boot ROM
; of the Davidge 460 series Single Board Computer.  It initializes the 
; first two serial ports, their related baud rate generators, and
; the floppy disk DMA and the FDC interrupt generator.
;
; After the above initialization, a message is output to SIO channel
; A, asking for a floppy disk to be inserted into the A: drive.
; It then waits until the diskette is mounted before continuing.
; If there is timeout on the disk, and no disk appears to be present,
; control is passed to the Davidge Boot Rom Monitor.
;
; After the floppy disk is mounted, the drive is restored and the 
; drive type (5.25" or 8") is determined.  Then, the drive is stepped
; in with a verify option to determine the density of the diskette.
; After a successful step-in is completed, a READ ADDRESS command
; is executed to obtain the sector size of the diskette.
;
; At this time, the drive is restored to track 00.  The first sector
; of the diskette is then read into locations 80h to 0ffh of memory.
; Finally, control is then passed to address 80h.
;
; To assist the loader program (resident on track 00 sector 1), the
; following parameters are passed:
;
;	(A) = Drive Select Bits
;	includes drive type and density
;	(D) = Sector Size
;	(E) = Suggested step rate indicator
;		1 (6 msec) if 8" drive detected
;		3 (30 msec) if 5.25" drive detected
; These parameters assist in developing a general purpose loader
; for the computer.
;
false:	equ	0
true:	equ	not false
;
; For the following equate, set romtyp = 1 for normal Davidge boot rom;
;   romtyp=2 for Advanced Controls boot rom;
;   romtyp=3 for Pioneer Data Systems boot rom
;
romtyp:	equ	1
;
pioneer: set	false	; Pioneer Data Systems boot rom
advc:	set	false	; Advanced Controls boot rom
mon:	set	false	; Normal Davidge boot rom
;
  if	romtyp eq 1
mon:	set	true
  endif
  if	romtyp eq 2
advc:	set	true
  endif
  if	romtyp eq 3
pioneer: set	true
  endif
;
	MACLIB	Z80
	maclib	flopdef
	maclib	portdef
	maclib	sasidef
;
ROM:	EQU	0
RAM:	EQU	0
DBUF:	EQU	80H
hdbuf:	equ	800h
;
romport:  equ	1ah	; rom control port for Rev B board
;
RUBOUT:	EQU	07FH
CONTU:	EQU	015H
CONTR:	EQU	012H
BACKSP	EQU	08H
PROMPT:	EQU	02DH
cr:	equ	13
lf:	equ	10
;
  page
;
	ORG	0
BOOT:	LXI	H,ROM
	sphl
	LXI	D,RAM
	LXI	B,2048
	LDIR
START:	MVI	A,1	; turn off the boot rom
	out	romport
	IM2	; select interrupt mode 2
	MVI	A,CTC0V SHR 8
	STAI
	call	tdly
	LXI	H,TABLE2	; bring up the IO ports
	LXI	SP,TABLE1
	MVI	D,ports
ST1:	POP	B
	OUTIR
	DCR	D
	JRNZ	ST1
	lxi	h,msg	; output the log-on message
st1a:
	jmp	load0
;
reqwt:	lxi	d,0	; set up timer loop
reqwt1:	in	hastat	; wait for request bit
	ani	req
	rnz
	dcr	d	; check the time out loop
	mov	a,d
	ora	e
	jrnz	reqwt1
	out	hareset	; reset the controller
	ret
;
tries:	db	10	; 10 attempts before aborting to monitor
	ORG	40H
IDSV:	DW	0,0,0
SECT:	DB	1
CUNIT:	DB	41H
;
INTDMA:
	DB	0C3H	; RESET
	DB	079H	; START ADDR AND BLOCK LENGTH ENABLE
			; SELECT IN AND OUT PORTS
DMAADR:	DW	hdbuf	; DMA BLOCK START ADDRESS
DMALN:	DW	256-1	; BYTE COUNT-1
	DB	014H	; ADDR INCR ON PORT A
	DB	028H	; ADDR ON B FIXED,I/O
	DB	085H	; PORT B ADDR FOLLOWS,BYTE MODE
DMPORT:	DB	HADATA	; PORT B ADDRESS
	DB	092H	; READY ACTIVE LOW
	DB	0CFH	; LOAD
DMASTGL: EQU	$-INTDMA ; LENGTH OF STRING
;
STATUS:	DB	0
CMD:	DB	0
;
	org	58h	; must remain at this location
CTC0V:	EQU	$
	dw	isr
	dw	isr
	dw	isr
	DW	CTCISR
;
CTCISR:
	IN	dcmmd
	STA	STATUS
isr:	EI
	RETI
;
  if	mon or pioneer
COMTBL:	DB	'DSPGF'	; Table of valid command letters
  endif
;
cmdbuf:	db	read	; command byte
lun:	db	0
addhb:	db	0	; address high byte
addlb:	db	0	; address low byte
blkcnt:	db	1	; block count
ctrl:	db	0	; control byte (step mode, etc.)
;
;
; DMA Initialization Command String
;
DMASTG:	DB	0C3H
	DB	79H
dmaad:	dw	idsv
dmalen:	dw	6-1	; read 6 bytes of ID
	DB	14H
	DB	28H
	DB	85H
	DB	ddata
	DB	0CFH
	DB	82H
	DB	87H
DSTGLGT:	EQU	$-DMASTG
;
TABLE1:
	DB	CTC0,3	; Serial A baud rate generator
	DB	CTC2,2	; Serial B baud rate generator
	DB	CTC3,2	; FDC mode 2 interrupt generator
	DB	SIOAC,6	; Main console serial port
	DB	SIOBC,6	; Printer serial port
ports:	equ	($-table1)/2
TABLE2:
	db	ctcmode	; set console baud rate gen mode
  if	pioneer
	db	3	; 38,400 baud
  else
	db	sbaud	; defauld baud
  endif
	DB	CTC0V MOD 256  ; interrupt vector

	DB	ctc2mode,pbaud	; printer baud
	DB	0D7H,1	; FDC IM2 generator
	DB	4,44H,3,0C1H,5,0EAH  ; SIO A 8 data, 1 stop, no par, auto enab
	DB	4,44H,3,0C1H,5,0EAH  ; SIO B 8 data, 1 stop, no par, auto enab
;
msg:
  if	pioneer
	db	cr,'Pioneer Data Systems'
	db	cr,'      10/01/84',0
  endif
  if	advc
	db	cr,'Advanced Controls'
	db	cr,'    09/01/84',0
  endif
  if	mon
	db	cr,'Davidge DSB-4000 V',ver or '0','.',rev or '0',0
  endif
;
	ORG	100H
load0:
	LXI	SP,RAM
	call	dismsg	; log onto the system
load:
	mvi	a,10
	sta	tries
load1:
	EI
	call	flopload
  if	not advc
	call	tdly
  endif
	call	sasiload	; try the sasi
	lxi	h,tries
	dcr	m
	jrnz	load1
	jmp	dsmon
	page
flopload:
	mvi	a,51h	;try a mini drive first
	MOV	B,A
	OUT	dctrl1
	MVI	A,8	;RESTORE COMMAND (6 MS STEP RATE)
flop1:
	push	b
	call	type1
	pop	b
	bit	3,a
	rnz		; abort if error
	bit	2,a	; see if trk00 reporting
	mvi	a,0bh	;prepare for slow restore
	jrz	flop1	;go do it if necessary
	in	dctrl2	;now, see if mini or 8"
	rar
	mvi	a,3	;set mini step rate
	jrnc	flop2
	mvi	b,41h	;set 8"
	mvi	a,1
flop2:	sta	cmd
	ori	5Ch	;make into a step in command
	mov	d,a	;save for a moment
	mov	a,b	;get the selbits
	out	dctrl1	;select the right drive type
	sta	cunit
	mov	a,d	;reget the step-in command
	call	type1
	ani	098h	;isolate the error bits
	jrz	flop3	;brif ok
	lda	cunit
	xri	40h	; try other density
	out	dctrl1
	sta	cunit
	mov	a,d
	call	type1
	ani	098h
	rnz		;abort if needed
flop3:	MVI	A,0c4h	; read address command
	lxi	h,idsv
	lxi	d,5
	call	fread
	ANI	9CH
	rnz		; abort if error
	lda	cunit	;reget the selbits
	ani	0bfh
	out	dctrl1	;set single density
	lda	cmd	;reget the step rate
	setb	3,a	;make into a restore
	call	type1	;go back to trk00
	mvi	a,1	;read t 0 s 1
	STA	SECT
	OUT	dsctr
	lxi	h,dbuf	; set up the dma string for read sector
	lxi	d,128-1	; byte count
	MVI	A,88H
	call	fread
	ANI	9CH
	rnz		; abort if error
	DI
	lda	cmd	; get the step rate
	mov	e,a
	lda	idsv+3	; get the sector size
	mov	d,a
	lda	cunit	; get the select bits
	JMP	DBUF	; pass to the loader
;
  page
;
; Boot Loader for the Davidge 460 series SASI host adapter
;
sasiload:
	out	hareset	; reset the controller
	call	tdly
	in	hastat	; see if host adapter on-line
	inr	a
	rz	; go try floppy
	call	reads
	jrz	sasi1	; brif good read
	cpi	noready	; see if drive not ready
	jrz	sasiload  ; brif so
	rnz	; try again if error
sasi1:	lxi	h,hdbuf	; see if sysgenned
	mvi	a,0e5h
	cmp	m
	rz	; try floppy if not sysgenned
	pchl	; else, go to loader
;
READS:
	mvi	a,read
	MVI	B,DMASTGL ; DMA STRING LENGTH
	MVI	C,CPUDMA  ; DMA ADDRESS PORT
	LXI	H,INTDMA  ; DMA STRING POINTER
	OUTIR	; INITIALIZE THE DMA CHIP
	mvi	c,hadata	; set up for sasi address
	CALL	TASKOUT	; START THE SASI INTERFACE
	mvi	a,87h	; release the dma
	out	cpudma
	LXI	D,0
	MVI	B,10	; SET UP FOR LONG TIME-OUT
LOADDMA2:
	IN	HASTAT	; WAIT FOR COMMAND*INPUT*REQUEST 
	ANI	019H  ;MASK REQ, COMMAND AND INPUT
	XRI	019H  ;MAKE SURE THAT THERE ALL THERE
	JRZ	LOADDMA3;LOOP TILL THEY ARE  
	DCX	D
	MOV	A,D
	ORA	E
	JRNZ	LOADDMA2
	DJNZ	LOADDMA2
LOADDMA3:
	MVI	A,0C3H	; DISABLE THE DMA
	OUT	CPUDMA
CHKSTAT:
	CALL	GETSTAT
	RZ	; DONE IF NO EROR
chkst0:
	MVI	A,SENSRQ
	CALL	TASKOUT
 	LXI	H,ERRBUF
	MVI  B,4
chkst1:
	call	reqwt
	jrz	chkst0
	INI
	JRNZ  chkst1  
	CALL	GETSTAT
	LDA	ERRBUF
	ani	7fh	; strip off the valid address bit
	rz	; done if status ok
	cpi	cecc	; correctable ECC is OK
	RET
;
tdly:
	lxi	h,1000	; do a short time out
sasi0:	dcx	h
	mov	a,h
	ora	l
	jrnz	sasi0
	ret
;
; TASKOUT OUTPUTS A 6 BYTE COMMAND TO THE CONTROLLER
; INPUT PARAMETERS:  (H,L) POINTS TO START OF COMMAND IN MEMORY
; OUTPUT PARAMETERS: (A) IS DESTROYED
;	(BC) IS DESTROYED
;	(HL) = (HL) + 6
task0:	lda	cmdbuf
TASKOUT:
	LXI	H,CMDBUF	; POINT TO THE COMMAND BUFFER
	MOV	M,A	; STORE THE COMMAND
	lxi	d,0	; set up for timer loop
TASK1:
	IN	HASTAT	; SEE IF CONTROLLER IS BUSY
	ANI	BUSY	; TEST IT
	JRZ	TASK2  ; BRIF NOT BUSY
	dcx	d	; check the timer loop
	mov	a,d
	ora	e
	jrnz	task1
task1a:	out	hareset	; reset the controller
	jr	task1
task2:	MVI	A,1
	OUT	HADATA	; OUTPUT SELECT BYTE
	OUT	HACTRL	; GET THE CONTROLLER'S ATTENTION
	lxi	d,0
task3:
	IN	HASTAT	; INSURE HE SAW US
	ANI	BUSY
	JRNZ	TASK4	; BRIF HE ACKNOWLEDGES
	dcx	d
	mov	a,d
	ora	e
	jrnz	task3
	jr	task1a
task4:	MVI	B,6
task5:	call	reqwt
	jrz	task0
task6:	OUTI
	JRNZ  task5
	RET
;
; GETSTAT READS THE STATUS BYTE AND THE NULL BYTE PROVIDED AT THE END
;	OF EVERY OPERATION.  IT CHECKS THE STATUS BYTE FOR ERORS AND
;	SETS THE ZERO FLG TO INDICATE A GOOD COMPLETION.  THE ZERO FLG
;	IS RESET IF AN EROR OR PARITY ERROR OCCURRED
; INPUT PARAMETERS:	NONE
; OUTPUT PARAMETERS:	(A) = 0 IF NO ERORS
;	(A) = 1 IF PARITY EROR
;	(A) = 2 IF EROR
;	(A) = 3 IF EROR AND PARITY ERROR
;	(D) IS DESTROYED
GETSTAT:
	call	reqwt
	jrz	getst3	; take error
	IN	HADATA	; READ THE STATUS BYTE
	mov	d,a	; save for a moment
	push	d
	call	reqwt
	jrnz	getst4
	pop	d
getst3:	mvi	d,0ffh	; take an error
	jr	getst5
getst4:	IN	HADATA	; CLEAR IT OUT
	pop	d
getst5:	MVI	A,ERROR
 	ANA	D	; CHECK FOR ERORS
	RET
;
  page
;
; More floppy disk routines
;
fread:	shld	dmaad
	xchg
	shld	dmalen
	LXI	H,DMASTG
	MVI	C,dmacmd
	MVI	B,DSTGLGT
	OUTIR
type1:	EI
	OUT	dcmmd
	mvi	a,0ffh	; set interrupt flag
	sta	status
	lxi	h,400h
type1a:	djnz	type1a
	lda	status	;see if command done yet
	bit	0,a
	rz		; return if done
;
type1b:	dcx	h
	mov	a,h
	ora	l
	jrnz	type1a
	dcr	a
	ret
;
; THIS SUBROUTINE SIMPLY OUTPUTS A "#", A CARRIAGE RETURN, AND A SPACE.
;
;	REGISTER ALTERED:  A
;
  if	mon or pioneer
hatch:	lxi	h,spmsg
  endif
;
; THIS ROUTINE OUTPUTS A MESSAGE TO THE SCREEN.  THE MESSAGE MAY BE OF ANY
; LENGTH AS LONG AS IT IS STORED IN CONSECUTIVE BYTES AND TERMINATES WITH
; A BYTE CONTAINING ZERO.
;
;	INPUT PARAMETER:  HL-POINTS TO 1ST BYTE OF MESSAGE
;	REGISTERS ALTERED:  A, HL
;
DISMSG:	MOV	A,M	; GET CHARACTER
	ORA	A	; TEST IF ZERO TERMINATOR
	RZ	; RETURN IF DONE
	CALL	CHROUT	; ELSE, SEND TO SCREEN
	INX	H	; INCREMENT PTR
	JR	DISMSG	; GET NEXT BYTE
;
; THIS IS A SUBROUTINE TO OUTPUT CHARACTERS TO THE CONSOLE, OR SCREEN.
; IF A <CR> IS TO BE OUTPUT, A <LF> IS ALSO OUTPUT.
;
;	INPUT PARAMETER:  A-ASCII CHARACTER TO BE DISPLAYED
;	OUTPUT PARAMETER:  A-ASCII CHARACTER THAT WAS DISPLAYED
;	REGISTERS ALTERED:  NONE
;
CHROUT:	PUSH	PSW	; SAVE CHARACTER
	cpi	cr	; see if carriage return
	jrnz	chro1	; brif not
	mvi	a,lf	; do a line feed
	call	chrout
CHRO1:	IN	SIOAC	; GET I/O CHANNEL STATUS
	ANI	4	; TEST IF READY
	JRZ	CHRO1	; LOOP UNTIL READY
	POP	A	; RETRIEVE CHARACTER
	OUT	SIOAD	; OUTPUT ON DATA LINE
	RET
;
; THIS IS A SUBROUTINE TO RECEIVE CHARACTER INPUT FROM THE CONSOLE, OR KEY-
; BOARD.
;
;	OUTPUT PARAMETER:  A-CHARACTER THAT WAS KEYED IN
; 	REGISTERS ALTERED:  A
;
CHRIN: 	IN	SIOAC	; GET I/O CHANNEL STATUS
	ANI	1	; TEST IF CHARACTER AVAILABLE
	JRZ	CHRIN	; LOOP TILL ONE IS
	IN	SIOAD	; GET CHARACTER
	ANI	07FH	; Remove extraneous bit
	cpi	'a'	; check for lower case
	rc
	cpi	'z'+1
	rnc
	ani	5fh	; convert to upper case
	RET
;
DSMON:
	mvi	a,10	; set up for 10 attempts
	sta	tries
  if	advc
	lxi	h,retrymsg
  	call	dismsg
load2:
	call	chrin
	cpi	cr
	jrnz	load2
	call	chrout
	jmp	load
  endif
  if	mon or pioneer
;
; This module is the Davidge Boot Rom Monitor.  Control passes to it when there
; is a timeout on the lack of disk boot.  A message is displayed which explains
; why the monitor was called in and gives a brief summary of the five commands
; available.  Then, the main loop of this module is continually executed.  Com-
; mands are received, the parameters are collected, and the appropriate command
; routines are executed.
;
	LXI	H,SIGNON  	; Point to sign-on message and display it
;
compro1:	CALL	DISMSG
comproc: lxi	h,comproc	; set up return address
	push	h
	MVI	A,PROMPT	; Display prompt
	CALL	CHROUT
	CALL	LININ	; Get command line
;
; Test for valid command letter.
	LXI	H,LINBUF	; Point to line buffer
	MOV	A,M	; Get 1st char. entered
	CPI	'H'
	JRZ	DSMON	; If request for help, go do it
	cpi	'B'
	jz	load
	STA	COMCHR	; Save it
	LXI	B,5	; # of valid command letters
	LXI	H,COMTBL	; Ptr to table of valid command letters
	CCIR		; Compare
	JRNZ	EROR	; If not a valid command, go to error routine
;
; The parameters are pulled out of the command, counted, and converted from
; ASCII characters into word values.  For a parameter with less than four
; ASCII characters, there is zero fill on the left.  More than four characters
; is a syntax error.  Use of any delimiters between the parameters other than
; spaces or commas is also a syntax error.
	LXI	H,LINBUF	; Point to 1st parameter character
	LXI	D,PARAM	; Point to block for parameter storage
	MVI	C,0	; Zero out the parameter count value
;
; Put parameters in parameter block, with zero fill.
COUNTP:	MVI	B,0FFH	; Zero out the bytes per param. count
	PUSH	H	; Save ptr to this parameter
COUNTB:	inr	b
	inx	h
	MOV	A,M	; Get byte
	CALL	ASCHEX	; Check for ASCII digit
	JRZ	COUNTB	; If not, check if delimeter
DELIM:	POP	H	; Retrieve parameter ptr
	adi	'0'	; put the ascii bias back on it
	CPI	','	; Test for valid delimeter
	JRZ	DELIM1
	CPI	' '
	JRZ	DELIM1
	CPI	cr	; Test for <CR>
	JRNZ	EROR	; If not, there is a syntax error
;
DELIM1:	STA	DELIMC	; Store delimeter char.
	mvi	a,4	; no more than 4 characters
	sub	b	; see if in range
	jrz	fillp	; brif full count
	jrc	eror	; brif too many
	PUSH	B	; Save byte count
	MOV	B,A	; Put in B count of bytes to zero fill
	MVI	A,'0'	; ASCII character for zero
FILLZ:	STAX	D	; Put byte in parameter block
	INX	D
	DJNZ	FILLZ	; Continue till done
	POP	B	; Retrieve byte count
;
FILLP:	INX	H	; Increment ptrs
	MOV	A,M	; Get parameter byte
	STAX	D	; Put in parameter block
	INX	D
	DJNZ	FILLP	; Loop till done
	INX	H	; point to delimiter character
	INR	C	; Increment parameter count
	mov	a,m	; get the delimiter
	CPI	cr	; Test for <CR>, test if done
	JRZ	PACK	; Jump ahead if so
	MOV	A,C	; Else, test for too many parameters
	CPI	4
	JRC	COUNTP	; If not, get next parameter
;
; THIS ERROR ROUTINE SIMPLY DISPLAYS A QUESTION MARK AND CONTROL IS RETURNED TO
; THE MAIN COMMAND PROCESSING LOOP.
;
EROR:	lxi	h,qmsg	; ERROR INDICATOR
	JMP	COMPRO1	; GO TO MAIN LOOP
qmsg:	db	'?',cr,0
;
; Currently, each parameter is represented by a four byte hex string in the
; parameter block.  These will be converted to word values which will be
; stored in the parameter block, overwriting the ASCII strings.
PACK:	LXI	H,PARAM1-1	; Point to 1st parameter
	CALL	ASCHEXW	; Convert it
	SDED	PARAM1	; Store it
;
; Repeat for the other two parameters.
	CALL	ASCHEXW
	SDED	PARAM2
	CALL	ASCHEXW
	SDED	PARAM3
;
; At this point, the command character has been saved, the parameters have been
; converted to word values and saved, and the parameter count has been stored
; in the C register.  The specific command routine may now be executed.
	LDA	COMCHR	; Get command letter
	CPI	'D'	; Test for which command to do and go to the
	JRZ	DISPLAY	;  appropriate command routine.
	CPI	'S'
	JZ	SETM
	CPI	'P'
	JZ	PORT
	CPI	'G'
	JZ	GO
;
; This is where the FILL command is executed.  The first parameter is the
; starting address of a block of memory, the second parameter is the block
; ending address, and the third parameter is a byte value to store in all the
; bytes in the memory block.
FILL:	MOV	A,C	; Check for correct # of parameters
	CPI	3
	JRNZ	EROR
	LHLD	PARAM3	; Insure that 3rd parameter is a byte value
	xra	a
	ora	h
	JRNZ	EROR
	MOV	A,L	; Get fill byte
	LDED	PARAM1	; Get block beginning and ending addresses
	LHLD	PARAM2
;
FILLOP:	STAX	D	; Fill byte
	INX	D	; Point to next
	PUSH	H	; Save end address
	ORA	A	; Clear carry
	DSBC	D	; Test if done
	POP	H	; Restore end address
	JRNC	FILLOP	; If not done, continue
	RET		; Go get another command
;
; This is where the DISPLAY command is done.  The first parameter is the
; starting address of memory to be displayed.  It is rounded down to be on a
; 16 byte boundry.  The second parameter, if entered, is the ending address in
; memory.  If not entered, it will be computed such that 16 lines of 16 bytes
; each are displayed.  If entered, it is rounded up to be on a 16 byte boundry.
; Each display line has the address of the first byte in the line, 16 bytes of
; data, and the ASCII of those bytes(a "." is used for non-displayable char-
; acters).  If the display is excessively long, it can be terminated by en-
; tering a <CR> from the keyboard.
DISPLAY:	MOV	A,C	; Check for correct # of parameters
	dcr	a
	jrz	eror
	cpi	2
	JRNC	EROR
	inr	a
	PUSH	A	; Save count and flags
	LHLD	PARAM1	; Get start address
	MOV	A,L	; Round it down
	ANI	0F0H
	MOV	L,A
	XCHG
	POP	A	; Retrieve count and flags
	LXI	B,16	; Default # of display lines
	JRZ	DISPREP	; Jump if default used
	LHLD	PARAM2	; Else, compute # of lines
	ORA	A	; Clear carry
	DSBC	D
	PUSH	D	; Save start address
	LXI	D,010H
	DAD	D	; Approximate # of bytes to be displayed
	POP	D	; Retrieve start address
	SRLR	H	; Divide by 16 to get # of lines
	rarr	l
	SRLR	H
	rarr	l
	SRLR	H
	rarr	l
	SRLR	H	; Move hi L nibble to low
	rarr	l
	PUSH	H
	POP	B
;
;
; At this point, BC contains a count of lines to display and DE contains the
; start address of first byte to display.  The display loop which follows sets
; up and displays each line and checks the keyboard input to test for halting
; display.
DISPREP:PUSH	B	; Save count of display lines in alternate
	EXX	;  register set, as the value is only used at
	POP	B	;  the end of the loop
	EXX
;
; Display a line of bytes and other info.
DISLIN:	MVI	A,' '	; Space character
	MVI	B,69	; # of bytes in display line buffer
	LXI	H,DISBUF	; Point to display line buffer
	PUSH	H	; Save ptr
CLRLOP:	MOV	M,A	; Fill display buffer with space char. and
	INX	H	;  put <CR> at end
	DJNZ	CLRLOP
	MVI	M,cr
	POP	H	; Point to display buffer
	MVI	B,16	; # of bytes per display line
;
; Display address of first byte of line.
	PUSH	D	; Save address pointer twice
	PUSH	D
	CALL	HEXASCW	; Put ASCII address in buffer
	INX	H	; Leave space after address
	POP	D	; Retrieve address
;
; Display 16 bytes.
DISBYT:	LDAX	D	; Get byte from address
	PUSH	D	; Save address
	CALL	HEXASCB	; Convert to ASCII
	MOV	M,D	; Put in display buffer
	INX	H
	MOV	M,E
	INX	H
	INX	H	; Leave space after byte
	POP	D	; Retrieve address
	INX	D	; Increment address
	DJNZ	DISBYT
;
; Display bytes in ASCII.
	POP	D	; Get address of 1st byte in line
	MVI	B,16	; Count of bytes
DISCHR:	LDAX	D	; Get byte
	CALL	DISASC	; Check if displayable
	MOV	M,A	; Put in buffer
	INX	H
	INX	D
	DJNZ	DISCHR	; Loop till done
	LXI	H,DISBUF	; Point to buffer
	CALL	LINOUT	; Send line to screen
;
; Test if a <CR>, for stopping display, has been entered.
	IN	SIOAC	; Get channel status
	ANI	1	; Test if char. available
	JRZ	LINTST	; If not, jump ahead
	IN	SIOAD	; Get character
	ANI	07FH	; Remove extraneous bit
	CPI	cr	; Test for <CR>
	RZ		; Else, get new command
	cpi	13h	; see if x-off
	cz	chrin	; suspend if so
;
; Test if all lines displayed.
LINTST:	EXX
	DCX	B	; Decrement count of display lines
	MOV	A,B	; Test for zero
	ORA	C
	EXX
	JRNZ	DISLIN
	RET		; If done, get another command
;
; The SET command is executed here.  One parameter is entered, the address of
; the first(and maybe only) byte to be displayed and possibly altered.  The
; address of the byte and the contents are displayed.  The user then has the
; option of what to do.  If a <CR> is entered, the next address and byte are
; displayed.  If a period is entered followed by a <CR>, the command is
; terminated.  If a byte value followed by a <CR> is entered, the current
; memory address is loaded with that value and the next address and byte are
; displayed.  All other inputs are syntax errors.
SETM:
	dcr	c
	JRNZ	EROR1
	LDED	PARAM1	; Get initial memory address
;
; Following is the change memory loop for this command.  First, display the
; memory address and its value.
CHGLOP:	PUSH	D	; Save current memory address
	LXI	H,LINBUF	; Point to output line buffer
	CALL	HEXASCW	; Convert address to ASCII and put in buffer
	POP	D	; Retrieve address
	MVI	M,' '	; ASCII space after address
	INX	H	; Point to next buffer location
	LDAX	D	; Get byte value from memory location
	PUSH	D	; Save address again
	CALL	HEXASCB	; Convert to ASCII
	MOV	M,D	; Put in buffer
	INX	H
	MOV	M,E
	INX	H
	MVI	M,' '	; Put space after byte value
	LXI	H,LINBUF	; Point to beginning of line buffer again
	MVI	B,8	; Count of bytes to display
	CALL	LINOUT	; Output the line
;
; Now wait for an input from user and check it.  Do what is indicated or do an
; error exit.
	CALL	LININ
	LXI	H,LINBUF	; Point to line buffer
	MOV	A,M	; Get byte
	CPI	'.'	; Test if done
	JRNZ	TRYCR	; If not, test if skipping to next address
	POP	D	; Clear stack
	INX	H	; Point to next byte which should be <CR>
	MOV	A,M
	CPI	cr
	RZ		; If so, go get next command
	JR	EROR1	; If not, syntax error
;
TRYCR:	CPI	cr	; Test if <CR> for skipping
	JRNZ	ALTER
	POP	D	; If so, increment address ptr and continue
	INX	D
	JR	CHGLOP
;
ALTER:	POP	D	; Retrieve memory address
	CALL	ASCHEX	; Check for ASCII digit and get byte value
	JRNZ	EROR1	; If not a digit, syntax error
	MOV	B,A	; Save byte value
	INX	H	; Get next input byte
	MOV	A,M
	CALL	ASCHEX	; Check for ASCII and get value
	JRZ	SECDIG	; Go ahead if ASCII digit
	CPI	cr	; If not, test for <CR>
	JRNZ	EROR1	; Syntax error if not
;
CHANGE:	MOV	A,B	; Get new byte value for address
	STAX	D	; Put in memory
	INX	D	; Point to next address and continue
	JR	CHGLOP
;
SECDIG:	SLAR	B	; Multiply 1st digit entered x16
	SLAR	B
	SLAR	B
	SLAR	B
	ADD	B	; Get byte value
	MOV	B,A	; Save it
	INX	H	; Now, test for <CR>
	MOV	A,M	; If not, syntax error
	CPI	cr
	JRZ	CHANGE	; Go and put new value in address
;	JMP	EROR1
EROR1:	lxi	h,qmsg	; ERROR INDICATOR
	JMP	COMPRO1	; GO TO MAIN LOOP
;
;
; The PORT command is done here.  One parameter, the port address, is entered.
; The port address and the read in value for it are displayed.  The user can
; then enter a byte value followed by a <CR> and that value will be output on
; the port, or just a <CR> and nothing will happen.  Any other input is a
; syntax error.
PORT:
	dcr	c
	JRNZ	EROR1
	LHLD	PARAM1	; Insure that parameter is a byte value since
	xra	a
	cmp	h	; insure port number in range
	JRNZ	EROR1
	MOV	C,L	; Set up for input instruction
	MOV	A,L
	call	chr3o
	INP	A	; Get port value
	call	chr3o	; display it
	CALL	LININ	; Wait for user input
	LXI	H,LINBUF	; Point to input line buffer
	MOV	A,M	; Get first byte entered by user
	CPI	cr	; Test if done with command
	RZ		; If so, go get another command
	CALL	ASCHEX	; Check for ASCII digit and get byte value
	JRNZ	EROR1	; If not digit, syntax error
	MOV	B,A	; Save byte value
	INX	H	; Get next input byte
	MOV	A,M
	CALL	ASCHEX	; Check for ASCII digit and get byte value
	jrnz	scnd1	; brif not ASCII digit
SCNDIG:	SLAR	B	; Multiply 1st digit entered by 16
	SLAR	B
	SLAR	B
	SLAR	B
	ADD	B	; Get byte value
	MOV	B,A	; Save it
	INX	H	; Now, test for <CR>
	MOV	A,M	; If not, syntax error
scnd1:	CPI	cr	; If not, test for <CR>
	JRNZ	EROR1	; Syntax error if not <CR>
;
;
OUTPRT:
	OUTP	B	; Output it to port
	RET		; Go get another command
;
;
; The GO command is executed here.  One parameter is entered, the address to
; which program control is to be transferred.
GO:
	dcr	c
	JRNZ	EROR1
	LHLD	PARAM1	; Get execution address
	PCHL	; Pass control to it
; 
;
TESTU:	CPI	CONTU	; TEST FOR LINE REMOVAL
	JRNZ	TESTR	; WELL, IF NOT, TRY AGAIN
	CALL	HATCH	; ELSE, KILL LINE
;	JR	LININ	; RESTART ROUTINE
;
; THIS SUBROUTINE IS MAINLY USED FOR AND INPUT AND DISPLAY OF COMMAND LINES.
; INPUT LINES CAN BE UP TO 32 CHARACTERS IN LENGTH(A <CR> IS INSERTED AS THE
; 33RD CHARACTER).  THE CHARACTERS ARE STORED IN A LINE BUFFER, SO THEY CAN
; BE ACCESSED LATER.  SIMPLE LINE EDITTING IS AVAILABLE IN THE FORM OF 
; RUBOUT FOR REMOVAL OF SINGLE CHARACTERS(WITH ECHO), CONTROL-U FOR REMOVING
; THE ENTIRE LINE, AND CONTROL-R FOR REDISPLAYING A LINE WITHOUT THE ECHOS OF
; RUBBED OUT CHARACTERS.
;
;	REGISTERS ALTERED:  A,B,HL
;
LININ:	LXI	H,LINBUF	; INITIALIZE BUFFER PTR
	MVI	B,32	; SET BUFFER CHARACTER COUNT
;
; THIS IS THE MAIN LOOP IN THIS ROUTINE, WHERE CHARACTERS ARE ENTERED.
CHRLOP:	CALL	CHRIN	; GET CHARACTER FROM KEYBOARD
	CPI	RUBOUT	; TEST FOR RUBOUT CHARACTER
	JRZ	RUB	; IF SO, PROCESS IT
	CPI	BACKSP	; TEST FOR BACKSPACE CHARACTER
	JRNZ	TESTU
;
RUB:	MOV	A,B	; GET CHARACTER COUNT
	CPI	32	; IF NONE, RUBOUT INVALID
	JRZ	CHRLOP
	DCX	H	; MOVE BUFFER PTR BACK
	MVI	A,backsp	; BACKSPACE OVER LAST CHARACTER
	CALL	CHROUT
	INR	B	; INCREMENT CHARACTER COUNT
	JR	CHRLOP
;
TESTR:	CPI	CONTR	; TEST FOR REDISPLAY
	JRNZ	ANYELSE
	CALL	HATCH	; END CURRENT DISPLAY LINE
	MVI	A,32	; COMPUTE # VALID CHARACTERS IN BUFFER
	SUB	B
	JRZ	CHRLOP	; IF NONE, JUST CONTINUE
	PUSH	B	; SAVE BUFFER CHARACTER COUNT
	MOV	B,A
	LXI	H,LINBUF	; POINT TO BEGINNING OF BUFFER
	CALL	LINOUT	; DISPLAY VALID CHARACTERS ONLY
	POP	B	; RESTORE BUFFER COUNT
	JR	CHRLOP	; CONTINUE
;
ANYELSE:MOV	M,A	; PUT CHARACTER IN BUFFER
	INX	H	; POINT TO NEXT SPOT
	CALL	CHROUT	; DISPLAY CHARACTER
	DCR	B	; DECREMENT CHARACTER COUNT
	jrz	any1
	CPI	cr	; IF <CR>, DONE
	RZ
	JR	CHRLOP	; IF NOT, CONTINUE
;
any1:	MVI	A,cr	; PUT IN FINAL <CR> IN BUFFER AT 33RD POSITION
	MOV	M,A
	jmp	chrout
;
chr3o:	CALL	HEXASCB	; Convert port number to ASCII
	MOV	A,D	; Output the port number
	CALL	CHROUT
	MOV	A,E
	CALL	CHROUT
	MVI	A,' '	; Put ASCII space after port number
	JMP	CHROUT
;
; THIS SUBROUTINE OUTPUTS A LINE, OR PART OF A LINE, TO THE SCREEN.  A BUFFER
; POINTER IS GIVEN ALONG WITH A CHARACTER COUNT OF BYTES TO DISPLAY.  IF THE
; COUNT PASSED IS ZERO, CHARACTERS ARE TAKEN FROM THE BUFFER AND DISPLAYED
; UNTIL A <CR> IS ENCOUNTERED.
;
;	INPUT PARAMETERS:  B-CHARACTER COUNT OR ZERO
;	  HL-POINTER TO LINE BUFFER
;	REGISTERS ALTERED:  A,B,HL
;
LINOUT:	XRA	A	; CLEAR A REGISTER AND ZERO GALG
	CMP	B	; TEST FOR ZERO COUNT
	JRNZ	PAROUT	; IF NOT, JUMP TO DISPLAY PARTIAL LINE
LINO1:	MOV	A,M	; GET CHARACTER FROM BUFFER
	CALL	CHROUT	; SEND TO SCREEN
	CPI	cr	; TEST FOR <CR>
	RZ
	INX	H	; INCREMENT BUFFER PTR
	JR	LINO1	; LOOP TILL DONE
;
PAROUT:	MOV	A,M	; GET CHARACTER FROM BUFFER
	CALL	CHROUT	; DISPLAY IT
	INX	H	; INCREMENT BUFFER PTR
	DJNZ	PAROUT	; LOOP TILL DONE
	RET
;
;
; THE PURPOSE OF THIS SUBROUTINE IS TO CONVERT AN ASCII CHARACTER REPRESENTING
; A NIBBLE HEX VALUE INTO THAT VALUE.  EMPROR IS INDICATED IF CHARACTER NOT AN
; ASCII HEX DIGIT.
;
;	INPUT PARAMETER:  A-ASCII CHARACTER
;	OUTPUT PAMPAMETERS:  A-BYTE VALUE, VALID ONLY IF NO ERROR
;	  Z FLAG-SET IF OKAY, NOT SET TO INDICATE EMPROR
;	REGISTER ALTERED:  A
;
ASCHEX:	SUI	'0'	; TEST FOR VALID RANGE 0-9
	RC
	CPI	10
	JRNC	LETTER	; IF NOT, TRY FOR A-F
letter1:	CMP	A	;  RETURN
	RET
;
letter:
	sui	7
	cpi	10
	jrc	letter2
	cpi	16
	jrc	letter1
letter2:
	adi	7
	RET
;
; THIS SUBROUTINE CONVERTS TWO ASCII CHARACTERS INTO A BYTE VALUE.  THERE IS AN
; EMPROR RETURN IF EITHER OF THE ASCII CHARACTERS REPRESENT SOMETHING OTHER THAN
; A HEX DIGIT.
;
;	INPUT PARAMETER:  D,E-ASCII CHARACTERS
;	OUTPUT PARAMETERS:  A-BYTE VALUE, IF VALID RETURN
;	  Z FLAG-SET IF VALID RETURN, NOT SET OTHERWISE
;	REGISTERS ALTERED:  A,D,E
;
ASCHEXB:	inx	h
	MOV	D,M	; GET FIRST TWO CHARACTERS
	INX	H
	MOV	E,M
	MOV	A,D	; GET HIGH ORDER CHARACTER
	CALL	ASCHEX	; CONVERT TO VALUE
	RNZ	; RETURN IF NOT HEX DIGIT
	rlc
	rlc
	rlc
	rlc
	MOV	D,A	; SAVE IT
	MOV	A,E	; GET LOW ORDER CHARACTER
	CALL	ASCHEX	; CONVERT TO VALUE
	RNZ	; RETURN IF INVALID
	ADD	D	; ADD IN HIGH ORDER BYTE
	CMP	A	; INDICATE OK RETURN
	RET
;
;
; THIS SUBROUTINE IS SIMILAR TO THE PRECEEDING ONE EXCEPT THAT FOUR ASCII
; CHARACTERS, POINTED TO BY HL, ARE CONVERTED INTO A WORD VALUE.  THERE IS
; EMPROR RETURN, ALSO.
;
;	INPUT PARAMETER:  HL-POINTER TO 4 BYTE STRING OF ASCII CHARACTERS
;	OUTPUT PARAMETERS:  DE-WORD VALUE
;	  Z FLAG-SET IF VALID RETURN, NOT SET OTHERWISE
;	REGISTERS ALTERED:  A,DE,HL
;
ASCHEXW:  CALL	ASCHEXB	; CONVERT INTO BYTE VALUE
	RNZ	; RETURN IF INVALID
	PUSH	A	; SAVE BYTE VALUE
	CALL	ASCHEXB	; CONVERT last 2 characters
	POP	D
	RNZ
	MOV	E,A	; LOW ORDER BYTE IN E
	RET
;
; THE PURPOSE OF THIS SUBROUTINE IS TO CORRECT A NIBBLE VALUE (4 BITS) INTO AN
; ASCII CHARACTER.
;
;	INPUT PARAMETER:  A-LOWER FOUR BITS CONTAIN NIBBLE
;	OUTPUT PARAMETER:  A-ASCII CHARACTER REPRESENTING NIBBLE
;	REGISTER ALTERED:  A
;
HEXASC:	ANI	0FH	; CLEAR UPPER NIBBLE, JUST IN CASE
	adi	90h
	daa
	aci	40h
	daa
	RET
;
; THE SUBROUTINE CONVERTS A BYTE VALUE INTO 2 ASCII CHARACTERS.
;
;	INPUT PARAMETER:  A-BYTE VALUE
;	OUTPUT PARAMETER:  D,E-ASCII CHARACTERS
;	REGISTERS ALTERED:  A,D,E
;
HEXASCB:PUSH	A	; SAVE BYTE VALUE
	rrc
	rrc
	rrc
	rrc
	CALL	HEXASC	; CONVERT TO ASCII
	MOV	D,A	; SAVE HIGH ORDER CHARACTER
	POP	A	; GET LOWER NIBBLE
	CALL	HEXASC	; CONVERT
	MOV	E,A	; STORE
	RET
;
; THIS SUBROUTINE CONVERTS A WORD VALUE INTO A STRING OF 4 ASCII DIGITS WHICH
; ARE INSERTED IN MEMORY WHERE SPECIFIED.
;
;	INPUT PARAMETERS:  DE-WORD VALUE
;	  HL-POINTER TO WHERE ASCII STRING SHOULD BE STORED
;	OUTPUT PARAMETER:  HL-POINTER TO LOCATION IMMEDIATELY AFTER STRING
;	REGISTERS ALTERED:  A,DE,HL
;
HEXASCW:MOV	A,D	; GET UPPER BYTE
	PUSH	D	; SAVE INPUT WORD
	call	hexw1
	POP	D	; GET LOWER BYTE
	MOV	A,E
hexw1:	CALL	HEXASCB	; CONVERT
	MOV	M,D	; STORE IN STRING
	INX	H
	MOV	M,E
	INX	H
	RET
;
spmsg:	db	'#',cr,' ',0
;
; THIS SUBROUTINE DETERMINES IF THE GIVEN ASCII CHARACTER IS DISPLAYABLE OR
; NOT.  IF IT IS, THE ASCII CHARACTER ITSELF IS RETURNED.  IF NOT, A PERIOD
; IS RETURNED.
;
;	INPUT PARAMETER:  A-ASCII CHARACTER
;	OUTPUT PARAMETER:  A-ASCII CHARACTER GIVEN AS INPUT IF ITS DISPLAYABLE,
;	ASCII PERIOD OTHERWISE
;	REGISTER ALTERED:  A, IF NON-DISPLAYABLE CHARACTER
;
DISASC:	CPI	' '
	JRC	PERIOD
	CPI	rubout
	RC
PERIOD:	MVI	A,'.'
	RET
;
; Below are the variable and string definitions.
;
SIGNON:
	DB	cr,cr,'            * * * * * DISK TIME '
	DB	'OUT * * * * *',cr,cr
	DB	'System can''t boot off disk.  Check disk, reset, and try '
	DB	'again.  If this doesn''t',cr,'work, a ROM monitor has been '
	DB	'provided for use in basic board test functions.',cr
	DB	'Available commands are:',cr,cr
	db	'  B-boot the disk',cr
	DB	'  D-display memory in lines of 16 bytes (D100,300)',cr
	DB	'  S-alter memory, byte by byte (S300)',cr
	DB	'  P-display and alter port values (P40)',cr
	DB	'  G-execute code, go to (G100)',cr
	DB	'  F-fill memory (F500,600,20)',cr
	DB	'  H-help, redisplay this summary',cr,cr
	DB	'Note that altering some values may affect operation of the'
	DB	' ROM monitor.',cr,cr,00H
;
  endif
;
ERRBUF:	DS	4	; SENSE ERROR BUFFER
;
  if	mon or pioneer
LINBUF:
DISBUF:	DS	70	; Buffer for input and output lines
PARAM:	; TABLE FOR COMMAND PARAMETERS
PARAM1:	DS	4
PARAM2:	DS	4
PARAM3:	DS	4
DELIMC:	DS	1	; Storage for delimiter found in input strings
COMCHR:	DS	1	; Storage for command character entered
  endif
  if	advc
retrymsg:  db	cr,lf,'Insert system diskette into Drive A:.'
	db	cr,'Press <CR> when ready to load the system - ',0
  endif
;
	end
