	.TITLE	'Sorted directory map program 2.1'
;
;
;15/08/82 Converted for use with MAC by Steven Engel.
;
;
	ORG	0100H

BDOS	EQU	5		;bdos call address
FCB	EQU	05CH		;default fcb
;
	JMP	START
;
	DB	'FMAP version 2.1 as of 15/08/82'
;
;	GOOD FOR DIRECTORIES TO 256 ENTRIES
;
START:	LXI	H,0		;0 offset for stack
	DAD	SP		;add to stack pointer
	SHLD	STACK		;and save that spack
	LXI	SP,STACK	;make a new stack
	LDA	FCB+16		;2ND UNIT PARAM.
	ORA	A
	JZ	NODEST
	STA	MYFCB		;CHG OUTPUT DRIVE
NODEST: LDA	FCB+17
	STA	FILESW		;FLAG FOR FILE WRITE
	MVI	C,25		;FIND LOGGED DISK
	CALL	BDOS
	STA	CDSK
	LDA	FCB
	ORA	A
	JNZ	FSPEC2		;NO DRIVE SPEC'D
	LDA	CDSK
	INR	A
FSPEC2: DCR	A		;ADJ FOR INPUT RNG
	MOV	E,A
	MVI	C,14		;SELECT DRIVE WANTED
	CALL	BDOS
	LXI	H,FCB
	MVI	M,'?'		;MATCH ALL ENTRIES
	MVI	C,27		;GET ALLOC VECT
	CALL	BDOS
	SHLD	ALLADR		;SAVE ALLOC PNTR.
	MVI	C,12		;FIND REVISION
	CALL	BDOS
	ORA	A
	JZ	OLDCPM		;BEFORE 2.0 USE DFLTS
	MVI	C,31		;GET DPB ADDRESS
	CALL	BDOS
	INX	H
	INX	H
	INX	H
	MOV	A,M		;GET BLM
	INR	A		;MAKE SECTORS/BLOCK
	RRC
	RRC
	RRC			;DIVIDE BY 8 FOR KB/BLK
	ANI	1FH
	STA	BLKSIZ		;SIZE IN KBYTES
	INX	H
	MOV	A,M		;EXTENT MASK
	STA	EXTMSK
	LDA	BLKSIZ		;RESTORE SIZE
	INX	H
	MOV	E,M
	INX	H
	MOV	D,M		;DSM
	XCHG
	INX	H		;TOTAL BLOCK COUNT
	SHLD	BLKCNT
;
OLDCPM: MVI	C,17		;SRCH FIRST
	LXI	D,FCB
	CALL	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H
	SHLD	COUNT
	MOV	A,H
	ORA	L
	POP	H
	JNZ	ENTRY
;
DFEXIT: MVI	B,2
	LXI	H,ROOM1
	CALL	TYPEIT
	LHLD	BLKCNT
	MOV	D,H
	MOV	E,L		;TOTAL COUNTER IN DE
	PUSH	H		;LEFT COUNTER ON STK
	LHLD	ALLADR
LP3:	MVI	B,8		;BIT COUNT
	MOV	A,M		;ALLOC BYTE
	XTHL			;SAVE PTR,GET LEFT CNT
LP2:	RLC			;BIT TO CY
	JNC	LP1		;UNUSED BLK
	DCX	H		;COUNT OFF
LP1:	DCX	D		;DECR TOTAL
	MOV	C,A
	MOV	A,D
	ORA	E
	JZ	CNTDUN		;FINISHED SCAN
	MOV	A,C		;RESTORE
	DCR	B
	JNZ	LP2
	XTHL
	INX	H		;STEP TBL PTR
	JMP	LP3
;
CNTDUN: XCHG			;COUNT IN DE
	POP	H		;CLEAR UP STACK
	LDA	BLKSIZ
	CALL	MPY8		;CALC IN KB
	CALL	OUTNUM
	MVI	B,8
	LXI	H,ROOM2
	CALL	TYPEIT
	CALL	FILECR
	CALL	CR
	LDA	NFILE
	MOV	L,A
	MVI	H,0
	CALL	OUTNUM
	LXI	D,NMSG
	CALL	WRCON
	LDA	FILESW
	ANI	0FEH
	CPI	'F'		;46H
	STA	FILESW
	JNZ	EXIT
	MVI	A,1AH
	CALL	FILCHR
	CALL	WRSEC
	LXI	D,MYFCB
	MVI	C,10H
	CALL	BDOS
	JMP	EXIT
;
NMSG:	DB	' Direct. Entries used$'
;
NFEXIT: XRA	A
	STA	FILESW
	JMP	DFEXIT
;
DIVIDE: XRA	A
	MVI	B,10H
DIV01:	DAD	H
	RAL
	CMP	C
	JC	DIV02
	SUB	C
	INX	H
DIV02:	DCR	B
	JNZ	DIV01
	RET
;
MPY8:	LXI	H,0
MPY81:	ORA	A
	RZ
	RAR
	JNC	MPY01
	DAD	D
MPY01:	XCHG
	DAD	H
	XCHG
	JMP	MPY81
;
OUTNUM: MVI	D,0
	MVI	C,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