	.title	"skipmap.asm 2/17/86 bmm"
; 2/17 fixed cleared map on A: 
; 2/16 FIXED 'ani 80h'
.remark	"Patch bdos to skip rebuilding allocation bitmap for
	permanently mounted drives. Uses bit 7 of extended
	dph for Turborom."

	debug  =\"Debug=1, rel = 0 ==> "

.phex

.ifg debug, [
	PATCH =\"Enter addr in bios for overflow code ==> "
	BIBASE =\"Enter bios base address ==> "
	BDOSLC = bibase-0E00h

; 61.75k reference values
;;bibase	=0f100h
;;biocod	=0da80h

; NOTE - to debug-run/test this code, must protect it and the ccp.

	.pabs
	]

; REL mode:
	[
	.xlink
;
origin:
ccpbase = origin+200h
bdoslc	= origin+200h+800h
patch	= bdoslc+28ah		;first free byte
patche	= bdoslc+2cdh		;last free byte
	]

.xlist

;referenced locations within the BDOS (from 2.2E file)
backup	=	bdoslc+01A4H
charbuf	=	bdoslc+030EH
chkrofl	=	bdoslc+0544H
chkwprt	=	bdoslc+0554H
ckconsol=	bdoslc+0123H
ckfilpos=	bdoslc+05F5H
closeflg=	bdoslc+0DD2H
counter	=	bdoslc+0DD8H
curpos	=	bdoslc+030CH
delfile	=	bdoslc+0CD7H
dirwrite=	bdoslc+05C6H
dowrite	=	bdoslc+03B8H
error5	=	bdoslc+00B4H
extmask	=	bdoslc+0DC5H
fcb2hl	=	bdoslc+055EH
filepos	=	bdoslc+0DEAH
findfst	=	bdoslc+0718H
findnxt	=	bdoslc+072DH
fndstat	=	bdoslc+0DD4H
getchar	=	bdoslc+00FBH
getempty=	bdoslc+0924H
getrdr	=	bdoslc+02CEH
gets2	=	bdoslc+0569H
getnext	=	bdoslc+095AH
goback1	=	bdoslc+0D91H
ioerr1	=	bdoslc+0305H
mode	=	bdoslc+0DD5H
morefls	=	bdoslc+057FH
newline	=	bdoslc+01B1H
nxentry	=	bdoslc+0605H
openit1	=	bdoslc+085AH
outchar	=	bdoslc+0148H
outflag	=	bdoslc+030AH
params	=	bdoslc+0343H
prtflag	=	bdoslc+030DH
rdbuff	=	bdoslc+01E1H
rdwrtflg=	bdoslc+0DD3H
reader	=	bdoslc+0E15H
samext	=	bdoslc+0707H
savefcb	=	bdoslc+0DD9H
savnrec	=	bdoslc+0DE3H
savnxt	=	bdoslc+0DE1H
setfile	=	bdoslc+066BH
setnrec	=	bdoslc+04D2H
sets2b7	=	bdoslc+0578H
setstat	=	bdoslc+0301H
showit	=	bdoslc+017FH
status	=	bdoslc+0345H
starting=	bdoslc+030BH
stfilpos=	bdoslc+05FEH
strdata	=	bdoslc+04BBH
trksec1	=	bdoslc+03D1H
usrstk	=	bdoslc+030FH
wtseq6	=	bdoslc+0A6EH
wtspecl	=	bdoslc+0D9BH
;*******************************************
; additional skipmap references

active	=	bdoslc+0342h
alloc0	=	bdoslc+0DCAh
alocvect =	bdoslc+0DBFh
bigdsk	=	bdoslc+0DDDh
bitmap	=	bdoslc+06a3h
chknmbr	=	bdoslc+058ch
chkvect	=	bdoslc+0DBDh
de2hl	=	bdoslc+034Fh
dirbuf	=	bdoslc+0DB9h
diskpb	=	bdoslc+0DBBh
dsksiz	=	bdoslc+0DC6h
homedrv	=	bdoslc+03A1h
scrat1	=	bdoslc+0DB3h
scrat2	=	bdoslc+0DB5h
scrat3	=	bdoslc+0DB7h
sectrs	=	bdoslc+0DC1h
shiftr	=	bdoslc+04Eah
stfilpos =	bdoslc+05Feh
userno	=	bdoslc+0341h
xlate	=	bdoslc+0DD0h
;
cks	=	bdoslc+0dcch	;disassembly calls this ALLOC1
selct0	=	bdoslc+0363h
;
bitm1a	=	bdoslc+06bah
bitm1b	=	bdoslc+06c4h

ccp	=	bdoslc-0800h	;for testing
ccp3	=	bdoslc-0800h+3
;
;these are already equated:
;
;nxentry	=	bdoslc+0605h
;fcb2hl		=	bdoslc+055Eh
;status		=	bdoslc+0345h
;setfile	=	bdoslc+066Bh

;********************************
;
;	BDOS patches
;
;********************************
;
.list
;
; (This higher-address routine one first for hex-load testing,
; otherwise zsid stops loading.)

;****************************************
;
; BDOS build-bitmap routine.
;
; For fixed drive, skip directory scan and bitmap re-build.
; For drive A:, scan directory for $ (submit) clue, but
; skip rebuild.
;
; Address alignment preserved for General Datestamper load checks.
;
;****************************************
;
	.LOC	BITMAP
;
	jmp	skpmap		;returns to bitmap caller if not A:
				;  and map already built
BITM0A:	mvi	c,3		; hl = dsksiz
	call	shiftr
	inx	h
	mov	b,h
	mov	c,l
	lhld	alocvect
..1:	mvi	m,0
	inx	h
	dcx	b
	mov	a,b
	ora	c
	jnz	..1
.ifn (bitm1a - .), [
	.prntx	"mismatch at BITM1A"
	]
;==> BITM1A:
	lhld	alloc0		;general DateStamper must match here
	xchg
	lhld	alocvect
	mov	m,e
	inx	h
	mov	m,d
.ifn (bitm1b - .), [
	.prntx	"mismatch at BITM1B"
	]
;==> BITM1B:
	call	homedrv		; and here
	lhld	scrat1
	mvi	m,3
	inx	h
	mov	m,a		;homedrv returns with A=0
	call	stfilpos
;
..2:	mvi	c,0ffh		;loop over the directory
	call	nxentry
	call	ckfilpos
	jrz	setbit		;no more files - exit
	call	fcb2hl
	mvi	a,0E5h
	cmp	m
	jrz	..2
	lda	userno
	cmp	m
	jrnz	..3
	inx	h
	mov	a,m
	sui	'$'
	jrnz	..3
	dcr	a		;show submit clue in returned STATUS
	sta	status
..3:	call	skipa		;set bitmap, unless hard A: already built
	call	chknmbr
	jr	..2
;
; (use free space for:)
;
; On exit from bitmap routine,
; set the "don't-need-to-rebuild" bit for active drive.
;
xdph = .+1
SETBIT:	lxi	h,0000		;set map-built bit
	set	7,m
	ret			;assumes Z flag not needed on return
;
bmfre = (bdoslc+0701h) - .
.ifl	bmfre,[.prntx "overflow in BITMAP"]
;
;
;****************************************
;
; BDOS select-disk routine patch.
; capture dph address on each actual disk select
; preserve alignment for General Datestamper verification code
;
;****************************************
;
	.LOC	SELCT0		;following 'call seldsk'
				;and bad-drive test
	dcx	h
	dcx	h
	shld	xdph		;save extended dph addr
	lxi	b,2		;shorter
	dad	b
	mov	e,m
	inx	h
	mov	d,m
	inx	h
	shld	scrat1		;scratch1
	dad	b
	shld	scrat2
	dad	b
	shld	scrat3
	dad	b
	xchg
	shld	xlate
	lxi	h,dirbuf
	mvi	c,8
	call	de2hl
	lded	diskpb
	lxi	h,sectrs	;sectors
	mvi	c,15
	call	de2hl		;returns with c=0
	lda	dsksiz + 1	;shortened here...
	lxi	h,bigdsk
	mov	m,c		;set bigdisk = c = 0 for >255 spg
	ora	a
	jrnz	selct1
	dcr	m		;set bigdisk = 0ff if <= 255
selct1:	ora	m		;set NZ = good select in both cases
	ret
	;
selfre = (bdoslc+3a1h) - .		; 0 ?
.ifl	selfre,[.prntx "overflow in SELECT"]
;
;
;******************************************
;
;  overflow code for BIOS or holes in BDOS
;
;******************************************
;
;
; Use bit 7 of extended dph SET to indicate bdos alloc. vector
; does not need to be rebuilt (if drive is non-removable).
;
;----------------------------------------
;
; If A:
;	if not mapped, return to next instr
;	else skip over clearing of bitmap
; If not A:
;	if not mapped, return to next instr
;	else return to BITMAP's caller.
;
	.loc	PATCH
;
SKPMAP:	lda	active
	ora	a
	jrnz	..b
;
; drive A:
;
..a:	call	dontskip	;and first-timers and floppies
	jrnz	..x		;1st-time, continue
	jmp	BITM1A		;skip clearing the A: bitmap
;
; all other drives
;
..b:	call	dontskip
	rz			;already built - return to BITMAP'S CALLER
..x:	lhld	dsksize		;overlayed instr.
	jmp	BITM0A		;go back to next sequential instr in BITMAP
;
;
; If drive A:, a hard drive, and map already built,
;	skip building map.
; Else build it.
;
SKIPA:	lda	active		;f A:
	ora	a
	jrnz	..0
	call	dontskip	;..and already built
	rz			;..skip rebuild
..0:	mvi	c,1		;c=1 to
	jmp	setfile		;.. set the bits
;
; return NZ if map must be built, Z if ok to skip
;
dontskip:
	lhld	cks		;if # check dir. entries >0
	mov	a,h
	ora	l
	rnz
	lhld	xdph		;or drive not yet logged in
	mov	a,m
	xri	80h		;reverse sense of bit
	ani	80h
	ret			;NZ if bit is NOT set
;
needed = .-patch		;space needed in bios/bdos holes
unused = patche+1-.
.ifl unused, [
	.prntx	"Overflow in patch area!"
	]
;

	.end
ctive
	ora	a
	jr