
;------------------------------------------------

; Port select debug and test program.

; Copyright B.J.Frost, May 1986, Version #1

;------------------------------------------------

	org 100H

; equates:

dir	defs 1		; store of direction chr
adr	defs 1		; port address
data	defs 1		; port data


start	ld de, port1
	call pstr	; print title
	call inch	; get user
	call lc2uc	; force upper case

	cp "E"		; want out?
	jp z,warms	; yes. out

	ld (dir),a	; save it

	ld de, port3	; ask for port address
	call pstr
	call inhex	; get address
	jr c,start	; error, loop reprint
	ld (adr),a	; save port

	ld a,(dir)
	cp "W"		; write data?
	jr nz, port4	; no. no data i/p
	ld de, port5	; ask for data to port
	call pstr
	call inhex	; get data
	jp c, warms	; bad data
	ld (data),a	; save it

; write loop

port7	ld de, port9	; tell of in loop
	call pstr

	ld a,(adr)
	ld c,a
	ld a,(data)
port8	out (c),a	; send data
	jr port8	; closed loop

; read loop

port4	ld de, port9	; tell of in loop
	call pstr

	ld a,(adr)	; fetch address
	ld c,a
port6	in a,(c)	; read data
	jr port6	; closed loop

port1	defw clear,pcurs; title
	defb 32,32+10
	defw ulon
	defm "I/O port address / data test program - 'port'. V1.1."
	defw uloff
	defb cr,lf,lf
	defm "Warning: This routine allows read or write in a fast, machine-code"
	defb cr,lf
	defm "         -loop for oscilloscope debug of address decode or data"
	defb cr,lf
	defm "         -lines. A re-boot will be required to restart!!!!!"
	defb cr,lf,lf,lf

	defm "Do you want to (R)ead , (W)rite  or  (E)xit? - "
	defb term

port3	defb cr,lf,lf
	defm "Enter port address in 2-digit hex ----"
	defb term

port5	defb cr,lf,lf
	defm "Enter port data in 2-digit hex ------ "
	defb term

port9	defb cr,lf,lf
	defm "Now in closed loop. Re-boot to restart.
	defb term

; ---------------------------------------------------

; jump table......

inch	jp xinch	; wait for, and get, chr to 'A'.
inhex	jp xinhex	; fetch hex byte from kbd
lc2uc	jp xlc2uc	; force upper case chr
pstr	jp xpstr	; print string, adr in 'de' then term.

; ---------------------------------------------------

; cp/m equates, function codes and call addresses.

conin	equ 1	; console input to 'A'
strpnt	equ 9	; print string, DE adr, then:
term	equ "$"	; ..string terminator

bdos	equ 5H	; cp/m BDOS entry address
warms	equ 0	; cp/m warm start

; ---------------------------------------------

; terminal control characters.


bell	equ 7		; bell code
lf	equ 10		; line feed
cr	equ 13		; carriage return
esc	equ 27		; escape

; terminal escape control sequences.

cup	equ 256*"A"+esc	; cursor up
cdn	equ 256*"B"+esc	; cursor down
crt	equ 256*"C"+esc	; cursor right
cle	equ 256*"D"+esc	; cursor left
clear	equ 256*"E"+esc	; clear screen
home	equ 256*"H"+esc	; cursor home
pcurs	equ 256*"Y"+esc	; set cursor position r,c
eline	equ 256*"l"+esc	; clear all line
rev	equ 256*"p"+esc	; into reverse video
revoff	equ 256*"q"+esc	; out of reverse
ulon	equ 256*"r"+esc	; underline on
uloff	equ 256*"u"+esc	; underline off

; -----------------------------------------------


; wait for, and get keyboard character.

xinch	ld c,1
	jp bdos	

; force ascii upper case if lower

xlc2uc	cp "a"
	jr c,lc1	; branch if <"a" (not lower)
	cp "z"+1
	jr nc,lc1	; branch if >"z" (not lower)
	sub "a"-"A"	; change "a".."z" to "A".."Z"
lc1	ret

; fetch hex byte from keyboard

xinhex	call xinh1	; get ms hex nibble
	ret c		; out if error
	rla
	rla
	rla
	rla		; shift up ms nibble
	and 0f0h
	push af		; clean up and save
	call xinh1	; get ls nibble
	pop bc
	ret c		; out if error
	add a,b		; add both chrs
	ret		; out clear carry

xinh1	call inch	; get chr
	call lc2uc	; force upper case
	sub "0"		; less than zero dec?
	jr c,xinh2	; yes, bad
	cp 10		; no. 0-9 inc?
	jr c,xinh3	; yes. got chr
	sub "A"-"0"-10	; remove chr base
	cp 16		; out of hex range?
	jr nc,xinh2	; yes. bad
xinh3	scf
	ccf		; clear carry
	ret		; return with good chr
xinh2	xor a
	scf
	ret		; return with carry error

; string print, address in 'de', ends in 'term'.

xpstr	ld c,strpnt	; string print function call
	call bdos	; do it
	ret

 end of system handling routines
; ---------------------------------------------------


	end

