	.Z80
;***************************************************************
;File:	HDBOOT.MAC
;Purp:	512-byte cold boot sector for hard disk to load CP/M
;	image from RehDesign GIDE board as implemented for
;	Davidge DSB 4000 v1.3
;
;Auth:	John D. Baker
;
; Revision History:
;
; 20 September 2026
;	Reworked sector count computation to handle 8-bit sector
;	count register.
;
;  1 July 2025	(JDB)
;	Adapted to MBR-esque boot sector/partition table as seen
;	on https://ardiehl.de implemented by Armin Diehl for his
;	Prof-80 ECB Z80 system.
;
;  2 May 2025	(JDB)
;	SECID_NF on a read returns the last valid sector's data
;	so don't advance the buffer address if we ran off the
;	end of the track so the next valid sector shows up in
;	the right place.  (lots of DE<->HL jiggery-pokery).
;
;	Find correct image length to compute # of host sectors.
;
; 26 April 2025	(JDB)
;	Adapt for use as hard disk boot sector (different ORG,
;	max size, etc.).  Also add switches to make floppy disk
;	chain loader instead of direct hard disk boot
;
; 25 April 2025	(JDB)
;	Created.

false	equ	0
true	equ	not false
test	equ	true

debug	equ	true		;so we can test with DDT(Z)

;
msize	equ	62	;cp/m version memory size in kilobytes
;
;	"bias" is address offset from 3400h for memory systems larger
;	than 16k (referred to as "b" throughout the text)
;
	if	test
bias	equ	(msize-20)*1024
	else
bias	equ	0	;generate relocatable system
	endif
ccp	equ	2a00h+bias	;base of ccp
bdos	equ	ccp+800h	;base of bdos
bdose	equ	bdos+6		;entry point of bdos
bios	equ	ccp+1600h	;base of bios
boot	equ	bios		;cold boot address

; MOVCPM leaves start of CCP at 0980h.
; If we're testing, load OS image there.
movcpm	equ	0980h

; The image produced by MOVCPM places the BIOS beginning at 1F80h.
; Davidge DSB 4/6 floppy-disk BIOS ends at 28FFh,
; as reported by DDT (Next 2900)
; When the GIDE BIOS extension is added, the image ends at 2F7Fh,
; as reported by DDT (Next 3000)
;
fbiose	equ	2900h	;end of Davidge floppy disk CBIOS
hbiose	equ	2f80h	;end of GIDE extension BIOS
fbiosln equ	fbiose-1f80h	;length of Davidge CBIOS
hbiosln equ	hbiose-1f80h	;length w/GIDE extenstion
biosln	equ	hbiosln
sysln	equ	bios+biosln-ccp	;length of full system image

; Davidge boot ROM is 2K (2048 bytes).	On power-up/reset, it copies
; itself in-place to RAM starting at 0000h.	Later revisions support
; hard disk boot and load the HD boot sector at the end of the ROM
; space.	Do likewise.

romsiz	equ	0800h	;2048 bytes, 2K
	if	not debug
dmavec	equ	BIOS+0B0H
	else
dmavec	equ	0058H	;ROM code DMAC intvec
	endif
;
	maclib	PORTDEF
	maclib	FLOPDEF
	maclib	IDEDEF
;
; host disk params:
hstsiz	equ	512	;HD sector size
secsiz	equ	hstsiz
btcdmxz equ	170h	;maximum boot code size

tpa	equ	0100h	;standard CP/M TPA for debugging

strtsec equ	2	;load starts @sec 2 of 1st trk
nhsecs	defl	sysln/hstsiz
	if	sysln mod hstsiz
nhsecs	defl	nhsecs+1  ;and reads this many sectors
	endif

if	debug
base	equ	tpa
ldadr	equ	movcpm	;load to same place as MOVCPM
	else
base	equ	romsiz	;advanced Davidge boot ROM loads
; HD boot sector here
ldadr	equ	ccp	;standard load address
	endif

	aseg	;'<-make 'a2ps' behave

	org	base

cold1:			;A 1 that isn't cold is scarcely a 1 at all.
	jr	cold	;jump over hybrid data/code
;
;Partition boot sector signature:
partsig:
	defb	'CPM2JB'  ;with apologies to Armin Diehl
;
;DPB for this partition:
prtdpb:
	defw	68	;SPT	;128-byte CP/M sectors
	defb	6	;BSH	;64 rec/blk (8K blks)
	defb	63	;BLM
	defb	3	;EXM
	defw	1023	;DSM	;1024 blocks (8MB capacity)
	defw	1023	;DRM	;1024 directory entries
	defb	0f0h	;AL0	;4 blocks of directory
	defb	0	;AL1
	defw	0	;CKS	;no check vector
	prtofs:	defw	2	;OFS	;2 reserved tracks
;
;xDBP adds physical shift factor (PSH), and physical shift mask (PSM)
	defb	2	;PSH	;4 CP/M secs per host sec
	defb	3	;PSM
;
;9 reserved bytes
	defs	9	;0,0,0,0,0,0,0,0,0
;
;Description of partition, 1-byte length, up to 20 byte text
	defb	prtdscln
partdesc:
	defb	'Legacy GIDE DSB-4/6'
prtdscln equ	$-partdesc

	if	prtdscln gt 20
	.printx "* * * Partition Description too long! * * *"
	else
	defs	20-prtdscln
	endif
;
;ROM code sets A to the SDH byte for the "disk" from which
;it loaded the boot sector so save it for loading the system.

cold:
	ld	hl,btpart	;HL->boot partition #
;
	and	0f0h	;mask unit
	ld	b,a	;in B
	ld	a,(hsthds)  ;get # of heads
	cp	1	;only 1?
	ld	a,b	;get unit back
	jr	nz,cylprt  ;brif more than 1 head
	ld	a,(hl)	;get boot partition number
	or	b	;make hstdsk
cylprt:
	ld	(hstdsk),a	;save it here
;
;get boot part start cylinder
	ld	a,(hl)	;get boot part # again
	inc	hl	;bump to start of partition table
	add	a,a
	add	a,a	;*4 to make offset
	add	a,l	;low byte of ptr
	ld	l,a	;in L
	jr	nc,btpcyl0	;brif no carry
	inc	h	;else bump H
btpcyl0:
	ld	a,(hl)	;low byte in A
	inc	hl	;bump to high byte
	ld	h,(hl)	;in H
	ld	l,a	;start cylinder in HL
	ld	(hsttrk),hl	;save it
;
	ld	hl,strtsec	;start reading at this sector
	ld	(hstsec),hl
;
;set up command byte, starting address, and # of sectors to load
	ld	a,iCmdRd	;Read command
	ld	(cmdbyt),a	;save here for init loop
	ld	hl,sysbase	;HL->starting load address
	ld	de,hstbuf	;DE->udata copy
	ld	bc,4		;4 bytes
	ldir			;move it
;
; compute host sec/cyl (C=convenient zero)
	ld	hl,(hsthds)	;H=SPT, L=#hds
	ld	b,l		;#hds in B for loop
	ld	e,h		;SPT in E for loop
	ld	d,c		;D=0
	ld	l,c		;HL=0
	ld	h,c
spclp:
	add	hl,de		;spt*hds
	djnz	spclp		;B=0 on exit		
	ld	(hstspc),hl	;save it
;
;compute how many sectors to read on first cylinder
;	ld	hl,(hstspc)	;sec/cyl in HL
	dec	hl		;always 1 less on first cyl
	ld	(remspc),hl	;save remaining sec/cyl
rdtrks:
	ex	de,hl		;DE=sec/cyl
	ld	hl,(remsecs)	;get remaining sectors to load
	or	a		;clear carry
	sbc	hl,de		;subtract
	jr	nc,samsiz	;remsecs >= hstspc (-1)
	add	hl,de		;remsecs < hstspc, restore remainder
	ex	de,hl		;save new sector count in DE
	ld	l,b		;0 in L (no more sectors)
	ld	h,b		;0 in H
; if no carry, HL (old remsecs) >= DE (sec/cyl)
; if carry, HL (old remsecs) < DE (sec/cyl)
; The smaller non-zero value of either sec/cyl or remaining # of sectors
; is now in DE.  If this is more than 256, we need to put back all but
; 256 of them since iRgSCnt is only 8 bits.
samsiz:
	ld	a,d		;DE >=256?
	or	a		;test
	jr	z,remupd	;DE < 256, update remainder
ge256:
	dec	d		;else bump down by 256
	add	hl,de		;add back to remaining sectors
	ld	e,b		;0 (=256) in E
remupd:
	ld	(remsecs),hl	;save remaining sector count
	ld	a,e		;(remsecs - hstspc) mod 256
	ld	(sctrcnt),a	;save for Task File init loop
;A=E=max # of sectors to read in one operation.  Update the count
;of sectors on the current cylinder for subseqent reads.
	ld	d,b		;D=0, DE=# secs to read
	or	a		;(A=E)==0?
	jr	nz,foo		;jrif <256
	inc	d		;else DE=256
foo:
	ld	hl,(remspc)	;get remaining secs/cyl
	sbc	hl,de		;subtract read count
	jr	nc,foo1		;jrif more secs on this cyl
	ld	l,b		;else no more sectors
	ld	h,b		;HL=0
foo1:
	ld	(remspc),hl	;save remaining secs on this cyl
;
;
iWtRdy:
	in	a,(iRgStat)	;get status
	rla			;BSY bit to Carry
	jr	c,iWtRdy	;loop until not BUSY
;
; Stupid Z80 tricks:  Use OUTI instruction to initialize Task File
; registers.  For the Z flag to indicate whether the loop terminates,
; we must increment the port # in C first.  Thus, we need to initialize
; C to the port BEFORE the first one we intend to write.
;
	ld	bc,tfissz*256+iRgSCnt-1	;set up count, port-1
	ld	hl,sctrcnt	;HL->sector count
	ld	d,(hl)		;in D for loop
initlp:
	inc	c		;bump to register
	outi			;output byte, dec count, inc ptr
	jr	nz,initlp
;
	ld	c,iRgDat	;data register (B=0)
	ld	hl,(hstbuf)	;get xfr adr in HL
;
iWtDrq:
	in	a,(iRgStat)	;get status
	bit	7,a
	jr	nz,iWtDrq	;brif still busy
	bit	0,a
	jr	z,iWtDrq1	;brif no error
	in	a,(iRgErr)	;read/clear error
	ld	(dskerr),a	;save it
	ld	a,(hstdsk)	;get disk/head
	jp	cold	;and start over
iWtDrq1:
	and	8	;check DRQ bit
	jr	z,iWtDrq	;loop if not ready yet
;
; no error, data ready, read it
	inir
	inir	;read 512 bytes
;
	dec	d
	jr	nz,iWtDrq	;for D sectors
;
;data read, update xfr adr, trk/hd/sec, count
	ld	(hstbuf),hl	;save nxt xfr adr
	ld	hl,(remsecs)	;get remaining sector count
	ld	a,h
	or	l	;more sectors?
;
	if	debug
	jr	nz,bumphd	;brif more sectors (debug mode)
	rst	38h	;else back to debugger
	else
	jp	z,boot	;if 0, all done.	Boot.
	endif
;
	if	debug
bumphd:
	endif
;
;else more sectors to read, move to next sec/head/cylinder
	in	a,(iRgSNum)	;get current sector
	ld	c,a		;save in C
	inc	c		;bump to next
	ld	a,(hstspt)	;get sectors/track
	cp	c		;spt-nxtsec
	ld	a,c		;nxt sec in A
	jr	nc,foo3		;Cy=0 nxt sec <= spt
	ld	a,1		;Cy=1 nxt sec > spt
foo3:	ld	(hstsec),a	;set new starting sec #
	jr	nc,bumptrk	;trk ovrflw, nxt hd or nxt cyl?
	ld	a,(hsthds)	;get number of heads
	cp	1		;only 1?
	jr	z,bumptrk	;if only 1 head, don't change it
;
	ld	e,a	;else save # of heads in E
	in	a,(iRgSDH)	;get current unit/head
	ld	c,a	;save in C
	and	0f0h	;mask unit
	ld	b,a	;unit in B
	ld	a,c	;get unit/head back
	and	0fh	;mask head
	inc	a	;bump to next
	cp	e	;compare with # of heads
	jr	nc,bmphd0	;brif head out of range
	or	b	;else make SDH byte
	ld	(hstdsk),a	;save it
	jp	rdtrks	;and go again
bmphd0:
	ld	a,b	;unit in A (head=0)
	ld	(hstdsk),a	;and save it
bumptrk:
	ld	hl,(remspc)	;more secs on current cyl?
	ld	a,l
	or	h
	jp	nz,rdtrks	;yes, go read them
;
	ld	hl,(hsttrk)
	inc	hl
	ld	(hsttrk),hl	;bump to next cylinder
	ld	hl,hstsec	;reset sector to 1
	ld	(hl),1
	ld	hl,(hstspc)	;for next calculation
	jp	rdtrks	;and go again
;
;pad so initialized data falls at end of bootcode space
	codlen	defl	$ - base	;length of code
	datlen	equ	4	;# of bytes of data below

	defs	(btcdmxz - datlen) - codlen

;
;***************************************************************
;initialized data
sysbase:
	defw	ldadr	;where to start loading
;
	defw	nhsecs	;system length in host sectors

; must fit in given size

	if	($ - base) gt btcdmxz
	.printx "* * *	BOOTCODE TOO LARGE!!!	* * *"
	endif
;
;
;	org	base+0170h
;
;Signature string for ROM to know it loaded a valid boot sector:
sigstr:
	defb	'CPM'
;
;6 reserved bytes
rsrvd:
	defs	6	;	0,0,0,0,0,0
;
;Host disk parameters, # cylinders, # heads, # sec/trk
hstcyl:	defw	981
hsthds:	defb	1	;really 5, but the current bios
			; only uses 1 head/"disk"
hstspt:	defb	17
;
;CP/M Boot Partition
btpart:	defb	2	;patch this to the preferred boot partition

;
;CP/M Partition table. 16 16-bit entries of StartCyl,NumCyl
partTbl:
	defw	0	;old-style partition all on cylinder 0
	defw	981
;
	defw	0
	defw	981
;
	defw	0
	defw	981
;
	defw	0
	defw	981
;
	defw	0
	defw	981
;
;the other partitions
	defs	4*11	;be sure to 0-fill before loading.
;
;
;DOS-compatible primary partition entries
;
dosprt0:
	defs	16
dosprt1:
	defs	16
dosprt2:
	defs	16
dosprt3:
	defs	16
;
;FreeDOS boot signature
		defw	0aa55h
dprtsz	equ	$-dosprt0
;
;
;The DOS partition table is only valid on the disk, so overlay it
;with the Udata area in RAM.
;
	org	dosprt0
;
dskerr:	defs	1	;iWtDrq saves error register here
hstbuf:	defs	2	;xfr adr
remsecs: defs	2	;remaining # of sectors to load
hstspc:	defs	2	;# host sectors/cylinder
remspc:	defs	2	;remaining sectors/cylinder
;
; Task File init string.
; HL->sctrcnt, B=tfissz (byte count) C=Sector Count register
; OUTI does (C)<-(HL++), B--.
; If B <> 0, C is incremented to next register port and the next
; byte is written.
;
; * * * DO NOT CHANGE THE ORDER OF THE FOLLOWING LABELS * * *
;
sctrcnt: defs	1	;count of sectors to read
hstsec:	defs	1	;sector number to read
hsttrk:	defs	2	;host track (cylinder)
hstdsk:	defs	1	;host disk (SDH byte)
cmdbyt:	defs	1	;put command byte here for init loop
tfissz	equ	$-sctrcnt
;
	if	($ - dosprt0) gt dprtsz
	.printx "* * * Udata area too large! * * *"
	endif
;
	if	($ - base) gt secsiz
	.printx "* * *	BOOT SECTOR TOO LARGE!!!	* * *"
	endif
;
	end
;
