;----------------------------------------------------------------------------
;
; System-dependent Overlay for ZMP (Z-Modem Program) v1.5
; Version for Davidge DSB 4/6 (SIO C) w/H19 terminal in ANSI mode.
;
;	Name	ZMO-DSBC.MAC
;
;	Dated 12 July 2025
;
; Updated to use interrupt-driven receive.  Reconstituted from executables
; produced by files not found on the disk to which I had access.
;
; -- John D. Baker, 12 July 2025
;
;	Dated 16 March 1994
;
; Built up from ZMO-BLNK.Z80 sample in ZMP15.LBR .  Like a Kaypro,
; the Davidge DSB 4/6 uses an SIO.  It also uses the Z80 CTC to
; generate the bit-rate clock.  For speeds below 450bps, the 4 MHz
; system clock is used to synthesize the bit rate with reasonable
; accuracy.  For 450bps and above, an external oscillator connected
; to the ctc's CLK/TRG inputs is used.  Standard bit rates from 75bps
; to 115,200bps may be thus generated.
;
; -- John D. Baker, 16 March 1994
;
;----------------------------------------------------------------------------
;
;	System-dependent code overlay for ZMODEM
;
;    Insert your own code as necessary in this file. Code contained herein
; has been written in Z80 code for use with M80 or SLR. Assemble as follows:
;
;	SLR ZMO-xx01/h
;	MLOAD ZMP.COM=ZMODEM.COM,ZMO-xx01.HEX
; or
;	M80 =ZMO-xx01.Z80
;	RELHEX ZMO-xx01
;	MLOAD ZMP.COM=ZMODEM.COM,ZMO-xx01.HEX
;
;       (Don't use L80 without changing the source for assembly as a
;         cseg file.)
;
;----------------------------------------------------------------------------
;
; Notes on modifying this file:
;
;    C requires that functions do not change either index register
; (IX or IY).
; If your overlay requires either of these to be changed, ensure they are
; restored to the original values on return.
;    Since collecting parameters from C functions can be tricky, only change
; the parts marked 'Insert your own code here'. Do NOT modify the jump
; table at the start. Do NOT modify the entry/exit sections of each
; function. Do NOT pass 'GO'. Do NOT collect $200.
;    Apart from defining modem functions, this file also defines terminal
; characteristics. Examples provided are for ADM-3A (with a few of my own
; additions). Modify to suit your own terminal. An inline print routine
; is provided for printing strings in the usual way: usage is
;
;	call	print
;	db	'required string',0
;
;----------------------------------------------------------------------------
;
;    Don't forget to set your clock speed at the clkspd variable.
;
;    If you find your overlay exceeds the maximum size (currently 0400h),
; you will have to contact me for another version. If too many people need 
; to do it, we haven't allowed enough room.
;
; Ron Murray 15/8/88
;
;---------------------------------------------------------------------------

false	equ	0
true	equ	not false
no	equ	false		; false value
yes	equ	true		; true value

;----------------------------------------------------------------------------

; User-set variables: 

clkspd	equ	4		; Processor clock speed in MHz
debug	equ	false		; to allow debugging of overlay with Z8E etc.

; Davidge DSB 4/6 port equates:

ctca	equ	08h	;CTC, ch 0 = console baud rate
ctcr	equ	ctca+1	;CTC, ch 1 = real-time interrupt generator
ctcb	equ	ctca+2	;CTC, ch 2 = serial printer baud rate
ctcf	equ	ctca+3	;CTC, ch 3 = FDC mode 2 interrupt interface
;
sioad	equ	00h	;SIO A data = console
sioac	equ	sioad+1	;SIO A cmd
siobd	equ	sioad+2	;SIO B data = serial printer
siobc	equ	sioad+3	;SIO B cmd
;
ctcc	equ	0ch	;CTC1, ch 0 = SIO port C baud rate (?)
ctcs	equ	ctcc+1	;CTC1, ch 1 = high-speed parallel (SASI) port
ctcd	equ	ctcc+2	;CTC1, ch 2 = SIO port D baud rate (?)
ctcp	equ	ctcc+3	;CTC1, ch 3 = parallel printer port
;
siocd	equ	04h	;SIO C data (designated modem port)
siocc	equ	siocd+1	;SIO C cmd
siodd	equ	siocd+2	;SIO D data (aux port)
siodc	equ	siocd+3	;SIO D cmd
;
;	Control bits
;rxrdy	equ	00000001b	;Rx buffer full
rdrf	equ	00h		;Rx bit
txmty	equ	00000100b	;Tx buffer empty
tdre	equ	02h		;Tx bit
slsdcd	equ	00001000b	;Data Carrier Detect
dcd	equ	03h		;DCD* bit

; CTC control bits
ctcccw	equ	00000001b	;bit 0 of CCW is always 1
ctcrst	equ	00000010b	;reset the channel (stop counter)
ctcltc	equ	00000100b	;load new time constant at next write
ctctrg	equ	00001000b	;start counting at next CLK/TRG pulse
ctcris	equ	00010000b	;use rising edge as trigger
ctc256	equ	00100000b	;prescale CLK by 256 instead of 16
ctccnt	equ	01000000b	;set counter mode
ctcint	equ	10000000b	;CTC channel may interrupt
;
; The serial port for the console is initialized to
; 9600 baud via the Z80 CTC.  Two equ(ate) labels provide
; for differing baud rates.
; Label 'ctcmode' sets the CTC mode, i.e. counter or timer.
; Label 'sbaud' is the time constant for the CTC.
; 
;	baud	ctcmode	sbaud
;
;	75	007h	208
;	110	007h	142
;	134.5	007h	116
;	150	007h	104
;	300	007h	52
;	450	047h	0 (256 for Z80 CTC)
;	600	047h	192
;	710	047h	162 (0.16% error)
;	1200	047h	96
;	1800	047h	64
;	2000	047h	58
;	2400	047h	48
;	3600	047h	32
;	4800	047h	24
;	7200	047h	16
;	9600	047h	12
;	19200	047h	6
;	38400	047h	3
;	57600	047h	2
;	115200  047h	1

;EQUates for customizing port used by ZMP....

sioc	equ	siocc		; SIO C ctrl port
siod	equ	siocd		; SIO C data port
sioivr	equ	siodc		; SIO D ctrl port (WR2IVL, WR1SAV)
baud	equ	ctcc		; baud rate port (CTC1, ch 0 )

rxrdy	equ	1		; receiver ready
txrdy	equ	4		; transmitter ready

;
; The following EQUates are valid only for Davidge FD-4000 BIOS v1.3
ctcavec	equ	0b0h		; ctca doesn't interrupt but BIOS
				; overlays it w/DMAC ISR
ctcrvec	equ	ctcavec+2	; real-time interrupt not used in
				; FD-4000 v1.3 BIOS
ctcbvec	equ	ctcrvec+2	; ctcb doesn't interrupt (SIOB baud)
ctcfvec	equ	ctcbvec+2	; FDC interrupts via ctcf.

siovec	equ	ctcbvec		; use ctcb vector offset for sio ints

;Set the following two equates to the drive and user area which will contain
;   ZMP's .OVR files, .CFG file, .FON file and .HLP file. Set both to zero
;   (null) to locate them on the drive from which ZMP was invoked.

overdrive	equ	'A'	; Drive to find overlay files on ('A'-'P')
overuser	equ	2	; User area to find files

;----------------------------------------------------------------------------


; NOT user-set variables

userdef	equ	0145h		; origin of this overlay
				; This address should not change with
				; subsequent revisions.

zmpstart equ	010Bh		; target of initial JP instruction...
				; valid for ZMP v1.5 only!!

mspeed	equ	03ch		; location of current baud rate. 
ovsize	equ	0400h		; max size of this overlay

tmode	equ	ctcltc+ctcrst+ctcccw	;set timer mode, reset and load
					;new time constant
cmode	equ	tmode+ctccnt	;be a counter instead of a timer


	.z80			; use z80 code
	aseg			;' absolute

	 if not debug
	org	0100h		;patch initial JP xxxx
	jp	steal		;jump here first
	 endif

	 if	debug
	org	100h		; so you can debug it with cebug, zsid, etc
	 else
	org	userdef
	 endif


esc	equ	1bh
ctrlq	equ	11h
cr	equ	0dh
lf	equ	0ah
bdos	equ	5


codebgn	equ	$

;Jump table for the overlay: do NOT change this
jump_tab:
	jp	scrnpr		; screen print
	jp	mrd		; modem read with timeout
	jp	mchin		; get a character from modem
	jp	mchout		; send a character to the modem
	jp	mordy		; test for tx buffer empty
	jp	mirdy		; test for character received
	jp	sndbrk		; send break
	jp	cursadd		; cursor addressing
	jp	cls		; clear screen
	jp	invon		; inverse video on
	jp	invoff		; inverse video off
	jp	hide		; hide cursor
	jp	show		; show cursor
	jp	savecu		; save cursor position
	jp	rescu		; restore cursor position
	jp	mint		; service modem interrupt
	jp	invec		; initialise interrupt vectors
	jp	dinvec		; de-initialise interrupt vectors
	jp	mdmerr		; test uart flags for error
	jp	dtron		; turn DTR on
	jp	dtroff		; turn DTR OFF
	jp	init		; initialise uart
	jp	wait		; wait seconds
	jp	mswait		; wait milliseconds
	jp	userin		; user-defined entry routine
	jp	userout		; user-defined exit routine
	jp	getvars		; get system variables
	jp	setport		; set port (0 or 1)

; Spare jumps for compatibility with future versions
	jp	spare		; spare for later use
	jp	spare		; spare for later use
	jp	spare		; spare for later use
	jp	spare		; spare for later use
	jp	spare		; spare for later use
	jp	spare		; spare for later use

;
; Main code starts here
;
steal:
	ld	hl,(bdos+1)	;get BDOS jump
	ld	e,l
	ld	d,h		;in DE
	ld	l,0		;back up to page boundary
	dec	h		;bump down one page
	dec	hl		;address of head pointer
;	ld	(head),hl	;save it
	ld	(hl),0		;zero the head pointer
	dec	hl		;address of tail pointer
	ld	(tail),hl	;save it
	ld	(hl),0		;zero the tail pointer
	dec	hl		;high byte of fake BDOS jump
	ld	(hl),d		;put real high byte there
	dec	hl		;low byte of fake BDOS jump
	ld	(hl),e		;put real low byte there
	dec	hl		;here is where we jump
	ld	(hl),0C3h	;so put JP xxxx instruction here
	ld	(bdos+1),hl	;and tell programs to jump here
;
; do any other intializations here
;
	jp	zmpstart	;and continue with ZMP as usual.
;
;Screen print function
scrnpr:
				; <== Insert your own code here
				; <== End of your own code
spare:	ret

; User-defined entry routine: leave empty if not needed
userin:
	ret

; User-defined exit routine: leave empty if not needed
userout:
	ld	hl,(bdos+1)	;get BDOS jump
	inc	hl		;point to low byte
	ld	e,(hl)		;low byte in E
	inc	hl		;point to high byte
	ld	d,(hl)		;high byte in D
	ld	(bdos+1),de	;restore BDOS jump to normal
;
	ret


;Get a character from the modem: return in HL
mchin:
	push	bc
				; <== Insert your own code here
	ld	hl,(tail)	;get index to last served
	inc	(hl)		;point to byte to serve
	ld	l,(hl)		;offset in L
	inc	h		;page in H
	ld	a,(hl)		;get the byte
				; <== End of your own code
	ld	l,a		; put in HL
	ld	h,0
	or	a		; set/clear Z
	pop	bc
	ret
;
tail:
	defw	0000h		;tail->tail_idx

;Send a character to the modem
mchout:
	ld	hl,2		; get the character
	add	hl,sp
	ld	a,(hl)
				; <== Insert your own code here
	out	(siod),a	;write modem output char.
				; <== End of your own code
	ret			; done

;Test for output ready: return TRUE (1) in HL if ok
mordy:
				; <== Insert your own code here
	in	a,(sioc)	;get modem output status
	ld	c,a		;save in C
	ld	hl,0000h	;assume not ready
	and	20H		;modem dropped CTS?
	ld	a,10h		;external/status reset
	out	(sioc),a	;reset status bits
	ret	z		;yes, say we can't transmit
	bit	2,c		;txrdy?
	ret	z		;rtn if not
	inc	l		;else return true
				; <== End of your own code
;	ld	a,l		; set/clear Z
;	or	a
	ret

;Test for character at modem: return TRUE (1) in HL if so
mirdy:
				; <== Insert your own code here
	ld	hl,(tail)	;get tail pointer
	ld	a,(hl)		;get in A
	inc	hl		;bump to head index
	xor	(hl)		;quick compare
	ld	hl,0001h	;assume not empty
	jr	nz,ntmty	;brif if char in FIFO
	inc	a		;A=1
	out	(sioc),a	;WR1
	ld	a,18h		;rcv int mode
	out	(sioc),a	;make sure ints are on
	ld	hl,wr5		;point at WR5 data
	set	1,(hl)		;1 in RTS bit
	push	bc		;save BC
	call	sioout		;raise RTS
	pop	bc		;get BC back
	xor	a		;buffer was empty, A=0,Z=1
	ld	h,a
	ld	l,h		;HL=0000
ntmty:
	ret

;Send a break to the modem: leave empty if your system can't do it
sndbrk:
				; <== Insert your own code here
	ld	a,(setf)	;start "break"
	or	a		;do we have valid data in wr[345]
	ret	z		;no - don't do it
	ld	hl,wr5
	set	4,(hl)		; set the bit
	call	sioout		; and go do it
	
	ld	hl,300		; wait 300 mS
	call	waithlms

	ld	hl,wr5		;end "break"
	res	4,(hl)
	jp	sioout		; go clear the bit (sioout returns to main)
				; <== End of your own code
;	ret

;
;Test UART flags for error: return TRUE (1) in HL if error.
mdmerr:
				; <== Insert your own code here
	ld	hl,0000h	;assume no errors
				; <== End of your own code
	ld	a,l		; set/clear Z
	or	a
	ret


;Turn DTR ON
dtron:
				; <== Insert your own code here
	ld	a,(setf)	;raise DTR
	or	a
	ret	z		; don't do it unless wr[345] are valid
	ld	hl,wr5
	set	7,(hl)
	jr	sioout		;turn it on and return to main
				; <== End of your own code
;	ret



;Turn DTR OFF
dtroff:
				; <== Insert your own code here
	ld	a,(setf)	;drop DTR
	or	a
	ret	z		; don't do it unless wr[345] are valid
	ld	hl,wr5
	res	7,(hl)
	jr	sioout		;turn it off and return to main
				; <== End of your own code
;	ret


;Initialise the uart

init:
	ld	hl,2		; get parameters
	add	hl,sp		; HL->parameters on stack
	ex	de,hl		; DE-> parameters on stack

	if	false	
	call	getparm		; in HL
	ld	(brate),hl	; baud rate
	call	getparm
	ld	(parity),hl	; parity
	call	getparm
	ld	(data),hl	; data bits (BINARY 7 or 8)
	call	getparm
	ld	(stop),hl	; stop bits (BINARY 1 or 2)

				; <== Insert your own code here
				; using values below
	endif
;
	call	setbd		;DE->brate idx
;
	inc	de
	inc	de		;DE->parity
setmd:
	ld	c,0		;0 in C, assume no parity
	ld 	a,(de)		;get parity flag
	cp	'N'		;no parity?
	jr	z,finpty	;yes, we're done with this part
	ld	c,4		;set up with offset to EVEN parity
	cp	'E'		;even?
	jr	z,finpty	;yes, we're done
	ld	c,8		;else must be ODD parity, load offset
finpty:
	inc	de
	inc	de		;bump to data bits
	ld	a,(de)		;get # of data bits
	sub	7		;normalize to 0, 1
	add	a,c		;add to offset
	ld	c,a		;save it
	inc	de
	inc	de		;bump to stop bits
	ld	a,(de)		;get # of stop bits
	dec	a		;normalize to 0, 1
	add	a,a		;*2 to make offset
	add	a,c		;add to offset
	ld	c,a		;
	ld	b,0		;mode byte index in DE
	ld	hl,modetb	;get address of mode table
	add	hl,bc		;HL->mode byte
	ld	a,(hl)		;get mode byte
;
setmd1:
	ld	(setf),a	; flag we've done a set
	ld	c,a		; save byte in c
	ld	hl,wr3		; HL->wr3
	res	7,(hl)		; turn off ms bit (rx # bits / char)
	add	a,a		; move bit from 6 to 7 in a
	and	80h		; mask off the rest
	or	(hl)		; or in the remainder
	ld	(hl),a		; and save it back
	inc	hl
	inc	hl		; HL->wr4
	ld	a,(hl)
	and	0f4h		; mask out bits we don't want
	ld	b,a		; save in b
	ld	a,c		; get set byte back
	and	0bh		; get bits out of set byte that we want
	or	b		; or in the other bits
	ld	(hl),a		; and save it back
	inc	hl
	inc	hl		; HL->wr5
	ld	a,c
	and	40h		; get the bit we want from c
	res	6,(hl)		; clear the bit in r5
	or	(hl)
	ld	(hl),a		; put new composite value back

sioout:
	ld	hl,siodat		;hl points to data block
	ld	bc,siodatln SHL 8 + sioc ;count in B, port in C
	otir
				; <== End of your own code
	ret
;
setbd:
	ld	a,(de)		;get brate idx
setbd0:
	ld	c,a
	ld	b,0		;in BC
setbd1:
	ld	hl,baudtb	;HL->baudrate table
	add	hl,bc		;HL->time constant
	ld	a,(hl)		;get in A
	inc	a		;if FF, now 00, so bad bit rate!
	ret	z		;return if rate not supported
	ld	a,(de)		;get brate idx again

	ld	(mspeed),a	; don't forget to load mspeed with the
				; current brate value if the new rate is
				; valid. See table of values below.

	cp	2		;is it 450bps indicator?
	ld	a,tmode		;assume < 450 bps (set timer mode)
	jr	c,setctc	;and output
	ld	a,cmode		;set counter mode  
setctc:
  	out	(baud),a	;set ctc mode
	ld	a,(HL)		;get time constant back
	out	(baud),a	;and put it out too
	ret

;--------------------------------------------------------------------------
	if	false
brate:	dw	8		; baud rate
parity:	dw	'N'		; parity
data:	dw	8		; data bits
stop:	db	1		; stop bits
	endif
;--------------------------------------------------------------------------

;Values of brate for each baud rate
;
; baud rate    brate
;
;   110		 0
;   300		 1
;   450		 2
;   600		 3
;   710		 4
;  1200		 5
;  2400		 6
;  4800		 7
;  9600		 8
; 19200		 9
; 38400		10
; 57600         11
; 76800         12
;115200		13

; The baud rate table has entries from 115200 baud down to 110 baud.
; The data here are the time constant values to be sent to the CTC
; for generating the requested bit rate.  A value of FF indicates
; that the bit rate is not supported.

baudtb:
	defb	142, 52		; 110, 300 bps (CTC in timer mode)
	defb	0, 192, 162	; 450, 600, 710 (0.16% error) bps
	defb	96, 48, 24, 12	; 1200, 2400, 4800, 9600 bps
	defb	6, 3, 2		; 19200, 38400, 57600 bps
	defb	1, 0ffh		; 76800 (not supp'd, use for 115200) bps
	defb    0ffh, 0ffh	; more than that not supported

; Communication mode table.  Single byte values for 12 combinations of
;    number-of-bits(7/8), parity(none/even/odd), number-of-stop-bits(1/2).

modetb:
n17:	defb	10000000b	;80h, 7n1
n18:	defb	11000000b	;C0h, 8n1
n27:	defb	10001000b	;88h, 7n2
n28:	defb	11001000b	;C8h, 8n2
e17:	defb	10000011b	;83h, 7e1
e18:	defb	11000011b	;C3h, 8e1
e27:	defb	10001011b	;8Bh, 7e2
e28:	defb	11001011b	;CBh, 8e2
o17:	defb	10000001b	;81h, 7o1
o18:	defb	11000001b	;C1h, 8o1
o27:	defb	10001001b	;89h, 7o2
o28:	defb	11001001b	;C9h, 8o2

;string for addressing Z80 SIO registers.  Generally, a Write Register
;number followed by the data to be written to that register.
siodat:
	defb	3
wr3:	defb	11000001b	; value for wr3 (C1h)
	defb	4
wr4:	defb	01000100b	; value for wr4 (44h)
	defb	5
wr5:	defb	11101010b	; value for wr5 (EAh)
siodatln equ	$-siodat

setf:	defb	0		; flag if we've done a set mode command


;
; Set the port. ZMP supplies either 0 or 1 as a parameter. You're on your
; own here -- your system is bound to be different from any other! You may
; implement a software switch on all the modem-dependent routines, or perhaps
; you can have one or two centralised routines for accessing the UARTs and
; modify the code from this routine to select one or the other. (Who said
; there was anything wrong with self-modifying code?). If you have only one
; UART port, or if you don't want to go through all the hassles, just have
; this routine returning with no changes made. Note that ZMP calls this
; routine with both values for the port on initialisation.
;
setport:
	ld	hl,2		; get port number
	add	hl,sp
	ex	de,hl
;	call	getparm		; in HL (values are 0 and 1)

				; <== Insert your own code here

				; <== End of your own code
	ret

port:	ds	1


;****************************************************************************
;Video terminal sequences: these are for HeathKit H19 in ansi mode.
;Modify as you wish....

;Cursor addressing: 
cursadd:
	ld	hl,2		; get parameters
	add	hl,sp
	ex	de,hl
	if	false
	call	getparm		; in HL
	ld	(row),hl	; row
	call	getparm
	ld	(col),hl	; column
				; <== Insert your own code here
				; using values in row and col
	endif
;
	call	print
	db	esc,'[',0	; VT-100 leadin
	ld	a,(de)		; row first
	inc	a		;  bump up by 1
	call	send2 		;send as ASCII string
	ld	a,';' 		;get separator
	call	cout		; send it
	inc	de
	inc	de
	ld	a,(de)		; same for column
	inc	a		; bump up 1
	call	send2
	ld	a,'f'		;get terminator 
	jp	cout		; put it out
;
send2:	call	btoa
	ld	a,h
	call	cout
	ld	a,l
	jp	cout
;
btoa:	ld	h,'0'
btoa1:	sub	10
	jr	c,btoa2
	inc	h
	jr	btoa1
btoa2:	add	a,10+'0'
	ld	l,a
				; <== end of your own code
	ret

	if	false
row:	ds	2		; row
col:	ds	2		; column
	endif


;Control strings for Heathkit H19 in ansi mode:

;Clear screen:
cls:
	call	print
	defb	esc,'[H',esc,'[J',0	;home and clear screen
	ret

;Inverse video on:
invon:
	call	print
	defb	esc,'[7m',0	;bright/bold string (h19-ansi)
	ret

;Inverse video off:
invoff:
	call	print
	defb	esc,'[m',0	;dim/normal string (h19-ansi)
	ret

;Turn off cursor:
hide:
	call	print
	defb	esc,'[1v',0
	ret

;Turn on cursor:
show:
	call	print
	defb	esc,'[v',0
	ret

;Save cursor position:
savecu:
	call	print
	defb	esc,'7',0
	ret

;Restore cursor position:
rescu:
	call	print
	defb	esc,'8',0
	ret

;****************************************************************************

;Service modem interrupt:
mint:
	ld	(oldstk),sp	;save stack pointer
	ld	sp,isrstk	;set up our own stack pointer
	push	af		;save AF
	push	hl		;save HL
mint0:
	in	a,(sioc)	;read status
	and	rxrdy		;rcv char avail?
	jr	z,othrint	;brif not
	ld	hl,(tail)	;char ready, get tail idx
	ld	a,(hl)		;in A
	inc	hl		;bump to head idx
	inc	(hl)		;bump to next slot
	sub	(hl)		;did we wrap?
	jr	z,wrapped	;brif wrapped (HEAD=TAIL)
	ld	l,(hl)		;else idx in L
	inc	h		;HL->slot for char
	cp	4		;(head-tail)-4
	jr	nc,havspc	;brif at least 4 spaces
	push	hl		;else save HL
	ld	hl,wr5		;HL points to WR5 data
	res	1,(hl)		;reset RTS bit
	push	bc		;save BC
	call	sioout		;drop RTS
	pop	bc		;restore BC
	pop	hl		;restore FIFO pointer
havspc:
	in	a,(siod)	;read char
	ld	(hl),a		;store in FIFO
	jr	mint0		;until no more chars
;
wrapped:
	dec	(hl)		;FIFO wrapped, reset head idx
	inc	a		;A was 0, now 1
	out	(sioc),a	;WR1
	dec	a		;A=0
	out	(sioc),a	;disable ints
othrint:
	ld	a,30h		;reset latched error bits in RR1
	out	(sioc),a
	pop	hl		;restore HL
	pop	af		;restore AF
	ld	sp,0000h	;filled in on entry
oldstk	equ	$-2		;entry saves old SP here
	ei
	reti
;
;	ret			; my system doesn't need this
;
	defs	12
isrstk:

;Initialise interrupt vectors:
invec:
	di			;disable ints while we work
	ld	hl,siovecstr	;HL->SIO int vec string
	ld	bc,siovsln SHL 8 + sioivr ;4 bytes to SIODC (WR[21])
	call	sioout		;set it up
;
	ld	c,18h		;receive+err interrupts (except parity)
	ld	de,mint		;ISR addr in DE
invec0:
	ld	a,i		;get IVH byte
	ld	h,a		;in H
	ld	a,siovec	;get IVL byte
	ld	l,a		;in L
	ld	a,(hl)		;get old LO(ISR)
	ld	(hl),e		;install new LO(ISR)
	ld	e,a		;old LO(ISR) in E
	inc	hl		;bump to MSB
	ld	a,(hl)		;get old HI(ISR)
	ld	(hl),d		;install new HI(ISR)
	ld	d,a		;old HI(ISR) in D
	ld	(oldisr),de	;save old ISR addr
	ld	a,1		;WR1
	out	(sioc),a
	ld	a,c		;get interrupt condition byte
	out	(sioc),a	;set interrupt mode
	ei			;enable interrupts
;
	ret			; ditto

;De-initialise interrupt vectors:
dinvec:
	di			;disable ints while we work
	ld	c,0		;int condition byte (disable ints)
	ld	de,(oldisr)	;get old isr adr in DE
	jr	invec0
;
;	ret			; ditto

siovecstr:
	defb	2	;WR2 (ch B only)
wr2x:	defb	siovec	;IVL
	defb	1	;WR1
wr1x:	defb	0	;SAV=0 (ch B only)
siovsln	equ	$-siovecstr
;
oldisr:	defs	2


;****************** End of user-defined code ********************************
;		Do not change anything below here.

;Modem character test for 100 ms
mrd:
	push	bc		; save bc
	ld	bc,100		; set limit
mrd1:
	call	mirdy		; char at modem?
	jr	nz,mrd2		; yes, exit
	ld	hl,1		; else wait 1ms
	call	waithlms
	dec	bc		; loop till done
	ld	a,b
	or	c
	jr	nz,mrd1
	ld	hl,0		; none there, result=0
	xor	a
mrd2:
	pop	bc
	ret

; Inline print routine: destroys A and HL

print:
	ex	(sp),hl		; get address of string
ploop:
	ld	a,(hl)		; get next
	inc	hl		; bump pointer
	or	a		; done if zero
	jr	z,pdone
	call	cout		; else print
	jr	ploop		; and loop
pdone:
	ex	(sp),hl		; restore return address
	ret			; and quit

;
;Output a character in A to the console
;
cout:
	push	bc		; save regs
	push	de
	push	hl
	ld	e,a		; character to E
	ld	c,2
	call	bdos		; print it
	pop	hl
	pop	de
	pop	bc
	ret

;Wait(seconds)
wait:
	ld	hl,2
	add	hl,sp
	ex	de,hl		; get delay size
	call	getparm
				; fall thru to..
;Wait seconds in HL
waithls:
	push	bc		; save bc
	push	de		; de
	push	ix		; and ix
	ld	ix,0		; then point ix to 0
				; so we don't upset memory-mapped i/o

;Calculate values for loop constants. Need to have two loops to avoid
;   16-bit overflow with clock speeds above 9 MHz.

outerval	equ	(clkspd / 10) + 1
innerval	equ	(6667 / outerval) * clkspd

wait10:
	ld	b,outerval

wait11:
	ld	de,innerval

wait12:
	bit	0,(ix)		; time-wasters
	bit	0,(ix)
	bit	0,(ix)		; 20 T-states each
	bit	0,(ix)
	bit	0,(ix)
	bit	0,(ix)
	dec	de
	ld	a,e
	ld	a,d
	or	e
	jr	nz,wait12	; 150 T-states per inner loop
	djnz	wait11		; decrement outer loop
	dec	hl		; ok, decrement count in hl
	ld	a,h
	or	l
	jr	nz,wait10
	pop	ix		; done -- restore ix
	pop	de		; de
	pop	bc		; and bc
	ret

;Wait milliseconds
mswait:
	ld	hl,2
	add	hl,sp
	ex	de,hl		; get delay size
	call	getparm
				; fall thru to..
;Wait milliseconds in HL
waithlms:
	push	de
w1ms0:
	ld	de,39 * clkspd
w1ms1:
	dec	de
	ld	a,d
	or	e
	jr	nz,w1ms1
	dec	hl
	ld	a,h
	or	l
	jr	nz,w1ms0
	pop	de
	ret

;Get next parameter from (de) into hl
getparm:
	ex	de,hl		; get address into hl
	ld	e,(hl)		; get lo
	inc	hl
	ld	d,(hl)		; then hi
	inc	hl		; bump for next
	ex	de,hl		; result in hl, address still in de
	ret

;Get address of user-defined variables

getvars:
	ld	hl,uservars
	ret

uservars:
	dw	overdrive	; .OVR etc. drive/user
	dw	overuser


	 if	($ - codebgn) gt ovsize
toobig:	jp	errval		; Overlay too large!
	 endif

	end

