; 							>B/S<
;	NEW FUNCTIONS - CP/M 2, CDOS, SDOS
;
; 02.20 KRS 07/??/80 ORIGINAL VERSION
; 02.35 dew 10/??/81 macros added
; 02.37 krs 02/??/82 get cp/m dpb
; 02.39 sbw 06/??/82 Fix DRSCX bug
; 02.40 sbw 08/05/82 Fix Dismount, CP/M DPB
; 02.42 sbw 12/13/82 17/18 Call consistency
;       sbw 01/28/83 edit
;
	icl	pdef		;02.39
	icl	df		;02.39
	df.dos			;02.39
	df.cpu			;02.39
	df.sys			;02.39
;
	NAM	DR3242
;
	ENT	SETBOT
	ENT	MNTA0,MNTC2,DMTA1,DMTC3
	ent	getdpb					;02.37
;
	ext	gtsdt,cducb,gtducb,div32			;02.40
	ext	setlog,XGETAT,cksvec,CLRDK,FLSALL	;02.37	;02.40
	EXT	SYSBOT,SYSOLD,MOVE				;02.42
	EXT	SAVAF,FOPN
	EXT	ASDTE,DSKBIT,ABIT,LOGDK			;02.39
	EXT	FREALL,DSKFN				;02.37
;
    ifc SYSUCB=SINGLE					;02.35
	EXT	?UDBF,?UHL,?udev
	ext	?ufcb,?ufcb1,?ufcb33,?udrb			;02.42
     elsec
	EXT	RDUCB,SHLRG
    endc
;
	EJECT
SETBOT:		;SET NEW SYSTEM BOTTOM
;
	MOV	A,E	; E=HIGH BYTE OF NEW ADDRESS
	LHLD	SYSBOT	; CURRENT BOTTOM
	CMP	H	; IS NEW HIGHER?
	BRGE	SETORG	; YES, RESET		;02.35
	SHLD	SYSOLD	; SAVE CURRENT AS OLD
	XCHG
	MOV	H,L
	MVI	L,0	; SET NEW SYSBOT
	SHLD	SYSBOT
	lxi	b,9	; MOVE					;02.40
	call	move	; JUMPS TO				;02.40
;			; NEW BOTTOM				;02.40
	BR	SETRST				;02.35
;
SETORG:	LHLD	SYSOLD
	SHLD	SYSBOT
SETRST:	MVI	A,0C3H
	STA	5
	LHLD	SYSBOT
	SHLD	6
	RET
	EJECT
;
	EJECT
MNTA0:
MNTC2:		; MOUNT DISK CALLS
;
; E = DISK (0=A:, 1=B:, etc.),					;02.42
; D = CHKSUM IF SUBDIR!!!
;
; RETURNS	  A = 0 IF OK, -1 IF PREVIOUS DISK ALREADY
;		      MOUNTED, -2 IF DISK I/O ERROR
;
	CALL	TSTMNT	; TEST IF ALREADY LOGGED IN
	BRZ	DOMNT					;02.35
errdmt:	MVI	A,-1	; ALREADY
	JMP	SAVAF	; AND RETURN TO CALLER
;
DOMNT:
	CALL	LOGDK	; LOG IN NEW ONE
GOODRT:	XRA	A	; CAN ONLY RETURN IF GOOD!
	JMP	SAVAF	; AND RETURN TO CALLER
;
TSTMNT:	MOV	B,E
	PUSH	B
	push	d		;02.37
	CALL	ASDTE
	CALL	DSKBIT
	CALL	ABIT
	ANA	M	; TEST LOGGED IN ALREADY
	pop	d		;02.37
	POP	B
	RET
;
DMTA1:
DMTC3:		; DISMOUNT DISK CALLS
;
; E = DISK (0=A:, 1=B:, etc.),					;02.42
;
; RETURNS	  A = 0 IF OK, (always ok as of 02.40)
;
	CALL	TSTMNT	; SEE IF LOGGED IN
	MVI	A,1	; SAY USER 0				;02.40
	brz	..1	; ERROR Return			;02.37	;02.40
	CALL	CLRDK		;02.37
	xra	a						;02.40
..1:								;02.40
	jmp	savaf						;02.40
;
	eject
;
;	cp/m disk descriptor code added at	 02.37
;
dpbcpm:	dw	26	; # sec/track				;02.40
	db	3,7,0	; tells cluster size, etc.
	dw	242	; number of clusters -1
	dw	63	; number of dir entries -1
	dw	-1	; fake allocation bits
	dw	0	; cksum records				;02.42
dpbrsv:	ds	2	; reserved tracks			;02.42
;
dbp00:	db	3,7,0		; 1k
	db	4,15,1		; 2k
	db	5,31,3		; 4k
	db	6,63,7		; 8k
;
getdpb:	lxi	h,dpbcpm	; Return the CP/M style parameter block
	shlucb	hl
;
	lhlucb	dev
	mov	b,l		; Get the current disk
	call	logdk		; Ensure it is logged		;02.40
;
;****	Added 02.40	********************************************
;
	mvi	l,sdtdub	; Set the current DUB pointer
	call	gtsdt
	mov	e,m
	inx	h
	mov	d,m
	xchg
	shld	cducb
;
	lxi	d,dubtrk	; Get the DUB sectors per track
	dad	d
	mov	l,m		; into HL
	mvi	h,0
	shld	dpbcpm		; Make CP/M DPB agree
;
	push	h		; Save for calculation of reserved tracks
;
;****	End of 02.40 addition	************************************
;
	mvi	l,sdtcsz					;02.40
	call	gtsdt						;02.40
	mov	a,m		; get cluster size
	lxi	h,dbp00-3	; Look up CP/M DPB values for	;02.42
	lxi	d,3		;	clustersize, etc.
;
	rrc			; Calculate clustersize/8	;02.42
	rrc							;02.42
	rrc							;02.42
  .do								;02.42
	dad	d		; Add 3 times log(2) of		;02.42
	rrc			;	clustersize/8		;02.42
	brnc	.do						;02.42
  .until							;02.42
;
	xchg			; de points to values		;02.40
	lxi	h,dpbcpm	; hl points into DPB		;02.40
	inx	h						;02.40
	inx	h						;02.40
	lxi	b,3		; Length of move		;02.40
	call	move						;02.40
	xchg			; de points into DPB		;02.40
;
	mvi	l,sdtncl	; Get number of clusters	;02.40
	call	gtsdt						;02.40
	mov	c,m
	inx	h
	mov	b,m
;
;
	dcx	b		; CP/M DPB expects (# clusters) - 1
	xchg
	mov	m,c
	inx	h
	mov	m,b
	inx	h
	xchg
;
	mvi	l,sdtmln	; Get directory entries		;02.40
	call	gtsdt						;02.40
	mov	a,m
	dcr	a		; CP/M DPB expects entries - 1
	stax	d
;
;****	Added 02.40	*******************************************
;
; Set the number of reserved tracks in the CP/M DPB.
;
; If DUBSEC==1 (i.e. old style DUB, zero single density blocks) then
;
;	(Reserved tracks) := (Directory start)/(Blocks per track)
;
; else
;
;	(Reserved tracks) := 1 + ((Dir start) - (SD blocks)) / (Blocks/Track)
;
	mvi	l,sdtmdr	; Get master directory start
	call	gtsdt
	mov	e,m		; into DE
	inx	h
	mov	d,m
;
	mvi	l,dubsec	; Get DUB single density sectors
	call	gtducb
	mov	a,m
	xchg			; (HL := directory start)
	pop	b		; (BC := Sectors per track)
	cpi	1		; Zero single density sectors (old style)?
	brz	..1
;
	cma			; Non-zero single density sectors:
	mov	e,a		; DE := -(Single density sectors)
	mvi	d,-1
	inx	d
	dad	d		; HL := (Dir start) - (SD sectors)
	dad	b		; (Add in one full track)
;
..1:
	lxi	d,0		; (Complete 32 bit number)
	call	div32		; DE := HL/BC
	xchg			; HL := # reserved tracks
	shld	dpbrsv		; To CP/M DPB
;
;****	End of 02.40 addition	***********************************
;
	ret
;
	END
