;
; 17 Sep 2026
;
;Grafting labels and comments from CCBIOS.ASM 18 Sep 2026
;
; Reconstructed FD-4000 v1.3 CBIOS
;
; Disassembly and commenting by John D. Baker begun January 1996
;
	title	'Davidge DSB-4/6 deblocked BIOS for CP/M 2.2'
	page	82
;
vers	equ	22	;version 2.2
;
false	equ	0
true	equ	not false
;
	maclib	z80
	maclib	portdef
	maclib	flopdef
;
;additional floppy disk control/status bits not defined
;in "FLOPDEF.LIB" (with thanks to Martin F. Eberhard):
;
;WD1793 status (DSTAT) after a command
;
SBUSY	equ	00000001b	;(All) busy
SCRCERR	equ	00001000b	;(All) CRC error
SWRPROT	equ	01000000b	;(All) write protect
SNOTRDY	equ	10000000b	;(All) drive not ready
SINDEX	equ	00000010b	;(I) index
STRACK0	equ	00000100b	;(I) track 0
SSEEKER	equ	00010000b	;(I) seek error
SHEADLD	equ	00100000b	;(I) head loaded
SDATRQ	equ	00000010b	;(II & III) data request
SLOSTD	equ	00000100b	;(II & III) lost data error
SRNFER	equ	00010000b	;(II & III) record not found
SRCTYF	equ	00100000b	;(II & III) record type fault
SWFALT	equ	00100000b	;(II & III) Write fault
;
;WD1793 command options (1st letter matches WD1793 data sheet)
;
RS03MS	equ	00000000b	;(I) 3 mS step rate
RS06MS	equ	00000001b	;(I) 6 mS step rate
RS10MS	equ	00000010b	;(I) 10 mS step rate
RS15MS	equ	00000011b	;(I) 15 mS step rate
VERTRK	equ	00000100b	;(I) verify on destination track
HDLOAD	equ	00001000b	;(I) head load
UPDTTR	equ	00010000b	;(I) update track reg
F1SCMP	equ	00000010b	;(II)enable side compare
F2SCS1	equ	00001000b	;(II)side compare, side 1
MULTCM	equ	00010000b	;(II) read/write multiple
ED15MS	equ	00000100b	;(II & III) settling delay
;
; disk status register 1 bits
ds1hdld	equ	HLMSK		;heads are loaded
ds1ds3	equ	00010000b	;ds3 in dctrl1
ds1ds2	equ	00001000b	;ds2 in dctrl1
ds1ds1	equ	00000100b	;ds1 in dctrl1
ds1ds0	equ	00000010b	;ds0 in dctrl1
ds1fdci	equ	00000001b	;FDC intrq line
;
; disk status register 2 bits
;ds2sid	equ	01000000b	;2-sided line from drive
ds2sngl	equ	01000000b	;disk is single-sided
ds2dbl	equ	00000000b	;disk is double-sided
;
ds2den	equ	00100000b	;dc1den bit in dctrl1
ds2dd	equ	00100000b	;double-density
ds2sd	equ	00000000b	;single-density
;
ds2sdsl	equ	00001000b	;-dctrl1 side-select
ds2s0sl	equ	00001000b	;side 0 selected
ds2s1sl	equ	00000000b	;side 1 selected
;
ds2wp	equ	00000100b	;write protect line from drive
ds2rw	equ	00000100b	;disk is writable
ds2ro	equ	00000000b	;disk is read-only
;
ds2typ	equ	00000001b	;drive type when on trk00
ds2t08	equ	00000001b	;8-in (i.e. 500kbps)
ds2t05	equ	00000000b	;mini (i.e. 250kbps)
;
sdmsk	equ	not dc1den and 00ffh	;set SD
s0msk	equ	not dc1sid and 00ffh	;set side 0
t0msk	equ	sdmsk and s0msk	;both for tk0 ops
;
; Composite FDC status masks used in this program:
;
typ1err	equ	snotrdy+sseeker+scrcerr	;(sseekerr=srnfer)
rwerr	equ	typ1err+swrprot+swfalt+slostd
;
$*macro
;
jdbfix	equ	false	;BIOS fixes enabled?
dridblk	equ	false	;HOME routine like DEBLOCK.ASM
;
test	equ	true	;true to replicate fixed-size system
;
msize	equ	64	;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	;make 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
cdisk	equ	0004h	;current disk number 0=a,...,15=p
iobyte	equ	0003h	;intel i/o byte
;
cibuff	equ	ccp+7	;ccp command input buffer
;
bdosv	equ	0005
wbootv	equ	0000	;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
defdma	equ	tbuff	;also default dma address
;
;************************************************
;*					  	*
;*	    CP/M to host disk constants		*
;*						*
;************************************************
cpmsiz	equ	128	;CP/M sector size
hstsiz	equ	1024	;host sector size (max)
cpmhst	equ	hstsiz/cpmsiz	;CP/M sectors per host sector
nsecs	equ	(bios-ccp)/cpmsiz  ;nr of sectors for warm boot
nhsecs	set	nsecs/cpmhst	;nr of host sectors
	if	(nsecs mod cpmhst) GT 0
nhsecs	set	nhsecs+1
	endif
wbtdrv	equ	'A'	;warmboot from this drive
;
;************************************************
;*						*
;*	BDOS constants on entry to write	*
;*						*
;************************************************
wrall	equ	0	;write to allocated
wrdir	equ	1	;write to directory
wrual	equ	2	;write to unallocated
;
;
ctlc	equ	3	;control-c
bel	equ	7	;control-g, bel
cr	equ	13	;<cr>
lf	equ	10	;<lf>
xoff	equ	19	;control-s, xoff
esc	equ	1bh	;escape
;
	org	bios
;
	jmp	boot
wboote: jmp	wboot
cstat:	jmp	const
conin:	jmp	coni
cono:	jmp	conout
	jmp	list
	jmp	punch
	jmp	reader
	jmp	home
	jmp	seldsk
	jmp	settrk
	jmp	setsec
	jmp	setdma
	jmp	read
	jmp	write
	jmp	listst
	jmp	sectran
;
maxdsk:	db	3	;max disk number (0-3)
;
stprt8:	db	rs03ms	;step-rate bits for 8-inch
stprt5:	db	rs15ms	;step-rate bits for 5.25"/3.5"
;
;disk delay values (msw in HL for delay loop)
hldly8:	dw	50	;8-in headload delay default 50ms
hldly5:	dw	50	;mini-disk headload delay default 50ms
hsdly:	dw	15	;Head-settling delay default 15ms
mtrdly:	dw	1000	;Motor-On delay default 1000ms
;
;output status masks for the 4 serial ports
;b7 set if using xoff/any handshaking
sioasm:	db	txmty
siobsm:	db	txmty
siocsm:	db	txmty
siodsm:	db	txmty
;
; CPU speed scale factor (in B for delay loop)
cpumhz	equ	4
cpuspd:	db	cpumhz * 40	;CPU scale factor, A0=160 for 4MHz Z80
;
; status bits/mask for parallel port
;
lptsm:	db	pbusy
lptsx:	db	0	;status never flipped?
;
;mini-disk (5.25", 3.5") DPBs, double-sided, 80-track
dp5s0:	dw	36	;spt	DSSD, 18 128-byte sec/trk
	db	4	;shf	16 sec/blk, 2k blocks
	db	15	;blm
	db	1	;exm	2-byte extent pointers?
	dw	172	;dsm	173*2k=346kb
	dw	63	;drm	64 directory entries
	db	128	;al0	1 block of directory	
	db	0	;al1
	dw	16	;cks	login vector size
	dw	3	;off	3 reserved trks
;
dp5s1:	dw	40	;spt	DSSD, 10 256-byte sec/trk
	db	4	;shf	or 5 512-byte sec/trk
	db	15	;blm
	db	1	;exm
	dw	191	;dsm	192*2k=384k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	3	;off
;
dp5s3:	dw	32	;spt	DSSD, 2 1024byte sec/trk
	db	4	;shf
	db	15	;blm
	db	1	;exm
	dw	153	;dsm	154*2k=308k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	3	;off
;
dp5d0:	dw	58	;spt	DSDD, 29 128-byte sec/trk
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	278	;dsm	279*2k=558k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	3	;off
;
dp5d1:	dw	72	;spt	DSDD, 18 256-byte sec/trk
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	345	;dsm	346*2k=692k
	dw	127	;drm	128 directory entries
	db	192	;al0	2 blocks of directory
	db	0	;al1
	dw	32	;cks
	dw	3	;off
;
dp5d2:	dw	80	;spt	DSDD, 10 512-byte
	db	4	;shf	or 5 1024-byte sec/trk
	db	15	;blm
	db	0	;exm
	dw	384	;dsm	385*2k=770k
	dw	127	;drm
	db	192	;al0
	db	0	;al1
	dw	32	;cks
	dw	3	;off
;
tries:	db	10	;10 retries on disk operations
;
wmbtgo:	dw	ccp+3	;re-start CP/M after wboot
;
;Parameter table for 4 floppy drives.  Cleared to all 0
;on cold and warm boot.
;	stprt	selbits	ssz	sector size code 0=128-byte,
prmtbl:	db	00, 01, 0ffh	;1=256-byte, 2=512-byte, 3=1kbyte
	db	00, 02, 0ffh	;ff=invalid/not initialized(?)
	db	00, 04, 0ffh
	db	00, 08, 0ffh
prmssz	equ	prmtbl+2
;
	ds	8 - $ mod 8	;pad to 8-byte boundary (2 bytes)
	;(shenanigans?)
ctc0v:
dmavec:
	dw	dmacisr	;ctc0 should never interrupt, so use for dma
	dw	0000	;bios+0b2h available for shenanigans?
	dw	0000	;bios+0b4h available for shenanigans?
ctc3v:
fdcvec:
	dw	fdcisr	;fdc interrupt via ctc3
;
;Individual subroutines to perform each function
;
;
retry:	dcr	c	;--retries
	jrnz	load1	;brif more tries
	lxi	h,botmsg  ;else get bad boot msg
	call	prtrd	;print, get char, and retry
			;if not ctrl-c or esc
wboot:	lxi	sp,tbuff  ;set up stack
	lda	tries	;get # retries
	mov	c,a	;save in c reg
	mvi	a,ctc0v mod 256  ;get ctc0-3 ivl byte
	out	ctc0	;write vector to ctc0
	mvi	a,ctc0v shr 8	;get ivh byte
	stai		;set it up
load1:	xra	a	;0 in A
	mvi	b,12	;4 disks * 3 bytes/disk
	lxi	h,prmtbl  ;HL->disk paramter table
load1a:	mov	m,a	;zero it out
	inx	h
	djnz	load1a	;B=0 after last iteration
;
	if	jdbfix
;
	mov	h,b	;so use it to initialize HL
	mov	l,h	;hl = (drv 'A', side 0)
	shld	hstdsk	;=l(00), hsthd=h(00)
	mvi	l,2	;ccp starts at trk0, sctr 2
	shld	hstsec	;hstsec=l(02), hsttrk=h(00)
	push	b	;save C (=# retries), B=0
	mov	c,b	;select disk 0
	mov	e,b	;set new unit indicator
;
	else
;
	lxi	h,0002h	;ccp starts at trk0, sctr 2
	shld	hstsec	;hstsec=l(02), hsttrk=h(00)
	lxi	h,0000h	;hl = 0000 (drv 'A', side 0)
	shld	hstdsk	;=l(00), hsthd=h(00)
	push	b	;save C (# retries)
	mov	c,l	;select disk 0
	mov	e,l	;set new unit indicator
;
	endif
;
	call	seldsk	;on return, hl->dph[c=dsk]
	mov	e,m	;&xltn (s/t,skf) in de(->s/t)
	inx	h
	mov	d,m
	push	d	;copy de.
;
	if	jdbfix
;
	popix		;IX->SPT for trk>0 reads
;
	else
;
	pop	h	;into hl (BUGBUG: should be popix)
;
	endif
;
	pop	b	;restore C (=# retries)
	lxi	h,ccp	;hl=xfr start adr (ccp)
	shld	hstbuf	;save for direct fdc read
	mvi	b,25	;if 8" sssd, 25 secs left on trk00
	in	dstat2	;read dstat2 reg
	ani	ds2min	;was it a mini disk?
	jrz	load2	;brif not mini (was 8-in)
	mvi	b,17	;if mini sssd, 17 secs left on trk00
load2:	push	b	;trk00 count, retries
	call	dread
	pop	b	;get trk00 count, retries
	ora	a	;set flagz
	jrnz	retry	;brif error
	sded	hstbuf	;save next xfer addr
	lxi	h,hstsec  ;point at hstsec
	djnz	load3	;dec sector count, brif more sectors
	mov	m,b	;no more on trk00, hstsec=0
	inx	h	;point to lsb of hsttrk
	inr	m	;hsttrk=0001h (bump track)
	dcx	h	;back to hstsec
	ldx	b,0	;b<-(ix+d) ix=?? (BUGBUG: see previous)
load3:	inr	m	;++hstsec
	lda	prmssz	;get ssz[0]
	add	d	;add page of next xfer addr
	sui	bios shr 8  ;>=start pg of bios?
	jrc	load2	;no, more to load
;
;End of load operation, set parameters and go to CP/M
gocpm:	lhld	wmbtgo	;load done, restart CP/M here
gocpm1:	push	h	;set up return address
	mvi	a,jmp	;set up a jp nnnn
	sta	wbootv	;at warm-start address
	sta	bdosv	;and at bdos zp entry
	lxi	h,wboote  ;get wboot:
	shld	wbootv+1  ;and save at warm-start
	lxi	h,bdose	;get bdos entry point
	shld	bdosv+1	;and put in zp address
	lxi	h,dbuf	;addr of host buffer
	shld	hstbuf	;for {rd,wr}hst
	shld	dmaad	;put in dma port a address
	lxi	h,tbuff	;get address of tbuff
	shld	dmaadr
	ei		;ok to turn ints back on
	xra	a	;0 in a
	sta	hstact	;mark dbuf inactive
	lda	cdisk	;get current du:
	mov	c,a	;in c
	ret		;and back to CP/M
;
;
;Select disk given by register C
seldsk:	lxi	h,0000h	;assume select error
	lda	maxdsk	;get max disk #
	cmp	c	;compare with requested disk
	rc		;return if c > max disk
	mov	a,c	;get disk # in a
	sta	sekdsk	;save it
;
	sspd	oldstk	;save the stack pointer
	lxi	sp,biosstk  ;set up local stack
;
; set up host parameters of selected disk
	push	b	;save disk #, etc.
	mov	l,c	;get disk # in l (h = 0)
	dad	h	;*2
	dad	h	;*4
	dad	h	;*8
	dad	h	;*16 to make offset
	lxi	b,dphbase
	dad	b	;hl->dph[c].xlt
	push	h	;save it
	call	fdsbs	;get sekdsk selbits in A, ssz+1 in B
	sta	seksel	;selbits for sekdsk
	bit	0,e	;test the new unit bit
	jrnz	sldk3	;brif not new unit (unwind stk & return)
;
	lhld	lunit	;new unit, get [lc]unit
	push	h	;save current disk assignments
	mov	l,h	;make sure they are different
	inr	l	;to force IDRD, etc.
	shld	lunit	;store again LUNIT=CUNIT+1
	lhld	hstdsk	;l=hstdsk, h=hsthd
	push	h	;save it
	lhld	sekdsk	;l=sekdsk, h=sekhd
	mvi	h,00h	;L=diskno, H=sid/hd 0
	shld	hstdsk	;save at hstdsk, hsthd=0
	call	idrd	;set dsk typ,dns,ssz
	jrnz	sldk2	;brif not zero
	sta	seksel
	mov	c,a	;selbits in c
	lxi	h,mseltbl-4  ;hl->5.25" xlt/dpb table
	bit	4,c	;mini?
	jrnz	sldk0	;brif mini
	lxi	h,seltbl-4  ;8-in, hl-> 8"SS xlt/dpb table
	in	dstat2	;read dstat2
	ani	ds2sid	;2sided?
	jrnz	sldk0	;brif ss
	lxi	h,seltbla-4  ;ds,hl-> 8"DS xlt/dpb table
sldk0:	lxi	d,0004h	;offset into table
	bit	6,c	;chk density 1=DD
	jrz	sldk1	;brif SD
	dad	d	;hl+=4
	dad	d	;+=8
	dad	d	;+=12
	dad	d	;+=16, skip to DD part of table
sldk1:	dad	d	;+4
	djnz	sldk1	;bump to ptr pair for sector size
	xchg		;DE->xlt/dpb pair for dsk in slctd drv
	cmp	a	;set z, clear cy
sldk2:	pop	h	;restore hstdsk,hsthd
	shld	hstdsk
	pop	h	;restore old [lc]unit
	shld	lunit
	lxi	h,0000h	;0 in hl to indicate error
	xthl		;hl->dph[c].xlt, (sp)=0000
	jrnz	sldk3	;brif disk ID routine returned nz
	pop	b	;bc=0
	push	h	;&dph[c].xlt on stack
	xchg		;de->dph[c].xlt, hl->xlt/dpb pair
	ldi		;(hl++)->(de++) set up dph[c].xlt
	ldi		;xlt addr into dph[c].xlt
	push	h	;save ptr to dpb for siz/sid/den/spt
	lxi	h,0008h	;offset of dpb pointer in dph[c]
	dad	d	;hl->dph[c].dpb
	xchg		;back in de
	pop	h	;restore dpb ptr
	ldi		;install dpb addr in dph[c].dpb
	ldi
sldk3:	pop	h	;hl->dph[c] or =0000
	pop	b	;get disk back
	lspd	oldstk	;restore stack pointer
	mov	a,c	;disk in a
	ret
;
;
;move to the track 00 position of current drive
;
home:
	if	jdbfix	;be more like PCPI CBIOS
	if	dridblk	;be more like DEBLOCK.ASM
	lxi	b,hstwrt  ;BC->hstwrt
	else
	lxi	b,hstact  ;BC->hstact
	ldax	b	;hstact in A
	ora	a	;host buffer active?
	jrz	homed	;brif not active
	inx	b	;bump to hstwrt
	endif
	ldax	b	;hstwrt in A
	ora	a	;check for pending write
	jrnz	homed	;brif pending write
	dcx	b	;back to hstact
	stax	b	;else mark host inactive
homed:
	endif
	lxi	b,0	;trk00 in bc
			;fall through to settrk
;
;set track given by register-pair BC
;
settrk:	sbcd	sektrk	;save at sektrk
	ret
;
;
;sector passed in bc has been translated by sectran...
;
setsec:	lxi	h,sekhd	;hl->side flag
	mov	a,c	;sector # in a
	bit	7,a	;test a:7 (side flag)
	mvi	m,side0	;assume side 0
	jrz	stsc0	;brif a:7==0 (side 0)
	mvi	m,side1	;else set side 1
stsc0:	ani	7fh	;strip off side bit
	dcr	a	;make 0-rel.
	sta	seksec
	ret
;
;Translate the sector given by BC using the
;translate table given by DE
sectran:
	push	d	;save xlt ptr
	call	fdsbs	;selbits in A, ssz+1 in B
	pop	h	;xlt ptr in hl
	push	b	;save ssz+1, sector
	mov	a,c	;lsb sector in a
	sta	cpmsec	;save it
	ral		;find 0-rel. phys sec (a:7->cy)
sect0:	rar		;sec/=2
	ora	a	;clear carry for next rotate
	djnz	sect0	;for b iterations (CP/M sec->physec)
	mov	e,b	;0 in e (counts sec==s/t occurances)
	cmp	m	;hstsec-s/t
	jrc	sect1	;brif sector on side 0 (sec < hst)
	mvi	b,side1	;sector >= s/t, side 1 flg in B)
	sub	m	;a -= (hl) normalize sector #
sect1:	push	b	;save bc (side flag, cpmsector)
	mov	d,m	;get s/t in d
	inx	h	;bump to skf
	mov	c,m	;get skf in c
	mov	b,a	;physec in b
	inr	b	;b++ for loop control
	xra	a	;0 in a (0-rel. physec)
	sub	c	;a-=skf (start one skf back)
sect2:	add	c	;add skf
	cmp	d	;physec - s/t
	jrc	sect3	;brif physec < s/t
	sub	d	;else physec >= s/t, reset
	jrnz	sect3	;brif physec > s/t
	inr	e	;physec == s/t, bump e
sect3:	djnz	sect2	;repeat physec times
	add	e	;add # of times physec == s/t
	mov	h,b	;0 in h
	mov	l,a	;0-rel. xltd sector in l
	pop	b	;restore side flag, cpmsector
	pop	d	;restore ssz+1, cpmsector
	mov	e,a	;0-rel. xltd sector in e
	call	sect4	;mk sec msk in A, CP/M sector in HL
	ana	c	;record offset in phys sector
	ora	l	;make sector number
	inr	a	;make 1-rel.
	ora	b	;or in side flag
	mov	l,a	;hl=xltd sector
	ret
sect4:	xra	a	;0 in a (sec mask for 128-byte secs)
sect5:	dcr	d	;decr ssz(+1)
	rz		;return if done (128-byte host sectors)
	stc		;else cy=1
	adc	a	;a=2a+1 compute hst sector mask (1,3,7)
	dad	h	;hl*=2 compute xltd CP/M sector mod ssz
	jr	sect5	;until done
;
;
;Set DMA address given by register-pair BC
setdma:
	sbcd	dmaadr	;save dma address here
	ret
;
;Logical to physical sector translation routine
physec:	call	fdsbs	;get selbits in A, ssz+1 in B
	lda	sektrk
	ora	a	;set flagz
	jrnz	physc0	;brif not trk 00
	inr	a	;was zero, now 1
	mov	b,a	;trk==0, assume 128-byte sectors
physc0:	mov	d,b	;save ssz+1 in d
	lda	seksec	;0-rel. xltd CP/M sector
	ral		;preserve a:7 in cy, prep for shift
physc1:	rar		;a/=2
	ora	a	;clear cy for next rar
	djnz	physc1	;do b times (make sekhst)
	inr	a	;starting at 1
	sta	sekhst
	lxi	h,sekdsk  ;hl points to sek*
	mvi	c,seklen  ;# of bytes to move/compare (B==0)
	ret
;
;Compare the units
comp:	push	d	;save pointer to hst*/una*
	call	physec  ;get pointer to sek*
	pop	d	;recover pointer to hst*/una*
comp1:	ldax	d	;a<-(de)
	cci		;a - (hl++)
	rnz		;return if no match
	rpo		;return if all bytes compared
	inx	d	;else bump to next
	jr	comp1	;and go again
;
; find address of disk parameter table
;
dpfnd:	lhld	sekdsk	;get sekdsk
	mvi	h,0	;only need l
	dad	h	;*2
	dad	h	;*4
	dad	h	;*8
	dad	h	;*16
	lxi	d,dphdpb  ;de->dph.dpb addr
	dad	d	;HL->DBH[sekdsk].DPB
	mov	e,m	;get addr of this disk's dpb
	inx	h
	mov	d,m	;in de
	ret
;
;********************************************************
;*							*
;*	sector deblocking algorithms for CP/M 2.0	*
;*							*
;********************************************************
;
write:	;write the selected CP/M sector
	xra	a	;0 in a
	sta	readop	;not a read operation
	mov	a,c	;write type in a
	sta	wrtype	;save at wrtype
	cpi	wrual	;was it wrual?
	jrnz	chkuna	;brif wrall, wrdir
;
; write to unallocated, set parameters
	call	physec  ;hl->sek*, bc=count
	lxi	d,unadsk  ;de->una*
	ldir		;copy sek* to una*
	call	dpfnd	;DE=DPH[sekdsk].DPB
	inx	d
	inx	d
	inx	d	;point to blm
	ldax	d
	inr	a	;make sec/blk
	sta	unacnt	;save at unacnt
;
chkuna:			;check for write to unallocated sector
	lxi	h,unacnt  ;point at unacnt
	xra	a	;0 in a
	cmp	m	;any unalloc remain
	jrz	alloc1	;alloc1 skip if not
;
; more unallocated records remain
	dcr	m	;--unacnt
	lxi	d,unadsk
	call	comp
	jrnz	alloc0	;brif not same
	call	dpfnd	;DE=DPH[sekdsk].DPB (DE->spt[dsk])
	xchg		;hl->spt[dsk], de=dph[dsk].dpb+1
	lda	cpmsec	;get cpmsec
	inr	a	;bump to next
	cmp	m	;ran off end of track?
	jrnc	ovrflw	;brif so [a>=(hl)]
;
;no overflow, translate next sector
	lxi	h,-11
	dad	d	;HL=DE-11 (back up to xltn)
	mov	e,m	;get xltn (s/t,skf entry) in de
	inx	h
	mov	d,m
	mov	c,a	;sector in c
	call	sectran	;0-rel. xltd sec in E (L=xltd,mskd,w/sid)
	mvi	l,side0	;l=side 0
	jp	noovf0	;jmp if a:7=0 (side 0)
	mvi	l,side1	;else a:7=1 (side 1), l=side 1 flag
noovf0:
	mov	h,e	;h<-e (sector in h)
	inr	h	;bump to next
	jr	nxtsec	;go set it
ovrflw:
	lxi	h,unatrk  ;point at unatrk
	inr	m	;next track
	lxi	h,0100h	;l=side 0, h=sector 1(?)
nxtsec:
	shld	unasec	;set next unalloc sector
	xra	a	;0 in a indicate read unnecessary
	jr	noovf1
;
;
read:	mvi	a,wrual	;treat as wrual
	sta	wrtype
	sta	readop	;indicate a read operation
alloc0:
	xra	a	;clear unacnt
	sta	unacnt
alloc1:
	inr	a	;1 in a, must read
noovf1:
	sta	rsflag
	sspd	oldstk	;save stack pointer (oldstack?)
	lxi	sp,biosstk  ;set up local stack
;rwoper:
	xra	a	;0 in a
	sta	erflag	;no errors (yet)
	lxi	h,hstact  ;point at hstact
	ora	m	;was it active?
	mvi	m,1	;it is now
	jrz	filhst	;brif was not active (filhst)
	lxi	d,hstdsk  ;dbuf active, d->hst*
	call	comp	;compare w/sek*
	jrz	match	;brif match
	lda	hstwrt	;else need to pre-read get hstwrt
	ora	a	;buffer needs writing?
	cnz	dwrite	;dbuf active, flush pend. wr
filhst:
	call	physec  ;hl->sek*, bc=len
	lxi	d,hstdsk  ;point de at hst*
	ldir		;copy
	lda	seksel	;get sekdisk selbits (seksel)
	sta	cunit
	lda	rsflag
	ora	a	;need to read?
	cnz	dread
	xra	a
	sta	hstwrt	;no pending write
match:
	call	physec  ;hl->sek*, bc=len
	call	sect4	;make host sector mask in a
	lhld	seksec	;xltd 0-rel cpm sector in l
	ana	l	;mask a w/l (sector index into dbuf)
	lxi	b,cpmsiz  ;CP/M sec size in bc
	lxi	h,dbuf	;hl->dbuf
	rar		;A/=2, A:0->Cy
	mov	d,a	;msb sector offset in d (which part of hstsec)
	mov	e,b	;0 in e
	rarr	e	;E/=2 (Cy->E:7) (lower,0 or upper,80h rec)
	dad	d	;hl points to CP/M sector
	lded	dmaadr
	lda	readop
	ora	a	;read or write?
	jrnz	rwmove	;brif reading
	xchg		;else writing, swap de<->hl
	inr	a	;1 in a
	sta	hstwrt	;indicate pending write
rwmove:
	ldir		;move the data
	lda	wrtype
	dcr	a	;becomes 0 if wrdir, non-0 otherwise
	lda	erflag	;return erflag
	lspd	oldstk	;restore stack pointer
	rnz		;return if read/wrall/wrual
	ora	a	;wrdir, check error
	rnz		;return on error
	sta	hstwrt	;no error, indicate no pend. wr.
			;and fall through to dwrite
;
;
; The following routines do the primitive disk accesses.
;       In all cases, one sector of data is transferred.
;       If the disk has not been previously accessed,
;       these routines will automatically determine the
;       disk type (8" or 5"), single or double density,
;       and sector size.
;
;       Before the desired data is transferred, the desired
;       track is seeked out, the desired sector and side is
;       set, then the actual data transfer.
;
;       Up to ten tries will be attempted before the data
;       transfer is aborted.  On return to the calling
;       routine, the A register will contain a zero if the
;       operation was successful, or non-zero if not
;       successful.  The flag register will not necessarily
;       correspond with the A register content.
;
;
dwrite:	db	3eh	;sim. "mvi a,n" instr
dread:	xra	a
	ani	wrbit	;A=00 or A=0AFh
	sta	rwflg	;rd=0, wr=20h
	lda	tries	;get # of tries
	mov	b,a	;in b
	mvi	c,0	;C=fdc err msk (none yet)
agn:	push	b	;save tries, err mask
	ana	c	;mask errors
	cnz	drstor	;if rnf error, restore
	call	seek	;then do this
	cz	rdwr	;do the read/write
	pop	b	;get tries back
	rz		;return if done (no error?)
	mvi	c,dstrnf  ;RNF mask in C
	djnz	agn	;try again
	lxi	h,erflag  ;point at erflag
	ora	m	;or in error flag
	mov	m,a	;and save it
	ret		;and done
;
rdwr:	call	setup	;start drive, A=trk, HL->selbits
	jrz	rdwr1	;brif trk00, A=00, ssz=128byt/sec
	inx	h	;HL->ssz
	mov	a,m	;get in a
rdwr1:	mov	b,a	;ssz in b
	lxi	h,0040h	;step (64) in hl
	inr	b	;++b for loop control
rdwr2:	dad	h	;h*=2^b (make xfr size)
	djnz	rdwr2
	xchg		;DE=xfr size
	lda	hstsec	;get hstsec
	out	dsctr	;write to fdc dsctr
	mvi	c,ddata	;fdc ddata port in c
	lda	rwflg	;0=rd, 20=wr
	ori	rdsec	;make fdc rdsec or wrsec cmd
	bit	5,a	;test for write
	lhld	hstbuf	;get xfr adr
;
;operate the fdc w/dmac.  hl=xfer address, de=size
;c=i/o port to rd/wr, a=fdc command
;
fdcdma:	shld	dmaad	;store xfr adr in dmac string
	dad	d	;make next xfr adr
	push	h	;save it
	push	psw	;save fdc cmd, flags
	mov	a,c	;fdc data port in a
	sta	dmaprt	;put in dmac string
	dcx	d	;make dma xfr len
	sded	dmalen	;put in dmac string
	lxi	h,dmadir  ;point to xfr dir
	mvi	m,01h	;assume read, xfr b->a
	pop	psw	;get fdc cmd, flags back
	jrz	fddmard	;brif read
	mvi	m,05h	;else write, xfr a->b
fddmard:
	lxi	h,flopdma  ;point at dma string
	lxi	b,dmastln shl 8 + dmacmd
			;^^string length in b, dmacmd in c
	outir		;set up the dmac
	pop	d	;get next transfer back
	outi		;write one more byte to start dma
	jr	fdcop	;now start fdc
;
;
dseek:	mvi	b,sekcmd  ;b=seek w/hdld, w/o vfy
dseek1:	out	ddata	;a=tgt trk->fdc data port
	ora	a	;track 0?
	jrz	tmpsd	;brif seeking track 0
dseek2:	mvi	c,s0msk	;mask selbits for side 0
	jr	type1
drstor:	mvi	b,restor  ;b=restore w/hdld, w/o vfy
tmpsd:	mvi	c,t0msk	;mask selbits for side 0, SD
type1:	push	h	;save hl
	push	b	;save bc (cmd, dctrl1 mask)
	call	fdsbh	;get a=selbits, b=ssz+1 for hstdsk
	pop	b	;restore cmd, dctrl1 mask
	ana	c	;mask select byte
	out	dctrl1	;select drive
	dcx	h	;point to step rate
	mov	a,m	;get step rate in a
	pop	h	;restore hl
	ora	b	;or in b to make command
;
fdcop:	push	h	;save hl
	out	dcmmd	;write to fdc dcmmd port
	sta	cmnd	;save fdc command here
	lxi	h,0ea60h  ;set up countdown, msw
fdcop0:	mvi	b,1eh	;countdown, lsb
fdcop1:	djnz	fdcop1	;spin here B times
	in	dstat1	;read dstat1
	rar		;bit 0 to cy (fdc intrq line)
	jrc	fdcop2	;brif fdc intrq active
	dcx	h	;else count down
	mov	a,h
	ora	l	;see if count exhausted
	jrnz	fdcop0	;brif count not exhausted.
	mvi	a,WDFI	;timed out, fdc force int: term w/o int
	out	dcmmd	;terminate cmd in progress w/o intrq
	mvi	a,0c3h	;dmac reset command
	out	dmacmd	;reset dmac
	exx		;swap register sets
	lxi	h,dsktmo  ;disk timeout message
	call	fdcoperr  ;print it
	lda	cunit	;get cunit
	out	dctrl1	;select drive
fdcop2:	pop	h	;restore HL (done with it?)
	in	dstat	;read fdc dstat port
	sta	status	;save fdc status
	ani	rwerr	;0fch	;mask errors
	rp		;rif a:7=0 (wasn't ntrdy)
	exx		;else swap register sets
	lda	hstdsk
	adi	'A'	;add 65 (make 0-3 -> 'A'-'D')
	sta	dnrdsk	;store in message
	lxi	h,dnrmsg  ;drive not ready msg
fdcoperr:
	call	prtrd	;print, wait for char
	exx		;swap register sets (back)
	cpi	ctlc	;did user press ctrl-c?
;
	if	jdbfix
	jz	wboote
	else
	jz	wboot	;if so, wboot:
wbpat0	equ	$-2
	endif
;
	sbi	esc	;did user press esc?
	rnz		;return (retry?) if not
;user pressed esc, set drive a: and do warm boot
	lda	cdisk
	ani	0f0h	;mask user (make drv 0 (a:))
	sta	cdisk
;
	if	jdbfix
	jmp	wboote
	else
	jmp	wboot
wbpat1	equ	$-2
	endif
;
;find disk sector size and install in prmtbl
;
idrd:	lhld	lunit	;get [lc]unit
	mov	a,h	;cunit in A
	cmp	l	;compare w/lunit
	rz		;return if same
	mvi	b,10	;else need to ID, b=10 tries
idrd1:	push	b	;save retry count
	call	setup	;make selbits, ssz for hstdsk, a=fdc tkreg
	push	h	;hl->prmtbl[hstdsk].selbits, save
	call	dseek	;A=trk
idrd2:	ani	typ1err	;isolate the error bits
	jrnz	idrd3	;brif any error
	call	wthdld	;wait hdld/mtron
	lxi	h,idsv	;point at idsv scratch space
	lxi	d,idsvln  ;this many bytes of id
	mvi	c,ddata	;fdc data port
	mvi	a,readid  ;fdc rd addr cmd
	call	fdcdma	;do fdcdma
idrd3:	pop	h	;restore ptr->selbits
	pop	b	;restore retry count
	jrz	idrd6	;brif no error
	djnz	idrd4	;try up to B times
	ora	a	;clear z for error return
	ret		;retries exhausted
;
idrd4:	mvi	a,dc1den  ;density bit in A
	xra	m	;toggle bit 6 (flip density)
	mov	m,a	;save it back
	bit	0,b	;even or odd retry?
	jrnz	idrd1	;brif odd
idrd5:	push	b	;even retry, save
	push	h	;save &prmtbl[hstdsk].selbits
	mvi	b,stepi	;b=step in w/tkupd,hdld, w/o vfy
	call	dseek2	;step in on side 0
	jr	idrd2	;check err, retry id
idrd6:	in	dsctr	;dsctr=trk# after read addr
	out	dtrck	;initialize fdc dtrck reg
	cpi	02h	;trk 2?
	jrc	idrd5	;brif trk < 2
	inx	h	;trk >=2, hl->prmtbl[hstdsk].ssz
	lda	idsv+3	;get ssz code from idsv buffer
	mov	m,a	;into (hl) (prmssz[hstdsk] byte)
	mov	b,a	;and in b
	inr	b	;++b for sec # shifting
	dcx	h	;--hl (back to selbits)
	mov	a,m	;selbits in A
	sta	lunit
idrd7:	cmp	a	;set z, clr cy
	ret		;and done
;
seek0:	call	drstor	;disk on trk00
seek:	call	idrd	;get ssz, density
	jrnz	seek0	;brif non-0
	in	dstat1	;get disk status
	mov	d,a	;in D
	in	dtrck	;get FDC track reg
	mov	c,a	;in C
	lda	hsttrk	;get hsttrk
	cmp	c	;same as FDC?
	mvi	b,seekv	;fdc seekv(erify) cmd in b
	jrnz	seek1	;brif no match
	mvi	b,seeknv  ;match, fdc seek (w/o vfy) in b
	bit	5,d	;head loaded?
	jrnz	idrd7	;return success if heads loaded
seek1:	call	dseek1	;else do the seek
	ani	typ1err	;ntrdy,sekerr,crcerr?
	rnz		;return if any error
	lhld	hsdly	;head settling delay
	bit	5,d	;head loaded?
	jrnz	wthlms	;brif heads loaded
wthdld:	lhld	hldly8	;8-in head-load delay
	lda	cunit	;get CUNIT
	bit	4,a	;check if mini
	jrz	wthlms	;brif 8-in
	lhld	hldly5	;mini head-load delay
	lda	lunit	;mtroff routine sets this to 0
	ora	a	;were motors turned off?
	jrnz	wthlms	;brif if motors already on
wtmtr:	lhld	mtrdly	;motor-on delay
;
;Delay loop waits the number of milliseconds given by the
;value in HL.
;
wthlms:	lda	cpuspd	;cpumhz scale value (13)
	mov	b,a	;in b (4)
wthlm1:	dcx	h	;bump down (4)
	inx	h	;and back up (4)
	djnz	wthlm1	;b times (7/12)
	dcx	h	;bump down again (4)
	mov	a,l	;check if zero (4)
	ora	h	;(4)
	jrnz	wthlms	;brif count not exhausted (7/12)
	ret		;(4)
;
;construct selbits and set step rate for hstdsk
;Exit: drive selected, A=fdc trk reg, Z=1 if trk00, Cy=0
;HL=prmtbl[hstdsk].selbits
;
setup:	call	fdsbh	;see if already set (hl->selbits)
	jrnz	set3	;brif so
	lda	hstdsk	;else get the disk
	mov	b,a	;in b
	inr	b	;++b for shifting
	xra	a	;0 in A (A:5=0 is motor on)
	stc		;set carry
set0:	ral		;a = 1 << (b-1)
	djnz	set0	;make DSx nybble
	ori	dc1den+dc1min  ;make DD mini dctrl1 byte
	mov	m,a	;save it in selbits entry in prmtbl
	out	dctrl1	;select the drive
	call	set5	;check ready, get trk#?
	mvi	a,restor  ;restore (home) cmd, shortest step rate
	call	fdcop	;returns w/A=dstat
	ani	strack0	;trk00 reporting?
	jrnz	set1	;brif so
	mvi	a,restor+3  ;else try again w/longest step value
	call	fdcop	;returns w/A=dstat
set1:	in	dstat2	;drive on trk00, which type?
	rar		;drive type flag->cy
	lda	stprt5	;get mini step-rate bits
	jrnc	set2	;brif mini drive
	mvi	a,dc1min  ;set up to toggle bit 4
	xra	m	;make 8-in selbits
	mov	m,a	;and save it
	lda	stprt8	;get 8-in step-rate bits
set2:	dcx	h	;HL->prmtbl[hstdsk].stprt
	mov	m,a	;save step rate for hstdsk
	inx	h	;bump back to selbits
set3:	in	dtrck	;read fdc dtrck reg
	ora	a	;set flagz
	lda	hsthd	;get side flag
	mov	b,a	;in b
	mov	a,m	;get selbits
	sta	cunit	;set CUNIT
	jrnz	set4	;brif not trk00
	ani	sdmsk	;else trk00, make single-density
set4:	ora	b	;or in side flag
	out	dctrl1	;select drive
	bit	4,a	;mini drive?
	jrnz	set6	;brif mini drive
set5:	in	dstat	;else 8-in, read fdc dstat reg
	bit	7,a	;test a:7 (not ready)
	push	h	;save ptr
	cnz	wtmtr	;motor-on delay (1s)
	pop	h	;restore ptr
set6:	in	dtrck	;read fdc dtrck register
	ora	a	;set flagz
	ret
;
;routine returns selbits in A, ssz+1 in B
;HL->prmtbl[{sek,hst,(hl)}dsk].selbits
;Z=0 if selbits already set.
;
fdsbs:	lhld	sekdsk	;for sekdsk
	jr	fdsb
fdsbh:	lhld	hstdsk	;for hstdsk
fdsb:	mvi	h,00h	;clear h
	push	d	;save de
	mov	d,h	;copy disk to DE
	mov	e,l
	dad	d	;*2
	dad	d	;*3 offset to disk data in hl
	lxi	d,prmssz  ;floppy sector size base adr
	dad	d	;hl->ssz for {sek,hst}dsk
	pop	d	;done with de
	mov	b,m	;ssc in b
	inr	b	;add 1 for loop control
	jrnz	fdsb0	;was 0 now non-0, branch
	mov	m,b	;was ff, now zero, mark it.
fdsb0:	dcx	h	;bump down to selbits
	xra	a	;0 in a
	ora	m	;or in the data to set/clr Z
	ret	;and go back
;
;
; routine coni reads the console and strips off the ASCII
;	parity bit.
;
prtrd:	call	prtwd	;print crlf, msg at (hl), wait for char.
coni:	call	ci	;get character in a
	ani	07fh	;clear b7
rts:	ret
;
; Routine prtwd prints an ASCII string onto the console.
;	The string must be terminated by bit 7 set in the
;	last character of the string.  The string will start
;	a new line (ep = prtwd) or continue on the same
;	line (ep = prtwa)
;
prtwd:
crlf:	push	h	;save msg ptr
crlfa:	lxi	h,crmsg
	call	prtwa	;print crlf
	pop	h	;restore msg ptr
;
prtwa:	push	b	;save bc
prta:	mov	c,m	;char in c
	if	jdbfix
	res	7,c	;clear high bit
	endif
	call	co	;print it
	bit	7,m	;test (hl):7
	inx	h	;next char
	jrz	prta	;brif not last char
prtb:	pop	b	;else restore bc
	ret
;
;sio output status routines w/XOFF/any handshaking
;
hspst:			;hi-spd punch (ptp) status
	mvi	c,siocc	;also lst.crt: status
	lda	siocsm	;get status bits/mask
	jr	sioost	;and branch to status routine
;
ttost:	mvi	c,siobc	;lst.tty:
	lda	siobsm
;
	if	not jdbfix
;
	jr	sioost
;
	mvi	c,sioac	;nothing references this
	lda	sioasm	;dead code?
;
	endif
;
sioost:	mov	b,a	;mask byte in b
sioos1:	bit	7,b	;b:7==1? (use xoff/any hskng?)
	jrz	sioos3	;brif b:7==0, no
	inp	a	;else read sio status
	rrc		;look at bit 0 
	jrnc	sioos3	;brif cy=0, no char waiting
sioos2:	call	chrin	;else char ready, read sio data
	inr	c	;bump back up to status port
	cpi	xoff	;was char xoff?
	jrz	sioos2	;yes, loop waiting for next char
sioos3:	mvi	a,10h	;not xoff, sio ext/stat reset in a
	outp	a	;clear ext/stat int condition
	inp	a	;read from sio status port
	ana	b	;mask with table data
	xra	b	;toggle with table data
	ani	07fh	;clear bit 7
sioos4:	jrz	sioos5	;brif all status bits 0
	mvi	a,01h	;else set up to make 0
sioos5:	dcr	a	;if 0, now ff.	if 1, now 0
	ret		;ff=char avail, 0=no char avail
;
;
;Output routines for the various ports
;Entry: char in C
;Exit: char output to device and returned in C
;
cuso1:
luse1:
puso1:	mvi	b,siodc	;con.uc1:,pun.up1:
	lda	siodsm	;get status bits/mask
	jr	sioout	;and branch to output routine
;
crtout:
hsp:	mvi	b,siocc	;con.crt:,pun.ptp:
	lda	siocsm
	jr	sioout
;
ttyco:
ttpnch:	mvi	b,sioac	;con.tty:,pun.tty:
	lda	sioasm
;
sioout:	mov	d,c	;save byte in d
	mov	c,b	;put port in c
	mov	b,a	;save status mask in b
sioot0:	call	sioos1	;get status
	jrz	sioot0	;until ready
	dcr	c	;bump down to data port
	outp	d	;output the byte
	mov	c,d	;and return it in c
	ret
;
lstat:	lda	iobyte
	ani	0c0h	;mask lst: bits
	jrz	ttost	;bif lst.tty:
	cpi	80h
	jrc	hspst	;bif lst.crt:
	jrz	lprts	;bif lst.lpt:
	mvi	c,siodc	;else lst.ul1:
	lda	siodsm	;get status bits/mask
	jr	sioost	;and branch to status routine
;
po:	lda	iobyte
	ani	30h	;mask pun: bits
	jrz	ttpnch	;bif pun.tty: (00)
	cpi	20h
	jrc	hsp	;bif pun.ptp: (10)
	jrz	puso1	;bif pun.up1: (20)
ttyout:	mvi	b,siobc	;lst.tty:,pun.up2:
	lda	siobsm	;get status bits/mask
	jr	sioout	;and branch to output routine
;
co:	lda	iobyte
	ani	03h	;mask con: bits
	jrz	ttyco	;bif con.tty: (0)
	cpi	02h
	jrc	crtout	;bif con.crt: (1)
	jrnz	cuso1	;bif con.uc1: (3)
;
lo:	lda	iobyte	;con.bat: (2), get iobyte
	ani	0c0h	;mask lst: bits
	jrz	ttyout	;bif lst.tty: (00)
	cpi	80h
	jrc	crtout	;bif lst.crt: (40)
	jrnz	luse1	;bif lst.ul1: (c0)
;
lprt:	call	lprts	;else lst.lpt: (80), check status
	jrz	lprt	;wait until ready
	mov	a,c	;byte in a
	out	pioad	;output to parallel printer
	ret
;
lprts:	in	pioas	;read parallel port status
	lxi	h,lptsm	;point at status bits/mask
	ana	m	;mask bits....
	inx	h	;bump to next data
	xra	m	;toggle bits
	jr	sioos4	;go make 00 or ff as appropriate
;
csts:	lda	iobyte
	ani	03h	;mask con: bits
	jrz	cittst	;bif tty:
	cpi	02h
	jrc	crtst	;bif crt:
	jrnz	cust1	;bif uc1:
;
batst:	lda	iobyte	;else bat:, get iobyte again
	ani	0ch	;mask rdr: bits
	jrz	rdttst	;bif rdr.tty:
	cpi	08h
	jrc	ptrst	;bif rdr.ptr:
	jrnz	rust2	;bif rdr.ur2:
cust1:
rust1:	mvi	c,siodc	;else uc1:,ur1:
	db	lda	;lda nnnn instruction falls through...
ptrst:
crtst:	mvi	c,siocc	;con.crt:,ptr:
	db	lda
rdttst:	mvi	c,siobc	;rdr.tty:
	db	lda
cittst:
rust2:	mvi	c,sioac	;con.tty:,rdr.ur2:
			; ...to stat routine below:
ckinst:	call	mtroff	;turn off motors, de-select all drives
	inp	a	;read sio status
	ani	rxrdy	;char ready?
	rz		;no, so return 0
	ori	0ffh	;yes, so make a:=0ffh
	ret
;
ci:	lda	iobyte
	ani	03h	;mask con: bits
	jrz	citty	;bif con.tty:
	cpi	02h	
	jrc	crtin	;bif con.crt:
	jrnz	cusi1	;bif con.uc1:
;
ri:	lda	iobyte	;else bat:, get iobyte again
	ani	0ch	;mask rdr: bits
	jrz	rdttin	;bif rdr.tty:
	cpi	08h
	jrc	ptrin	;bif rdr.ptr:
	jrnz	rusi2	;bif rdr.ur2:
cusi1:	mvi	c,siodc	;con.uc1:,rdr.ur1:
	db	lda
crtin:
ptrin:	mvi	c,siocc	;con.crt:,rdr.ptr:
	db	lda
rdttin:	mvi	c,siobc	;rdr.tty:
	db	lda
citty:
rusi2:	mvi	c,sioac	;con.tty:,rdr.ur2:
;
chrin:	call	ckinst	;get sio rx status
	jrz	chrin	;until ready
	dcr	c	;bump down to data port
	inp	a	;read a byte
	ret		;and return it.
;
mtroff:	in	dstat1
	ani	hlmsk	;head loaded?
	rnz		;rif head loaded
	sta	lunit	;else clear last-used drive
	ori	dc1mot	;turn motor off
	out	dctrl1	;and de-select all drives
	ret
;
ctc0isr:
dmacisr:
	push	psw	;save af
	mvi	a,0c3h	;dmac reset command in a
	out	dmacmd	;send to dma controller
	pop	psw	;restore af
ctc3isr:
fdcisr:	ei	;enable interrupts
	reti	;return from interrupt
;
flopdma:		;setup string for dma read of fdc
	db	0c3h	;wr6: dma reset command
	db	079h	;wr0: xfr b->a, adr a, len next
dmaad:
	dw	0000h	;port a address
dmalen:
	dw	0000h	;transfer length - 1
	db	014h	;wr1: prt a: mem, inc
	db	028h	;wr2: prt b: i/o, fxd
	db	095h	;wr4: byte mode, b adr, int ctl nxt
dmaprt:
	db	017h	;fdc data port
	db	012h	;int @ endofblk, ivl follows
	db	dmavec mod 256	;dma int vec @ bios+0b0h (overlays ctc0)
	db	092h	;wr5: rdy*, stop @ end, ce*/wait* mux
	db	08bh	;wr6: re-init status byte
	db	0cfh	;wr6: ld src (b) adr, clr byte cntr
dmadir:
	db	001h	;wr0: (1: b->a, rd; 5: a->b, wr)
	db	0cfh	;wr6: ld src (a | b) adr, clr byte cntr
	db	0abh	;wr6: enable interrupts
dmastln	equ	$-flopdma	;send only this many bytes for setup
	db	087h	;wr6: start the dma
;
;BIOS message strings.  Last character in each string has bit 7 set
;to indicate end of string.
;
dsktmo:	db	cr,lf,'DISK TIMEOU','T'+080h
dnrmsg:	db	'DRIVE '
dnrdsk:	db	0	;for drive letter in msg
	db	' NOT READ','Y'+080h
botmsg:	db	bel,'BAD BOO','T'+080h
crmsg:	db	cr,lf+80h
;
;dph's for the floppy disks
dphbase:
	dw	0000	;xlt0
	dw	0000,0000,0000	;scratch space
	dw	dirbuf	;dirbuf
dphdpb:	dw	0000	;dpb0 (filled in dynamically)
	dw	csv0	;csv0
	dw	alv0	;alv0
;
	dw	0000
	dw	0000,0000,0000
	dw	dirbuf
	dw	0000
	dw	csv1
	dw	alv1
;
	dw	0000
	dw	0000,0000,0000
	dw	dirbuf
	dw	0000
	dw	csv2
	dw	alv2
;
	dw	0000
	dw	0000,0000,0000
	dw	dirbuf
	dw	0000
	dw	csv3
	dw	alv3
;
;select table for single-sided 8" floppy disks
;pointer pair: translation table (xlt), DPB
;
;pointer arithmetic requires starting 4 bytes before the
;actual start of the table.
seltbl:		;xlt,dpb
;single-density (fm)
	dw	t826,dp8s0	;ssz=128
	dw	t815,dp8s1	;ssz=256
	dw	t808,dp8s2	;ssz=512
	dw	t804,dp8s2	;ssz=1024
;double-density (mfm)
	dw	t848,dp8d0	;ssz=128
	dw	t826,dp8d1	;ssz=256
	dw	t815,dp8d2	;ssz=512
	dw	t808,dp8d3	;ssz=1024
;
;DPBs for single-sided 8-inch floppy disks
;single-density
dp8s0:	dw	26	;spt	128byt/sec
	db	3	;shf
	db	7	;blm
	db	0	;exm
	dw	242	;dsm	;243k
	dw	63	;drm
	db	192	;al0
	db	0	;al1
	dw	16	;cks
	dw	2	;off
;
dp8s1:	dw	30	;spt	256byt/sec
	db	4	;shf
	db	15	;blm
	db	1	;exm
	dw	139	;dsm	;140*2k=280k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	2	;off
;
dp8s2:	dw	32	;spt	512,1024byt/sec
	db	4	;shf
	db	15	;blm
	db	1	;exm
	dw	149	;dsm	;150*2k=300k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	2	;off
;
;double-density
dp8d0:	dw	48	;spt	128byt/sec
	db	4	;shf
	db	15	;blm
	db	1	;exm
	dw	195	;dsm	;196*2k=392k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	2	;off
;
dp8d1:	dw	52	;spt	1s256,2s1d128byt/sec
	db	4	;shf
	db	15	;blm
	db	1	;exm
	dw	242	;dsm	;243*2k=486k
	dw	63	;drm
	db	128	;al0
	db	0	;al1
	dw	16	;cks
	dw	2	;off
;
dp8d2:	dw	60	;spt	1s512,2s1d256byte/sec
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	280	;dsm
	dw	191	;drm	;192*2k=384k
	db	224	;al0
	db	0	;al1
	dw	48	;cks
	dw	2	;off
;
dp8d3:	dw	64	;spt	1s1024,2s1d512,1024byt/sec
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	299	;dsm	;300*2k=600k
	dw	127	;drm
	db	192	;al0
	db	0	;al1
	dw	32	;cks
	dw	2	;off
;
;select table for double-sided 500kbps (8", HD5.25") floppies.
;pointer pair: translation table (xlt), DPB
;
;pointer arithmetic requires starting 4 bytes before the
;actual start of the table.
seltbla:	;xlt,dpb
;single-density
	dw	t826,dp8d1	;ssz=128
	dw	t815,dp8d2	;ssz=256
	dw	t808,dp8d3	;ssz=512
	dw	t804,dp8d3	;ssz=1024
;double density
	dw	t848,dp8d0a	;ssz=128
	dw	t826,dp8d1a	;ssz=256
	dw	t815,dp8d2a	;ssz=512
	dw	t808,dp8d3a	;ssz=1024
;
;DPBs for double-sided 8-inch floppy disks:
dp8d0a: dw	96	;spt	DSDD, 48 128-byt sec/trk
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	449	;dsm	450*2k=900k
	dw	255	;drm
	db	0f0h	;al0
	db	0	;al1
	dw	64	;cks
	dw	2	;off
;
dp8d1a:	dw	104	;spt	DSDD, 26 256-byte sec/trk
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	01e6h	;dsm	487*2k=974k
	dw	255	;drm
	db	0f0h	;al0
	db	0	;al1
	dw	64	;cks
	dw	2	;off
;
dp8d2a:	dw	120	;spt	DSDD, 15 512-byte sec/trk
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	0231h	;dsm	562*2k=1124k
	dw	255	;drm
	db	0f0h	;al0
	db	0	;al1
	dw	64	;cks
	dw	2	;off
;
dp8d3a:	dw	128	;spt	DSDD, 8 1024-byte sec/trk
	db	4	;shf
	db	15	;blm
	db	0	;exm
	dw	0257h	;dsm	600*2k=1200k
	dw	255	;drm
	db	0f0h	;al0
	db	0	;al1
	dw	64	;cks
	dw	2	;off
;
;translation table (xlt) for 500kbps (8", HD5.25") disks
;		s/t, skf
t848:	db	48, 1
t826:	db	26, 6
t815:	db	15, 4
t808:	db	8, 3
t804:	db	4, 2
;
;select table for 250kbps (SD/DD 5.25",3.5") disks
;pointer pair: translation table (xlt), DPB
;
;pointer arithmetic requires starting 4 bytes before the
;actual start of the table.
mseltbl:	;xlt,dpb
;single-density
	dw	t518,dp5s0	;ssz=128
	dw	t510,dp5s1	;ssz=256
	dw	t505,dp5s1	;ssz=512
	dw	t502,dp5s3	;ssz=1024
;double-density
	dw	t529,dp5d0	;ssz=128
	dw	t518,dp5d1	;ssz=256
	dw	t510,dp5d2	;ssz=512
	dw	t505,dp5d2	;ssz=1024
;
;translation table for 250kbps (SD/DD 5.25", 3.5") disks
;		s/t, skf
t529:	db	29, 7
t518:	db	18, 5
t510:	db	10, 3
t505:	db	5, 2
t502:	db	2, 1
;
;
;one-time use code overlaid by udata area
;
boot:
	if	jdbfix
	lxi	h,wboot	;get wboot adr
	shld	bios+1	;cut off access to this code
	endif
	im2		;mode 2 interrupts
	mvi	a,ctc0v	/ 256	;ivh byte in a
	stai		;set it up
;
; fdinstal.com stashes alternate settings based on user
; preferences in the bdos stack space just below bios
;
	if	jdbfix
	lxi	h,bios-20h	;HL->ctc00 mode, count
	lxi	d,table2
	ldi
	ldi			;move 2 bytes
	inx	d		;bump past CTC0 IVL byte
	lxi	b,lntbl2a
	else
	lhld	bios-20h	;ctc00 mode, count
	shld	table2		;override default values
	lhld	bios-1eh	;ctc02 mode, count
	shld	table2+3
	lhld	bios-1ch	;ctc10 mode, count
	shld	table2+5
	lhld	bios-1ah	;ctc12 mode, count
	shld	table2+7
	lxi	h,bios-18h	;sioa-d wr4,3,5 data
	lxi	d,sioinit	;table2+9
	lxi	b,lnsioinit
	endif
	ldir			;move the rest
;
;zero-out parameter table:
	xra	a
	mvi	b,12
	lxi	h,prmtbl
st0:	mov	m,a
	inx	h
	djnz	st0
;
	lxi	h,table2  ;bring up the ports
	lxi	sp,table1  ;sp points to port/count data
	mvi	d,ports	;init 9 devices
st1:
	pop	b	;get count in b, port in c
	outir		;setup device at (c)
	dcr	d	;decrement device count
	jrnz	st1	;brif not done all devices
;
;user-defined iobyte and cdisk value stash 22h bytes
;below bios in bdos stack area
	lhld	bios-22h	;get default iobyte/du: data
	shld	iobyte		;set up in zero page
;
	lxi	sp,tbuff	;set up stack pointer
;
; compute CP/M serial #
;
	lda	bdos	;get lsb mfr code
	mov	e,a	;in e
	lda	bdos+2	;get msb of mfr code
	mov	d,a	;in d
	lxi	h,mfrcd	;hl -> mfr code field
	call	h2d2a	;hex->dec->ascii conversion
	lhld	bdos+4	;get bdos serial # (lsw)
	mov	d,l	;byte-swap into de
	mov	e,h
	lxi	h,snstr	;hl -> ser. # field
	call	h2d2a	;hex->dec-ASCII conversion
;
	lxi	h,logmsg  ;sign on to the system
	call	prtwd	;print crlf then message
	lxi	h,ccp	;go here after cold boot
cbootgo	equ	$-2	;patch the previous word to chain
			; to gide bios
	jmp	gocpm1	;finish up at end of wboot.
;
h2d2a:
	push	h	;save output string addr.
;
; zero-out field in output string
	xra	a	;0 in A
	mvi	b,6	;up to 6 decimal digits
snfldzrlp:
	mov	m,a	;0 in (hl)
	inx	h	;bump to next
	djnz	snfldzrlp  ;until done 6 bytes
;
	popix		;output field ptr in ix
	lxiy	dvsrtbl	;divisor table base in iy
	xchg		;dividend in hl
;
	if	jdbfix
	mov	d,b	;B=0, initial-D
	else
	lxi	d,0	;0000 in de
	endif
;
sndvlp:	mov	a,d	;get d into a (1st 0, then '0')
	dcr	a	;decrement now ff/2f (init quotient)
	ldy	c,0	;byte from table into c
	inr	c	;if end of table, ff->00
	jrz	sndvdn	;brif end of table
	dcr	c	;wasn't 0 (ff, end of table), restore
	ldy	b,1	;get 1(iy) into b (bc=divisor)
	inxiy
	inxiy	;point to next divisor
sndvlp1:
	ora	a	;clear Cy
	inr	a	;++quotient
	dsbc	bc	;hl = hl-bc...
	jrnc	sndvlp1	;while hl >= bc
;
	if	not jdbfix  ;elide do-nothing code
	inr	e	;E is 0 on entry...
	dcr	e	;E is 0 on exit...
	jrnz	wtfdtd	;jrif E non-0 (never taken)
	endif
;
	dad	b	;e==0, hl = hl + bc (restore remainder)
	ora	a	;set/clear flags
	jrz	sndvlp	;brif quotient (a) == 0 (hl < bc)
	; get next divisor
	cpi	10	;non-0 quotient, >= 10?
	jrnc	wrsnfld	;brif already ascii (stor byt & go agn)
	mvi	d,'0'	;a < 10, make ascii digit
	ora	d
wrsnfld:
	stx	a,0	;store in field @ 0(ix)
	inxix	;++ix to next char in field
	jr	sndvlp	;continue w/next divisor
;
	if	not jdbfix  ;elide dead code
wtfdtd:			;(never reached by above code)
	dcr	e	;--e
	jr	sndvlp1	;continue w/same divisor
	endif
;
;we get here when we reach the end of the divisor table.
;a=ff if dividend (hl) was 0000, 2f otherwise
sndvdn:
	inr	a	;bump last quotient
	rnz		;if non-0, done
	mvi	a,'0'	;else ff->0, get ascii zero char
	stx	a,0	;store in field @ 0(ix)
	ret
;
dvsrtbl:	;divisors for hex->dec(->ascii) conversion
	dw	10000, 1000,	100,	10,	1
	dw	-1	;end of table
;
table1:	;port # and byte count table
	db	ctc0,3	;ctc0 (ctl, tc, ivl)
	db	ctc2,2	;ctc2 (ctl, tc)
	db	ctc10,2	;ctc10 "
	db	ctc12,2	;ctc12 "
	db	sioac,6	;sioa wr4,3,5
	db	siobc,6	;siob "
	db	siocc,6	;sioc "
	db	siodc,6	;siod "
	db	ctc3,2	;ctc3 (fdc) (ctl, tc)
ports	equ	($-table1)/2
;
table2:	;init strings for devices
	db	047h	;ctc0 data, cmode
	db	00ch	;divide by 192	;9600 baud
	db	ctc0v mod 256	;int vector at bios+0b0h
;
tbl2a:	db	047h	;ctc2 data, cmode
	db	00ch	;divide by 192
;
	db	047h	;ctc10 data, cmode
	db	00ch	;divide by 192
;
	db	047h	;ctc12 data, cmode
	db	00ch	;divide by 192
sioinit:
	db	014h	;ext/stat reset, select wr4
	db	044h	;set up wr4 on sioa
	db	003h	;select wr3
	db	0c1h	;set up wr3	; 8data, rcvr enab.
	db	005h	;select wr5
	db	0eah	;set up wr5
;
	db	004h, 044h  ;select/set up siob wr4
	db	003h
	db	0c1h
	db	005h
	db	0eah
;
	db	014h, 044h  ;sioc... (2nd phys dev)
	db	003h
	db	0c1h
	db	005h
	db	0eah
;
	db	004h, 044h  ;siod...
	db	003h
	db	0c1h
	db	005h
	db	0eah
lnsioinit	equ	$-sioinit
lntbl2a	equ	$-tbl2a
	db	047h, 001h  ;ctc3 = fdc int generator
;
logmsg:
	if	test
	db	(msize/10)+'0',(msize mod 10)+'0'
	else
	db	'00'	;filled in by CPMOVE.COM
	endif
	db	'k CP/M Vers '
	db	(vers/10)+'0','.',(vers mod 10)+'0'
	db	'  (FD4000 V',ver+'0','.'
	if	jdbfix
	db	(rev+1)+'0',')'
	else
	db	rev+'0',')'
	endif
	db	cr,lf,'  Serial # '
mfrcd:	db	0,0,0,0,0,0	;up to 6 digit mfr code
	db	'-'		;dash
snstr:	db	0,0,0,0,0,0	;up to 6 digit S/N
	db	cr,lf+80h
;
;
biosz	equ	$-bios	;size of fixed code/data
;
gbldofs	equ	$ + 16 - $ mod 16  ;gide bios ldr @fbios + this
cmp1ofs	equ	comp1	- bios	;comp1 proc @fbios + this
gcpmofs	equ	gocpm	- bios	;gocpm proc @fbios + this
prtwdof	equ	prtwd	- bios	;prtwd proc @fbios + this
prtwaof	equ	prtwa	- bios	;prtwa proc @fbios + this
gbptofs	equ	cbootgo - bios	;patch @fbios + this to chain
				; to gide loader, hboot:
	if	not jdbfix
wbpof0	equ	wbpat0	- bios	;patch w/wboote
wbpof1	equ	wbpat1	- bios	;patch w/wboote
	endif
;
;
	org	boot
;
;uninitialized data areas:
;
dbuf:	ds	hstsiz	;disk buffer
;
dmaadr:	ds	2	;direct memory address
;
hstdsk:	ds	1	;host disk
hsthd:	ds	1	;host side/head (0,80h for floppy)
hstsec:	ds	1	;host sector
hsttrk:	ds	2	;host track
;
status:	ds	1	;fdc status hold
cmnd:	ds	1	;fdc command hold
LUNIT:	ds	1	;last-used drive
CUNIT:	ds	1	;current drive
rwflg:	ds	1	;flag for host rd (0) or wr (20)
hstbuf:	ds	2	;address of buffer for host r/w
erflag:	ds	1	;flag for error reporting
rsflag:	ds	1	;flag for pre-read required
readop:	ds	1	;indicate rd (non-0)/wr (0)
wrtype:	ds	1	;wrall, wrdir, wrual
;
sekdsk:	ds	1	;disk to seek
sekhd:	ds	1	;side/head to seek (0,80h for floppy)
sekhst:	ds	1	;host sector to seek
sektrk:	ds	2	;track to seek
seklen	equ	$-sekdsk  ;length of sek*/una*/hst*
;
seksec:	ds	1	;sector to seek
seksel:	ds	1	;seksel, selbits for sekdsk
hstact:	ds	1	;active data in host buffer
hstwrt:	ds	1	;host buffer needs writing
;
unadsk:	ds	1	;disk for rd/wrual
unasec:	ds	2	;sector for rd/wrual
unatrk:	ds	2	;track for rd/wrual
unacnt:	ds	1	;sector count for rd/wrual
;
cpmsec:	ds	2	;CP/M sec# saved in sectran
;
oldstk:	ds	2	;save SP before switching to local stack
;
idsv:	ds	6	;4 bytes trk/sec/sid/siz + 2 bytes CRC
idsvln	equ	$-idsv
;
	ds	1	;??
;
;scratch RAM area for BDOS use
;
dirbuf:	ds	128
;
alv0:	ds	75
csv0:	ds	64
alv1:	ds	75
csv1:	ds	64
alv2:	ds	75
csv2:	ds	64
alv3:	ds	75
csv3:	ds	64
;local bios stack space
	ds	24
biosstk:
biosmem:	equ	$-bios	;size w/udata area
;
const:	equ	csts
conout:	equ	co
list:	equ	lo
punch:	equ	po
reader:	equ	ri
listst:	equ	lstat
	end
