;---------------------------------------------------------------------
; 
;  ALS8CUT - Patch for ALS-8 PROM to use CUTER (or SOLOS) as the
;      default SYSIO driver. Also sets the symbol table pointer
;      to a valid address (D700) so assemblies will work without
;      having to issue a STAB command.
;
;  Mike Douglas, 08/21/24
;
;---------------------------------------------------------------------
SINP	equ	0C01Fh			;CUTER/SOL standard input
SOUT	equ	0C019h			;CUTER/SOL standard output
EORMS	equ	0E060h			;ALS-8 command loop
ESC	equ	1Bh			;ASCII ESC character

; The default ALS8 I/O driver (SYSIO) is in PROM from E1D9 to E215. During
;    initialization, ALS-8 copies this driver to RAM at D08F. This patch
;    is an I/O driver that uses the standard SINP and SOUT entry points in
;    CUTER and SOLOS for I/O.

PROMLOC	equ	0E1D9h			;location of this code in PROM
RUNLOC	equ	0D08Fh			;location of this code in RAM
OFFSET	equ	PROMLOC-RUNLOC		;offset betweeen RAM and PROM location

	org	PROMLOC
	db	'SYSIO'
	dw	inp8			;input vector
	dw	outp8			;output vector

; Input Driver

inp8	equ	$-OFFSET		;runtime location in RAM
	call	stat			;wait for a new character
	jz	inp8

	ani	7Fh			;strip possible parity
	mov	b,a			;preserve in B
	jmp	inp8fin			;go finish inp8

; stat MUST be at D0A4

stat	equ	$-OFFSET		;runtime location in RAM
	lda	char			;char already available?
	ora	a
	rnz				;yes

	call	SINP			;else, see if hardware has new char
	sta	char			;if so, it will be in A and char
	ret

; inp8 finishes here

inp8fin	equ	$-OFFSET		;runtime location in RAM
	xra	a			;zero to indicate char is gone
	sta	char
	mov	a,b			;exit with new char in A (and B)
	ret

; Output Driver

outp8	equ	$-OFFSET		;runtime location in RAM
	call	stat			;has anything been typed?
	jz	noChr			;no

	push	b			;preserve B
	call	inp8			;get the new character
	pop	b
	cpi	ESC			;was ESC typed?
	jz	EORMS			;yes, exit to command loop

noChr	equ	$-OFFSET
	call	SOUT			;output character in B
	mov	a,b			;exit with char in A and B
	ret

char	equ	$-OFFSET		;runtime location in RAM
	db	0			;queued character from SINP

; An instruction at this address initializes the symbol table address. Set
;   that address to be D700 (RAM) instead of E000 (invalid/not set)

	org	0E04Fh
	dw	0D700h
	end
