;
;This  Boot Strap program is intended to be used in the Boot  ROM
; of the Davidge Corporation 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.
;
;$-PRINT
;
	MACLIB	Z80
;
ROM:	EQU	0
RAM:	EQU	0
DBUF:	EQU	80H
;
false	equ	0
true	equ	not false
;
dma:	equ	10h
DCMD:	EQU	14H
DSTAT	EQU	DCMD
DTRK:	EQU	DCMD+1
DSCTR:	EQU	DCMD+2
DDATA:	EQU	DCMD+3
DCTRL1:	EQU	DCMD+4
dstat1:	equ	dctrl1
DSTAT2:	EQU	dcmd+5
DCTRL2:	EQU	DSTAT2
side0:	equ	0
side1:	equ	80h
;
CTC0:	EQU	8
CTC1:	EQU	CTC0+1
CTC2:	EQU	CTC0+2
CTC3:	EQU	CTC0+3
;
SIOAD:	EQU	0
SIOAC:	EQU	SIOAD+1
SIOBD:	EQU	SIOAD+2
SIOBC:	EQU	SIOAD+3
;
txmty:	equ	4
;
MBANK	EQU	40H
romport:  equ	1ah	; rom control port for Rev B board
;
;
	ORG	0
BOOT:	LXI	H,ROM
	LXI	D,RAM
	LXI	B,2048
	LDIR
START:	MVI	A,1	; turn off the boot rom
	OUT	MBANK
	out	romport
	IM2		; select interrupt mode 2
	MVI	A,CTC0V SHR 8
	STAI
	LXI	H,TABLE2	; bring up the IO ports
	LXI	SP,TABLE1
	MVI	D,ports
ST1:	POP	B
	OUTIR
	DCR	D
	JRNZ	ST1
	LXI	SP,DBUF
	lxi	h,msg	; output the log-on message
st1a:
	call	dismsg
	jmp	st2
;
	ORG	40H
IDSV:	DW	0,0,0
SECT:	DB	1
CUNIT:	DB	41H
;
CTC0V:	EQU	$
CTC1V:	EQU	CTC0V+2
CTC2V:	EQU	CTC0V+4
CTC3V:	EQU	CTC0V+6
	ORG	CTC3V
	DW	CTCISR
;
STATUS:	DB	0
CMD:	DB	0
;
;
; DMA Initialization Command String
;
DMASTG:	DB	0C3H,0C3H,0C3H,0C3H,0C3H,0C3H
	DB	079H
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	47H,0CH,CTC0V MOD 256  ; 9600 baud
	DB	7,34H	; 300 baud
	DB	0D7H,1	; FDC IM2 generator
	DB	4,44H,3,0C1H,5,0EAH  ; SIO A 8 data, 1 stop, no par
	DB	4,44H,3,0C1H,5,0EAH  ; SIO B 8 data, 1 stop, no par
;
msg:	db	13,10,'Davidge Computer Systems',13,10,0
;
	ORG	100H
ST2:	EI
	mvi	a,51h	;try a mini drive first
	MOV	B,A
	OUT	DCTRL1
	MVI	A,8	;RESTORE COMMAND (6 MS STEP RATE)
st3:
	push	b
	call	type1
	pop	b
	bit	3,a
	JRNZ	ST2
	bit	2,a	; see if trk00 reporting
	mvi	a,0bh	;prepare for slow restore
	jrz	st3	;go do it if necessary
;	pop	b
	in	dctrl2	;now, see if mini or 8"
	rar
	mvi	a,3	;set mini step rate 
	jrnc	st3a
	mvi	b,41h	;set 8"
	mvi	a,1
st3a:	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	st4a	;brif ok
	lda	cunit
	xri	40h	; try other density
	out	dctrl1
	sta	cunit
	mov	a,d
	call	type1
	ani	098H
	jrnz	st2	;abort if needed
;
; insert IDRD code here
;
st4a:	MVI	A,0c4h	; read address command
	call	read
	ANI	9CH
	JNZ	ST2
	lxi	h,dbuf	; set up the dma string for read sector
	shld	dmaad
	lxi	h,128-1 ; byte count
	shld	dmalen
	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
	MVI	A,88H
	call	read
	ANI	9CH
	JNZ	ST2
	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
;
read:	LXI	H,DMASTG
	MVI	C,DMA
	MVI	B,DSTGLGT
	OUTIR
type1:	EI
	OUT	DCMD
	mvi	a,0ffh	; set interrupt flag
	sta	status
	lxi	h,1000h
type1a:	djnz	type1a
	lda	status	;see if command done yet
	bit	0,a
	jrnz	type1b	; brif not
	ret
;
type1b:	dcx	h
	mov	a,h
	ora	l
	jrnz	type1a
	jmp	dsmon
;
CTCISR:
	IN	DCMD
	STA	STATUS
	EI
	RETI
;
$+PRINT
;
;
; 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.
;
; Below are the variable and string definitions.
;
;	ORG	300H
SIGNON:	DB	0DH,0DH,'                             DAVIDGE ROM MONITOR',0DH
	DB	'                                   12/25/82',0DH
	DB	0DH,0DH,'                      * * * * * DISK TIME '
	DB	'OUT * * * * *',0DH,0DH
	DB	'System cannot boot off disk.  Check disk, reset, and try '
	DB	'again.  If this doesn''t',0DH,'work, a ROM monitor has been '
	DB	'provided for use in primitive board test functions.',0DH
	DB	'A summary of available commands follows:',0DH,0DH
	DB	'        B-boot the disk',13
	DB	'        D-display memory in lines of 16 bytes (D100,300)',0DH
	DB	'        S-alter memory, byte by byte (S300)',0DH
	DB	'        P-display and alter port values (P40)',0DH
	DB	'        G-execute code, go to (G100)',0DH
	DB	'        F-fill memory (F500,600,20)',0DH
	DB	'        H-help, redisplay this summary',0DH,0DH
	DB	'Note that altering some values may affect operation of the'
	DB	' ROM monitor.',0DH,0DH,0DH,00H
;
COMTBL:	DB	'DSPGF'		; Table of valid command letters
RUBOUT:	EQU	07FH
CONTU:	EQU	015H
CONTR:	EQU	012H
BACKSP	EQU	08H
PROMPT:	EQU	02DH
;
;
DSMON:	LXI	H,SIGNON	; Point to sign-on message and display it
	CALL	DISMSG
;
COMPROC: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
	ANI	5FH		; Remove extraneous bits
	CPI	'H'
	JRZ	DSMON		; If request for help, go do it
	cpi	'B'
	jz	st2
	STA	COMCHR		; Save it
	LXI	B,5		; # of valid command letters
	LXI	H,COMTBL	; Ptr to table of valid command letters
	CCIR			; Compare
	JRNZ	ERROR		; 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+1	; 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,0		; Zero out the bytes per param. count
	PUSH	H		; Save ptr to this parameter
COUNTB:	MOV	A,M		; Get byte
	CALL	ASCHEX		; Check for ASCII digit
	JRNZ	DELIM		; If not, check if delimiter
	INR	B		; Else, increment byte count
	INX	H		; Point to next byte
	JR	COUNTB
;
DELIM:	POP	H		; Retrieve parameter ptr
	CPI	','		; Test for valid delimiter
	JRZ	DELIM1
	CPI	' '
	JRZ	DELIM1
	CPI	0DH		; Test for <CR>
	JRNZ	ERROR		; If not, there is a syntax error
;
;
DELIM1:	STA	DELIMC		; Store delimiter char.
	MOV	A,B		; Test for too many bytes in parameter(>4)
	CPI	5
	JRNC	ERROR		; If so, syntax error
	CPI	0		; Test for no bytes in parameter
	JRZ	ERROR		; If so, syntax error
	MVI	A,4		; Test if any zero fill needed
	SUB	B
	JRZ	FILLP		; If not, go fill parameter block
;
	PUSH	B		; Save byte count
	MOV	B,A		; Put in B count of bytes to zero fill
	MVI	A,030H		; ASCII character for zero
FILLZ:	STAX	D		; Put byte in parameter block
	INX	D
	DJNZ	FILLZ		; Continue until done
	POP	B		; Retrieve byte count
;
FILLP:	MOV	A,M		; Get parameter byte
	STAX	D		; Put in parameter block
	INX	H		; Increment ptrs
	INX	D
	DJNZ	FILLP		; Loop until done
;
	INX	H		; Skip delimiter character
	INR	C		; Increment parameter count
	LDA	DELIMC		; Retrieve delimiter character
	CPI	0DH		; 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
;	JR	ERROR		; If so, syntax error
;
; THIS ERROR ROUTINE SIMPLY DISPLAYS A QUESTION MARK AND CONTROL IS RETURNED TO
; THE MAIN COMMAND PROCESSING LOOP.
;
ERROR:	MVI	A,'?'		; ERROR INDICATOR
	CALL	CHROUT		; DISPLAY IT ON SCREEN
	MVI	A,0DH		; <CR> CHARACTER
	CALL	CHROUT		; NEW DISPLAY LINE
	JR	COMPROC		; GO TO MAIN LOOP
;
; 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	; Point to 1st parameter
	CALL	ASCHEXW		; Convert it
	SDED	PARAM1		; Store it
;
; Repeat for the other two parameters
	INX	H
	CALL	ASCHEXW
	SDED	PARAM2
	INX	H
	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 the 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	ERROR
;
	LHLD	PARAM3		; Insure that the 3rd parameter is a byte value
	MOV	A,H
	CPI	0
	JRNZ	ERROR
;
	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
	JMP	COMPROC		; 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, if 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
	CPI	3
	JNC	ERROR
	CPI	1
	JC	ERROR
	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	L		; Divide by 16 to get # of lines
	SRLR	L
	SRLR	L
	SRLR	L		; Move hi L nibble to low
	MOV	A,H
	SLAR	A		; Shift over H to left losing hi nibble
	SLAR	A
	SLAR	A
	SLAR	A
	ORA	L		; Get new low byte value
	MOV	L,A
	SRLR	H		; Move H hi nibble to low and done
	SRLR	H
	SRLR	H
	SRLR	H
	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,020H		; 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,0DH
;
	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	0		; Get character
	ANI	7FH		; Remove extraneous bit
	CPI	0DH		; Test for <CR>
	JRNZ	LINTST		; If not, continue
	JMP	COMPROC		; Else, get new command
;
; 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
	JMP	COMPROC		; 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:	MOV	A,C		; Test for correct # of parameters
	CPI	1
	JNZ	ERROR
	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,020H		; 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,020H		; 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	0DH
	JZ	COMPROC		; If so, go get next command
	JMP	ERROR		; If not, syntax error
;
TRYCR:	CPI	0DH		; 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
	JNZ	ERROR		; 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	0DH		; If not, test for <CR>
	JNZ	ERROR		; 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	0DH
	JRZ	CHANGE		; Go and put new value in address
	JMP	ERROR
;
;
; 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:	MOV	A,C		; Test for correct # of parameters
	CPI	1
	JNZ	ERROR
;
	LHLD	PARAM1		; Insure that parameter is a byte value since
	MOV	A,H		;  ports are numbered 00H thru 0FFH
	CPI	0
	JNZ	ERROR
	MOV	C,L		; Set up for input instruction
;
	MOV	A,L
	CALL	HEXASCB		; Convert port number to ASCII
	MOV	A,D		; Output the port number
	CALL	CHROUT
	MOV	A,E
	CALL	CHROUT
	MVI	A,020H		; Put ASCII space after port numver
	CALL	CHROUT
;
	INP	A		; Get port value
	CALL	HEXASCB		; Convert to displayable ASCII
	MOV	A,D		; Display it
	CALL	CHROUT
	MOV	A,E
	CALL	CHROUT
	MVI	A,020H		; Put ASCII space after port value
	CALL	CHROUT
;
	CALL	LININ		; Wait for user input
	LXI	H,LINBUF	; Point to input line buffer
	MOV	A,M		; Get first byte entered by user
	CPI	0DH		; Test if done with command
	JZ	COMPROC		; If so, go get another command
;
	CALL	ASCHEX		; Check for ASCII digit and get byte value
	JNZ	ERROR		; 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
	JRZ	SCNDIG		; Go ahead if ASCII digit
	CPI	0DH		; If not test for <CR>
	JNZ	ERROR		; Syntax error if not <CR>
;
;
OUTPRT:	MOV	A,B		; Get new byte value
	OUTP	A		; Output it to port
	JMP	COMPROC		; Go get another command
;
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		
	CPI	0DH
	JRZ	OUTPRT
	JMP	ERROR
;
; The GO command is executed here.  One parameter is entered, the address to
; which program control is to be tranferred.
GO:	MOV	A,C		; Test for correct # of parameters
	CPI	1
	JNZ	ERROR
;
	LHLD	PARAM1		; Get execution address
	PCHL			; Pass control to it
;
;
; 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	7FH		; Remove extraneous bit
	RET
;
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	; INTIALIZE 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,08H		; BACKSPACE OVER LAST CHARACTER
	CALL	CHROUT
	INR	B		;INCREMENT THE 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
	CPI	0DH		; IF <CR>, DONE
	RZ
;
	XRA	A		; CLEAR A REGISTER AND ZERO FLAG
	CMP	B		; TEST IF NO ROOM LEFT FOR CHARACTERS
	JRNZ	CHRLOP		; IF NOT, CONTINUE
;
	MVI	A,0DH		; PUT IN FINAL <CR> IN BUFFER AT 33RD POSITION
	MOV	M,A
;	JMP	CHROUT		; DISPLAY IT
;
; 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	A		; SAVE CHARACTER
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
	CPI	0DH		; TEST FOR  <CR>
	RNZ			; RETURN IF NOT
;
CHRO2:	IN	SIOAC		; IF <CR>, OUTPUT LF
	ANI	4
	JRZ	CHRO2
	MVI	A,0AH
	OUT	SIOAD
	MVI	A,0DH
	RET
;
; 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-CHARACER 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	0DH		; 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
;
; 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 A 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
;
;
; 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 OK, NOT SET TO INDICATE EMPROR
;	REGISTER ALTERED:  A
;
ASCHEX:	CPI	'0'		; TEST FOR VALID RANGE 0-9
	RC
	CPI	':'
	JRNC	LETTER		; IF NOT, TRY FOR A-F
	ORA	A		; CLEAR CARRY
	SBI	030H		; ELSE, CONVERT, INDICATE VALID VALUE, AND
	CMP	A		;  RETURN
	RET
;
LETTER:	ANI	5FH		; UPPER OR LOWER CASE
	CPI	'A'		; TEST FOR VALID RANGE A-F
	RC
	CPI	'G'
	RNC
	ORA	A		; CLEAR CARRY
	SBI	037H		; CONVERT, INDICATE VALID VALUE, AND RETURN
	CMP	A
	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:MOV	A,D		; GET HIGH ORDER CHARACTER
	CALL	ASCHEX		; CONVERT TO VALUE
	RNZ			; RETURN IF NOT HEX DIGIT
	SLAR	A		; X2
	SLAR	A		; X4
	SLAR	A		; X8
	SLAR	A		; X16
	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 RECEEDING ONE EXCEPT THAT FOUR ASCII
; CHARACTERS, POINTED TO BY HL, ARE CONVERTED INTO A WORD VALUE.  THERE IS
; EMPROR RETURN, ALSO.
;
;	INPUT PARAMETERS:  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:MOV	D,M		; GET FIRST CHARACTER
	INX	H
	MOV	E,M
	CALL	ASCHEXB		; CONVERT INTO BYTE VALUE
	RNZ			; RETURN IF INVALID
	PUSH	A		; SAVE BYTE VALUE
;
	INX	H		; GET LAST 2 CHARACTERS
	MOV	D,M
	INX	H
	MOV	E,M
	CALL	ASCHEXB		; CONVERT
	POP	D
	RNZ
;
	MOV	E,A		; LOW ORDER BYTE IN E
	CMP	A		; INDICATE VALID RETURN
	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
;	REGISTERS ALTERED:  A
;
HEXASC:	ANI	0FH		; CLEAR UPPER NIBBLE, JUST IN CASE
;	CPI	0AH		; TEST FOR 0-9 OR A-F
;	JRNC	LETTER1
;	ADI	030H		; CONVERT FOR 0-9
	adi	90h
	daa
	aci	40h
	daa
	RET
;
;LETTER1:ADI	037H		; CONVERT FOR A-F
;	RET
;
; THE SUBROUTINE CONVERTS A BYTE VALUE INTO 2 ASCII CHARACTERS
;
;	INPUT PARAMETER:  A-BYTE VALUE
;	OUTOUT PARAMETER:  D,E-ASCII CHARACTERS
;	REGISTERS ALTERED:  A,D,E
;
HEXASCB:PUSH	A		; SAVE BYTE VALUE
	SRLR	A		; SHIFT UPPER NIBBLE TO LOWER NIBBLE
	SRLR	A
	SRLR	A
	SRLR	A
	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	HEXASCB		; CONVERT TO 2 ASCII DIGITS
	MOV	M,D		; STORE DIGITS IN STRING
	INX	H
	MOV	M,E
;
	POP	D		; GET LOWER BYTE
	MOV	A,E
	CALL	HEXASCB		; CONVERT
	INX	H
	MOV	M,D		; STORE IN STRING
	INX	H
	MOV	M,E
	INX	H
	RET
;
; THIS SUBROUTINE SIMPLY OUTPUTS A "#", A CARRIAGE RETURN, AND A SPACE.
;
;	REGISTERS ALTERED:  A
;
;HATCH:	MVI	A,'#'
;	CALL	CHROUT
;	MVI	A,0DH
;	CALL	CHROUT
;	MVI	A,020H
;	JMP	CHROUT
hatch:	lxi	h,spmsg
	jmp	dismsg
spmsg:	db	'#',13,' ',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	020H
	JRC	PERIOD
	CPI	07FH
	RC
PERIOD:	MVI	A,'.'
	RET
;
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
;
;
	end
