	.Z80
;***************************************************************
;File:  GBIOS-WB.MAC
;Purp:  BIOS extension to support the RehDesign GIDE adapter
;	on the Davidge DSB 4/6 single-board computer w/v1.3 BIOS
;Auth:  John D. Baker
;

; Revision History:
;
; 26 September 2026 (JDB)
;	Added "jdbfix" variable for conditional assembly if using
;	a stock FD4000 BIOS (jdbfix equ false), or with the fixes
;	applied (jdbfix equ true).
;
; 23 September 2026 (JDB)
;	Added code to the "cold boot" routine to patch two
;	"JMP WBOOT" instructions in the CBIOS to instead
;	"JMP WBOOTE" (BIOS+3) so the correct warm boot routine
;	is used.
;	Changed turnkey command stuffing code to skip the
;	copy if the length is 0 and instead make the CIBUFF
;	length 0 so the jump to the CCP acts the same as
;	a jump to CCP+3.
;	Changed to use IDEDEF.MAC (IDEDEF.LIB)
;
; 14 September 2026 (JDB)
;	Use C as temp storage in swapping jump tables, no need
;	for stack usage.
;	Make "Hhome" conform to DRI example in DEBLOCK.ASM plus
;	CPM22PAT.09 (as found on "deramp.com").
;	Jump into CBIOS at "GOCPM" (BIOS+123h) to finish up warm
;	boot routine.*** UNTESTED! ***
;	Move signon from loader to cold boot routine.
;	Use CBIOS "prtwd" and "comp1" routines.
;
;  9 March 1996  (JDB)
;	Reworked hard disk selection and mapping to improve
;	handling of non-sequential head usage.
;
; 21 January 1996  (JDB)
;	Added code to permit warm-booting from the hard disk.
;
; 18 January 1996  (JDB)
;	More code tightening and tweaking.  Re-wrote clock driver
;	to use RLD instructions.  Relocated some data areas above
;	clock driver entry point.
;
; 15 January 1996  (JDB)
;	Major rewrite.  First public release.
;
; 12 January 1996  (JDB)
;	Created.
;
false	equ	0
true	equ	not false
;
jdbfix	equ	false	;true if fixes applied to FD4000 BIOS
;
msize	equ	62		;cp/m version memory size in kilobytes
xtrkb	equ	3		;less this many KB for BIOS
xtrpg	equ	2		;plus this many pages for BIOS
;
;	"bias" is address offset from 3400h for memory systems larger
;	than 16k (referred to as "b" throughout the text)
;
bias	equ	((msize-xtrkb)-20)*1024+xtrpg*256
ccp	equ	3400h+bias	;base of ccp (10 pages lower than std)
bdos	equ	ccp+800h	;base of bdos
bdose	equ	bdos+6h		;entry point of bdos
bios	equ	ccp+1600h	;base of bios
wboote	equ	bios+3		;warm-boot entry
cdisk	equ	0004h		;current disk number 0=a,...,15=p
iobyte	equ	0003h		;intel i/o byte
;
hstsiz	equ	512		;host sector size
nsecs	equ	(bios-ccp)/128	;number of sectors for warm boot
nhsecs	equ	nsecs/4		;number of host sectors
wbtdrv	equ	'A'		;warmboot from this drive
;
BDOSV	equ	5
WBOOTV	equ	0		;jump here for warm boot
fcb0	equ	5ch		;first FCB
fcb1	equ	fcb0+10h	;second FCB
currec	equ	fcb0+20		;current sequential record
r0	equ	currec+1	;random 0
r1	equ	r0+1		;random 1
r2	equ	r1+1		;random 2
tbuff	equ	0080h		;command tail buffer

;
; bdos constants on entry to write (passed in C)
;
wrall	equ	0		;write to allocated
wrdir	equ	1		;write to directory
wrual	equ	2		;write to unallocated
;
	maclib	idedef
;
TPA	equ	0100h		;start here
	if	jdbfix
FBTOP	equ	bios + 0F1Eh	;top of davidge floppy-disk BIOS
	else
FBTOP	equ	bios + 0F16h	;top of davidge floppy-disk BIOS
	endif
ZDTIME	equ	0FFF5h		;entry point for CardZ180 OEM ZDDOS clock
systop	equ	ZDTIME		;don't go higher than this
xtrspc	equ	ZDTIME+3	;unless it's here
HDBIOS	equ	(FBTOP+15) AND 0FFF0h
;HDBIOS	equ	0F720h		;this is where our stuff loads

	if	(HDBIOS - FBTOP) gt 07fffh
	  .printx "	***  MSIZE too large!  ***	"
	endif

prntstr	equ	9	;CP/M print string


	ASEG

	org	TPA

	ld	a,bios/256	;check for size
	ld	hl,(WBOOTV+1)	;get Warm boot
	sub	h		;compare
	jr	c,toosmall	;exit if running bios is too high
;
	ld	hl,(szchain)	;get command length
	ld	h,0		;0 in H
	ex	de,hl		;in DE
	ld	hl,(szfixed)	;get code length
	add	hl,de		;make size of code to move
;
	ld	b,h
	ld	c,l		;BC has length of code
	ld	hl,BiosCode	;source in HL
	ld	de,Fboot	;destination in DE
	ldir			;move the code
;
	jp	Fboot		;finish up here.
;
toosmall:
	ld	de,tsmlmsg	;point at too small message
	ld	c,prntstr	;print string in C
	jp	bdosv		;print msg and back to CP/M
;
; put length of fixed code here to allow easier patching of
; user's CCP chain command:

szfixed:
	defw	tkcln-Fboot	;size of executable
szchain:
	defb	tkcend-tkcln	;length of chain command
;
tsmlmsg:
	defb	0dh,0ah,0ah,'***  No space for GIDE BIOS!!'
	defb	'  Memory size must be 62K or less.  ***'
	defb	0d,0ah,'$'


BiosCode:
	.phase	HDBIOS

; Define a replacement set of BIOS jump vectors which point to
; our custom functions or back to the standard BIOS functions,
; as appropriate.

Fboot:
	jp	Hboot		;cold boot
Fwboot:
	jp	Hwboot		;warm boot
Fconst:
	jp	Fconst		;console status
Fconin:
	jp	Fconin		;console input
Fconot:
	jp	Fconot		;console output
Flist:
	jp	Flist		;list output
Fpunch:
	jp	Fpunch		;punch output
Freader:
	jp	Freader		;reader input
Fhome:
	jp	Hhome		;home disk drive
seldsk	equ	$-Fboot+bios	;seldisk in BIOS jmp tbl
Fseldsk:
	jp	Hseldsk		;select drive
Fsettrk:
	jp	Hsettrk		;set track
Fsetsec:
	jp	Hsetsec		;set sector
Fsetdma:
	jp	Hsetdma		;set dma address
Fread:
	jp	Hread		;read sector
Fwrite:
	jp	Hwrite		;write sector
Flistst:
	jp	Flistst		;list status
Fsectrn:
	jp	Hsectrn		;sector translate

szjptab	equ	$-Fboot		;size of jump table

;***************************************************************
;Some fixed data options.
;
;
;selary -- array of select values for mapping the disk number
;	passed to SELDSK to either the floppy-disk bios or the
;	hard disk bios--selecting which disk to use.
;
;	00-0F = Standard CBIOS
;	80-8F = Hard-disk extension BIOS, SCSI/SASI
;	A0-AF = Hard-disk extension BIOS, IDE (primary unit)
;	B0-BF = Hard-disk extension BIOS, IDE (secondary unit)
;	FF    = invalid drive
;
selary:
	defb	0		;A: = first floppy
	defb	1		;B: = second floppy
	defb	2		;C:
	defb	3		;D:
	defb	0a0h		;E: = first IDE disk (head 0)
	defb	0a1h		;F:
	defb	0a2h		;G:
	defb	0a3h		;H:
	defb	0a4h		;I:
	defb	0ffh		;J: = no such disk
	defb	0ffh		;K:
	defb	0ffh		;L:
	defb	0ffh		;M:
	defb	0ffh		;N:
	defb	0ffh		;O:
	defb	0ffh		;P:
;
; wbtdsk -- disk number 0-F to use for warm boots
;
wbtdsk:
	defb	wbtdrv-'A'	;warm boot disk number 0-F
;
; szwrmbt -- number of CPM sectors to read on warm boot
;
szwrmbt:
	defw	nsecs


;***************************************************************
;Function:  Hhome
;Purpose:   Home current disk
;Entry:     None
;Exit:      Disk homed
;Uses:      AF, BC

Hhome:	call	chkide		;is it our disk?
	jr	z,Fhome		;no, jump to FBIOS
;
	ld	bc,hstact	;BC->hstact,hstwrt
	ld	a,(bc)		;hstact in A
	or	a		;set flagz
	jr	z,Hhomed	;brif host not active
	inc	bc		;bump to hstwrt
	ld	a,(bc)		;hstwrt in A
	or	a		;set flagz
	jr	nz,Hhomed	;brif pending write
	dec	bc		;else bump back to hstact
	ld	(bc),a		;and clear host active flag
Hhomed:
	ld	bc,0		;track 0 in BC
	jr	Hsttrk0		;set it

;***************************************************************
;Function:  Hsettrk
;Purpose:   Set logical track number for disk I/O
;Entry:     BC = logical track number
;Exit:      track set up
;Used:      All

Hsettrk:
	call	chkide		;is it our disk?
	jr	z,Fsettrk	;no, jump to Fbios
;
Hsttrk0:
	ld	(sektrk),bc	;save it
	ret			;and done


;***************************************************************
;Function:  Hsetsec
;Purpose:   Set logical sector number for disk I/O
;Entry:     BC = logical sector number
;Exit:      sector set up
;Used:      All

Hsetsec:
	call	chkide		;is it our disk?
	jr	z,Fsetsec	;not our disk.  Jump to FBIOS
;
	ld	(SEKSEC),bc	;save sector to seek
	ret


;***************************************************************
;Function:  Hsetdma
;Purpose:   Set transfer address for disk I/O
;Entry:     BC = dma address
;Exit:      DMA address set up
;Used:      All

Hsetdma:
	ld	(dmaadr),bc	;set it here
	jr	Fsetdma		;and let FBIOS set it as well.
	

;***************************************************************
;Function:  Hsectrn
;Purpose:   Do logical->physical sector translation
;Entry:     BC = logical sector number, DE points to xlate table
;Exit:      HL = physical sector number
;Uses:      All

Hsectrn:
	call	chkide		;is it our disk?
	jr	z,Fsectrn	;not our disk, jump to FBIOS
;
	ld	l,c		;L=C
	ld	h,b		;H=B (no translation)
	ret			;and we're done


;***************************************************************
;Function:  Hwrite
;Purpose:   Write logical CP/M sector
;Entry:     disk, track, sector, DMA, etc. set up
;Exit:      sector transferred to disk..sort of
;Uses:      All

Hwrite:
	call	chkide		;is it our disk?
	jr	z,Fwrite	;no, jump to FBIOS
;
	XOR	A		;0 in A
	LD	(READOP),A	;indicate a write operation (READOP)
	LD	A,C		;get the write type in A
	LD	(wrtype),A	;store at WRTYP
	CP	wrual		;is it write to unallocated?
	JR	NZ,chkuna	;no, jump over (to chkuna)
;
	LD	HL,SEKDSK	;point to SEKDSK
	LD	DE,UNADSK	;set pointer to UNADSK
	LD	BC,sekln	;count of data to move
	LDIR			;copy SEKDSK --> UNADSK
	LD	A,(HDBLM)	;get BLM
	INC	A		;make sec/blk
	LD	(UNACNT),A	;set up unacnt
chkuna:
	LD	HL,UNACNT	;point at unacnt
	XOR	A		;0 in A
	CP	(HL)		;any more unallocated sectors?
	JR	Z,alloc1	;JRIF no more (alloc1)
	DEC	(HL)		;--unacnt
	LD	HL,SEKDSK	;HL points to SEKDSK
	LD	DE,UNADSK	;point to unadsk disk data
	LD	BC,sekln	;count of data to move/compare
	CALL	comp1		;compare with SEKDSK
	JR	NZ,alloc0	;JRIF not same disk (alloc0)
;
	LD	HL,(UNASEC)	;get unasec
	INC	HL		;bump up 1 
	LD	(UNASEC),HL	;put it back
	LD	DE,(DPBHD)	;get cp/m sec/trk
	or	a		;clear carry
	sbc	hl,de		;check for overflow
	JR	C,noovf0	;JRIF no overflow 
;
	ld	hl,0000		;overflow, 0 in HL
	LD	(UNASEC),HL	;reset unasec to 0
	LD	HL,(UNATRK)	;get UNATRK
	INC	HL		;bump to next track
	LD	(UNATRK),HL	;and put it back
noovf0:
	XOR	A		;0 in A
	JR	noovf1		; and do a write


;***************************************************************
;Function:  Hread
;Purpose:   Read logical CP/M sector
;Entry:     disk, track, sector, DMA, etc. set up
;Exit:      sector transferred to memory
;Uses:      All

Hread:
	call	chkide		;is it our disk?
	jp	z,Fread		;no, jump to FBIOS
;
	LD	A,wrual		;treat as write to unallocated
	LD	(wrtype),A	;store at WRTYPE
	LD	(READOP),A	;indicate a read (READOP)
alloc0:
	XOR	A		;0 in A
	LD	(UNACNT),A	;clear unacnt
alloc1:
	INC	A		;bump to 1
noovf1:
      	LD	(RSFLAG),A	;flag a rd(!0) or wr(0) (rsflag)
	LD	(OLDSTACK),SP	;save old stack pointer
	LD	SP,BIOSSTK	;set up local stack pointer
;rwoper:
; calculate host sector...
;
	ld	a,(SecShf)	;get sector shift
	ld	b,a		;into B
	or	a		;check for 0
	ld	hl,(SEKSEC)	;get sector
	jr	z,rwoper1	;jrif 0 (don't shift)
rwoper0:
	or	a		;clear carry
	rr	h		;shift sector right H:0->Cy
	rr	l		;shift sector right Cy->L:7
	djnz	rwoper0		;until found
rwoper1:
	inc	hl		;make 1-relative
	ld	(SEKHST),HL	;store host sector to seek
;
	XOR	A		;0 in A
	LD	(erflag),A	;no errors (yet)
	LD	HL,HSTACT	;point at hstact
	OR	(HL)		;OR in the host active flag
	LD	(HL),1		;always becomes 1
	JR	Z,filhst	;JRIF was not already 1 (filhst)
;
	LD	HL,SEKHST	;HL points to SEKHST
	LD	DE,HSTSEC	;DE points HSTSEC
	LD	BC,sekln	;length to move/compare in BC
	call	comp1		;compare
	JR	Z,match		;jrif same buffer, disk, track
;nomatch:
	LD	A,(HSTWRT)	;match, host written?
	OR	A
	CALL	NZ,WRITEHST	;flush pending write
;
filhst:
	LD	HL,SEKHST	;point at SEKHST
	LD	DE,HSTSEC	;point to HSTSEC stuff
	LD	BC,sekln	;count of data to move
	LDIR			;set up HSTDSK
	LD	A,(RSFLAG)	;GET RSFLAG
	OR	A		;Need to read?
	CALL	NZ,READHST
	XOR	A		;0 in A
	LD	(HSTWRT),A	;no pending write
match:
	LD	HL,(SecMsk)	;get mask value in L
	LD	A,(SEKSEC)	;get SEKSEC
	AND	L		;mask with L (A has sector offset)
	LD	BC,0080H	;sector size in BC
	LD	HL,dbuf		;buffer base in HL?
	RRA			;A /= 2 (A:0->Cy)
	LD	D,A		;in D  (make high byte of offset)
	LD	E,B		;0 in E
	RR	E		;E /= 2	(Cy->E:7)
	ADD	HL,DE		;make address of CP/M sector in HL
	LD	DE,(DMAADR)	;get DMA address in DE
	LD	A,(READOP)	;get READOP
	OR	A		;is this a READ?
	JR	NZ,rwmove	;yes, skip
	EX	DE,HL		;no, swap pointers
	INC	A
	LD	(HSTWRT),A	;hstwrt = 1
rwmove:
	LDIR			;move data to/from host buffer
	LD	A,(wrtype)	;get WRTYPE
	DEC	A		;bump down
	LD	A,(erflag)	;get erflag
	LD	SP,(OLDSTACK)	;restore the stack
	RET	NZ		;return if WRTYPE was wrall or wrual
	OR	A		;was wrdir, errors?
	RET	NZ		; return if errors
	LD	(HSTWRT),A	;no errors, mark buffer written
				;and fall through to writehst sector...


;***************************************************************
;Function:  WriteHst
;Purpose:   Do physical write to IDE drive
;Entry:     hstdsk, hsttrk, hstsec set up
;Exit:      sector in dbuf written to disk
;Used:      all

Writehst:
	call	tfsetup		;set up Task File
	ld	a,iCmdWr	;get a write command
	out	(iRgCmd),a	;send it to Task File
	call	WaitDrq		;wait for data
	otir
	otir			;write 512 bytes
	jr	chkerr		;check for/report errors


;***************************************************************
;Function:  ReadHst
;Purpose:   Do physical read of IDE drive
;Entry:     hstdsk, hsttrk, hstsec set up
;Exit:      sector read into (hstbuf)
;Used:      all

Readhst:
	call	tfsetup		;set up Task File
	ld	a,iCmdRd	;get a read command
	out	(iRgCmd),a	;send to task file
	call	WaitDrq		;wait for data
	inir
	inir			;read 512 bytes
chkerr:	
	in	a,(iRgStat)	;get status
	and	1		;check error bit
	ld	(erflag),a	;save as error flag
	ret			;return error/no error


;***************************************************************
;Function:  Hwboot
;Purpose:   perform initializations and re-load CP/M
;Entry:     zero page set up
;Exit:      initializations done, current disk in C
;Used:      All

	if	jdbfix
gocpm	equ	bios+122h	;loads HL w/Fbios WBOOTGO adr
	else
gocpm	equ	bios+123h	;loads HL w/Fbios WBOOTGO adr
	endif
gocpm1	equ	gocpm+3		;pushes HL for RET

Hwboot:	ld	sp,tbuff	;set stack below buffer
	ld	a,(wbtdsk)	;get the disk we warm-boot from
	ld	c,a		;in C
	ld	e,0		;inidicate 1st select
	call	seldsk		;Hseldsk ;set it via BIOS jmp tbl
	call	chkide		;is it our disk?
	jr	nz,hwbt1	;brif our disk
hwbftl:	call	rdycpm		;else do required initializations...
	jp	Fwboot		;and continue here.
;
hwbt1:	ld	a,(sekdsk)
	ld	(hstdsk),a	;set up disk
	ld	hl,0		;track 0
	ld	(hsttrk),hl
	ld	hl,2		;start at host sector 2
	ld	(hstsec),hl
	ld	hl,ccp		;get load address
	ld	(hstbuf),hl	;set it up
rdsec:
	call	ReadHst		;read a sector
	or	a		;errors?
	jr	z,bumpsec	;brif no error
;
	in	a,(iRgErr)	;get error register
	and	10h		;did we run off end of track?
	jr	z,hwbftl	;NO!  Panic!  Ask floppy to do it...
	ld	hl,(hsttrk)	;end of track, get last track
	inc	hl		;bump to next
	ld	(hsttrk),hl
	ld	hl,1		;and reset to host sector 1
	jr	bmpsc1		;and continue here
;
bumpsec:
	ld	hl,(hstsec)	;get last sector
	inc	hl		;bump to next
bmpsc1:
	ld	(hstsec),hl
	ld	hl,(hstbuf)	;get last DMA address
	ld	bc,hstsiz	;increment in BC
	add	hl,bc		;make next address
	ld	(hstbuf),hl
	ld	a,h		;xfr page in A
	ld	hl,(fbios)	;get CBIOS base addr
	cp	h		;compare w/BIOS base page
	jr	c,rdsec		;brif more to do (A<H)
;
	ld	hl,gocpm	;finish up in CBIOS
	push	hl		;use this trick 2x
;
rdycpm:
	ld	hl,0000		;0000 in HL
 	ld	(unacnt),hl	;clr unalloc cnt, hst buf inactive
	ld	hl,dbuf		;get address of host buffer
	ld	(hstbuf),hl	;and put it here
	ld	(dmaadr),hl	;and set DMA address
	ret			;and go back


;***************************************************************
;Function:  Hseldsk
;Purpose:   Select disk drive
;Entry:     Disk number 0-F in C
;Exit:      Address of DPH in HL or 0000 if error
;Used:      All

Hseldsk:
	ld	hl,0		;assume select error
	ld	a,0fh		;max disk in A
	cp	c		;compare w requested disk
	jr	c,errsel	;jrif out of range
;
	ld	b,l		;L=0, put in B
	call	makesel		;get disk select data
;
	inc	a		;if FF, now 00
	ld	l,b		;B=0, in HL if invalid disk
	ld	h,b		;or it's our disk
	jr	z,errsel	;jrif no such disk
	dec	a		;else restore to previous
;
	rlca			;preserve bit 7
	rrca			;into Carry
	jr	c,Hsldsk1	;if bit 7 set, it's our disk
;
	ld	c,a		;CBIOS disk, put back in C
	xor	a		;0 in A
	ld	(idesel),a	;let our HBIOS segment know
	jp	Fseldsk		;and let Fbios handle it
;
Hsldsk1:
	ld	(idesel),a	;flag as our disk
	or	0fh		;only need low nybble
	ld	l,a		;and put in L
	ld	a,c		;get disk number again
	ld	(sekdsk),a	;save disk number
	add	hl,hl		;*2
	add	hl,hl		;*4
	add	hl,hl		;*8
	add	hl,hl		;*16
	ld	de,hdpbase	;get base of hard disk dph's
	add	hl,de		;make pointer to DPH for drive
	ret			;and go back

errsel:				;HL=0 already
	ld	a,(cdisk)	;get current disk
	ld	b,a		;save in B
	and	0fh		;strip user #
	sub	c		;same as selected?
	ret	nz		;wasn't same
	ld	a,b		;else get cdisk back
	and	0f0h		;mask user number
	ld	b,a		;save user in B
	ld	a,(wbtdsk)	;get warm boot drive
	or	b		;OR in user number
	ld	(cdisk),a	;force warm-boot drive current
	ret			;and return with error

makesel:			;on entry B=0, C=disk
	ld	hl,selary	;base of select array in HL
	add	hl,bc		;make pointer to select data
	ld	a,(hl)		;get the select data
	ret


;***************************************************************
;Function:  TFsetup
;Purpose:   send host parms to IDE Task File registers
;Entry:     hstdsk, hsttrk, hstsec set up
;Exit:      Task file registers set up.  HL points to dbuf,
;	    BC has count, data port address

tfsetup:
	in	a,(iRgStat)	;get status
	rla			;BSY bit to Carry
	jr	c,tfsetup	;loop until not BUSY
;
	ld	a,1		;1-sector transfer
	out	(iRgSCnt),a	;tell IDE drive
	ld	a,(hstsec)	;get host sector number
	out	(iRgSNum),a	;send to Task File
	ld	hl,(hsttrk)	;get host track
	ld	a,l		;get low order byte
	out	(iRgCLo),a	;send to Task File
	ld	a,h		;get high-order byte
	out	(iRgCHi),a	;send to task file
	ld	a,(hstdsk)	;get host disk number
	ld	c,a		;put in c
	ld	b,0		;0 in B
	call	makesel		;get the select data in A
	and	0fh		;mask low nybble
	ld	c,a		;in C
	ld	hl,sdhbase	;point to base of SDH array
	add	hl,bc		;make pointer in HL
	ld	a,(hl)		;get the SDH byte
	out	(iRgSDH),a	;send to drive
;
	ld	hl,(hstbuf)	;HL points to host buffer
	ld	c,iRgDat	;B=0 (256), C->IDE data reg.
	ret			;and go back
	


;***************************************************************
;Function:  chkide
;Purpose:   check if disk operation is for IDE disk or floppy
;Entry:     "idesel" is set for IDE disk, clear for floppy
;Exit:      Z set if floppy operation, clear if IDE disk
;Uses:      AF

chkide:	ld	a,(idesel)	;is it our disk?
	or	a		;set flagz
	ret			;and done


;***************************************************************
;Function:  WaitDrq
;Purpose:   Wait until data available or ready for data
;Entry:     None
;Exit:      None
;Uses:      AF

WaitDrq:
	in	a,(iRgStat)
	and	8
	ret	nz
	jr	WaitDrq


;***************************************************************
;Function:  strcmp
;Purpose:   Compare two strings [(SEK|UNA|HST)TRK]
;Entry:     HL, DE have addresses of strings, BC = length
;Exit:	    Z set if strings are the same, clear if different
;		P/V flag clear if entire string checked
;Uses:	    AF, BC, DE, HL

	if 	jdbfix
comp1	equ	bios+274h
	else
comp1	equ	bios+267h
	endif

	if	false
strcmp:
	LD	A,(DE)		;get a byte from (DE)
	CPI			;compare A-(HL++), BC--
	RET	NZ		;return if no match
	RET	PO		;return if string exhausted
	INC	DE		;else bump to next source byte
	JR	strcmp		;and repeat comparison
	endif


;***************************************************************
;The clock driver conforms to the ZS/ZDDOS/DateStamper standard.
;On entry:
;		DE = address of 6-byte BCD string of the form:
;			YY MM DD HH MM SS
;		 C = 0 for read, 1 for set
;
;On exit:
;		 E = previous contents of (DE+5) [old seconds]
;		HL = entry value of DE + 5 [ptr to new seconds]
;		 A = 1 if success, FF if error.
;
; *** NOTE ***
; This particular instance of the clock driver is for the Davidge
; DSB 4000 BIOS v1.3 and the CardZ180 OEM version of ZDDOS.
; For use with standard (generic) ZDDOS, some re-arrangement
; may be required.
;
; It is more probable that the clock driver could be stashed in
; some extra space in the NZCOM VBIOS segment (NZBIO) once the
; VBIOS segment is loaded.
;


ClkDrv:
	ex	de,hl		;time pointer to HL
	dec	c		;check read or set
	jr	z,RTCset	;C->0 if was set command
RTCread:
	call	RTCwait 	; check for RTC non-busy
RTCrd1:
	ld	e,(hl)		; save byte (last will be old seconds)
	in	a,(c)		; read tens digit
        rld			; rotate into [HL]:0-3
	dec	b		; bump to units
	in	a,(c)		; read units digit
	rld			; and rotate units in...
	inc	hl		; bump pointers
	dec	b
	jp	p,RTCrd1	; repeat until seconds done
RTCexit:
	dec	hl		; reset HL to new seconds
	ld	b,0Dh
	xor	a
	out	(c),a		; reset Hold bit
	inc	a		; return success in A
	ret

; Set date & time of RTC according to buffer given by (DE)

RTCset:
	call	RTCinit		; Init the clock on a set, just in case...
RTCst1:
	ld	e,(hl)		; save byte (last will be seconds)
	rld			; get tens into A
	out	(c),a		; write tens to RTC
        dec     b		; bump to units
	rld			; get units to A
	out	(c),a		; write units to RTC
	rld			; and restore (HL)
	inc	hl		; bump pointers
	dec	b
	jp	p,RTCst1	; repeat until seconds done
	jr	RTCexit		; and done


; Init the RTC for normal operation.
; If the RTC is battery backed, this is needed only for
; the first time the RTC is powered up!

RTCinit:
	ld	bc,0F00h+RTC	; access Ctl-Reg. F
	ld	a,5
	out	(c),a		; RTC Ctl-F: 24h mode, Reset
	dec	a
	out	(c),a		; release Reset

				;and fall through to wait routine...

; Prepare for R/W access to the RTC. The hold bit is set,
; and the busy flag checked to see if accesses are allowed.
; Also set up time register pointer in B.

RTCwait:
	ld	bc,0D00h+RTC	; access Ctl-D
	ld	a,1
        out     (c),a           ; set Hold bit
        in      a,(c)
        bit     1,a             ; check Busy flag
	jr	z,RTCntbsy	; not busy: set up B and return
	xor	a
	out	(c),a		; reset Hold bit
	jr	RTCwait 	; go try again
RTCntbsy:
	ld	b,0Bh		; point at year tens register
	ret


;***************************************************************
;Fixed data area
;

SDHBASE:
	defb	0a0h		;SDH byte for first logical drive
	defb	0a1h
	defb	0a2h
	defb	0a3h
	defb	0a4h		;five of them (for now)


HDPBASE:
HDPH0:	defw	0000		;XLT0
	defw	0000
	defw	0000, 0000	;scratch
	defw	dirbuf
hdpbofs	equ	$-hdpbase
	defw	DPBHD
	defw	CSV0
	defw	ALV0

HDPH1:	defw	0000		;XLT1
	defw	0000
	defw	0000, 0000	;scratch
	defw	dirbuf
	defw	DPBHD
	defw	CSV1
	defw	ALV1

HDPH2:	defw	0000		;XLT2
	defw	0000
	defw	0000, 0000	;scratch
	defw	dirbuf
	defw	DPBHD
	defw	CSV2
	defw	ALV2

HDPH3:	defw	0000		;XLT3
	defw	0000
	defw	0000, 0000	;scratch
	defw	dirbuf
	defw	DPBHD
	defw	CSV3
	defw	ALV3

HDPH4:	defw	0000		;XLT4
	defw	0000
	defw	0000, 0000	;scratch
	defw	dirbuf
	defw	DPBHD
	defw	CSV4
	defw	ALV4

;
; DPB followed by shift factor and mask value makes a CP/M 3-style
; "xDPB".
;
;A proposed Disk Parameter Block for 8MB drives with 8K blocks
DPBHD:
	defw	68	;17*4 CP/M Sec/trk
	defb	6	;64 recs/block
HDBLM:
	defb	63	;ditto
	defb	3	;extent mask
	defw	3ffh	;1024 blocks
	defw	3ffh	;1024 directory entries
	defb	0f0h	;4 blocks of directory
	defb	0	;
	defw	0	;no scratch
	defw	2	;2 tracks offset
;
; Host Parameters for Conner CP-342 IDE drive...
SecShf:
	defb	2		;log_2(512/128)
SecMsk:
	defb	3		;512/128 - 1


;***************************************************************
;one-time use code--will be overlaid w/udata areas
;
Hboot:	ld	hl,Hwboot	;get address of warm boot code
	ld	(Fboot+1),hl	;cut off access to this code
;
	ld	a,0C3h		;get a jump instruction
	ld	(ZDTIME),A	;and put it here
	ld	hl,ClkDrv	;get address of clock driver
	ld	(ZDTIME+1),HL	;and stick it here


;***************************************************************
;Function:  SWPJTAB
;Purpose:   Swap stock BIOS jump table with ours
;Entry:     Standard CP/M v2.2 BIOS jump table
;Exit:      Jump tables swapped.
;Uses:      All

swpjtab:
	ld	hl,(WBOOTV+1)	;get Warm boot
	ld	l,0		;point at start
	ld	(Fbios),hl	;save CBIOS base adr
	ld	de,Fboot	;point at our jump table
	ld	b,szjptab	;size in B

swpjtlp:
	ld	c,(hl)		;get stock byte
	ld	a,(de)		;get our byte
	ld	(hl),a		;put it where stock was
	ld	a,c		;stock byte in A
	ld	(de),a		;put it where ours was
	inc	hl		;bump stock pointer
	inc	de		;bump our pointer
	djnz	swpjtlp		;until all bytes done
;
	if	jdbfix
prtwd	equ	bios+56bh	;BIOS print CRLF+(HL)
	else
;
; patch two "jmp wboot" so they go through the BIOS jump table.
;
wbpof0	equ	42eh
wbpof1	equ	43ch
;
	ld	c,h		;save BIOS base page in C
	ld	b,2		;number of patches to make
	ld	de,wbpof0	;point at patch table
	jr	patch		;go fix it
patch0:	ld	de,wbpof1	;the second
	ld	h,c
patch:	ld	l,0		;HL=BIOS
	add	hl,de		;make address to patch
	ld	(hl),3		;offset of wboote
	inc	hl		;bump to high byte
	ld	(hl),c		;install BIOS page
	djnz	patch0		;and go again
;
prtwd	equ	bios+55eh	;BIOS print CRLF+(HL)
	endif
;
	ld	hl,signmsg	;point at signon message
	call	prtwd		;print it
;
	call	rdycpm		;initialize data pointers
				;and fall through to chain code...

;***************************************************************
;code to stuff CCP command buffer with chain command
;
	ld	a,(BDOSV+2)	;get BDOS base page
	sub	8		;back up to CCP base
	ld	d,a		;in D
	ld	e,7		;DE->cibuff
	ld	hl,tkcln	;point at our command
	ld	a,(hl)		;get length
	inc	a		;else make byte count
	ld	c,a		;in C (B=0 from 'djnz' above)
	ldir			;put our command in buffer
	ex	de,hl		;CCP space in HL
	ld	(hl),b		;set terminator
	ld	l,b		;0 in HL (point to CCP)
	ld	a,(cdisk)	;get current disk
	ld	c,a		;in C for CCP
	jp	(hl)		;and finish here
;
signmsg:
	defb	0dh,'RehDesign GIDE:  62k BIOS for Davidge DSB 4/6'
	defb	0dh,0ah,' -- John D. Baker, 30 September 2026'
	defb	0dh,0ah+80h
;
tkcln:	defb	tkcend-tkcmd	;turn-key command length
tkcmd:	defb	'NZCOM'		;command string
tkcend	equ	$

szhcbt	equ	$-Hboot

	.dephase

	if	(hstsiz-szhcbt) gt 07fffh
	  .printx	"	***  Cold boot code too large!  ***	"
	endif

	.phase	Hboot
;***************************************************************
;RAM Variables and unitialized data
;
dbuf:	defs	hstsiz		;dbuf
;
idesel:	defs	1		;flag if disk operation is for us.

; DO NOT CHANGE THE ORDER OF THESE DATA AREAS!!!

hstsec:	defs	2		;host sector
hstdsk:	defs	1		;host disk
hsttrk:	defs	2		;host track
sekhst:	defs	2		;host sector to seek
sekdsk:	defs	1		;disk to seek
sektrk:	defs	2		;track to seek
sekln	equ	$-sekhst	;length of data to move/compare
seksec:	defs	2		;sector to seek
unadsk:	defs	1		;unallocated disk
unatrk:	defs	2		;unallocated track
unasec:	defs	2		;unallocated sector
unacnt:	defs	1		;unallocated sector count
hstact:	defs	1		;host active flag
hstwrt:	defs	1		;host written flag
erflag:	defs	1		;error reporting flag
rsflag:	defs	1		;read sector flag
dirbuf:	defs	128
CSV0:
ALV0:	defs	128
CSV1:
ALV1:	defs	128
CSV2:
ALV2:	defs	128
CSV3:
ALV3:	defs	128
CSV4:
ALV4:	defs	128
	defs	24	;12-level stack
biosstk:
fbios:	defs	2	;save CBIOS base adr here
;
; locate these above ZDTIME to get more space...
readop	equ	xtrspc		;1 if this is a read (FFF8)
wrtype	equ	readop+1	;type of write operation (FFF9)
dmaadr	equ	wrtype+1	;last dma address (FFFA)
hstbuf	equ	dmaadr+2	;address of host buffer (FFFC)
oldstack equ	hstbuf+2	;old stack pointer (FFFE)

	.dephase		;end of HDBIOS extension

	if	(biosstk gt systop) or (biosstk lt Fboot)
	  .PRINTX	"	***  Origin of HDBIOS too high!  ***	"
	endif

	end
