	title	Free User Area Checker

; This small progam simply looks for free user areas
; on a given drive.
; Call it:
;	FREE drv:

; Note:	It is written for 8080 instructions only
;	It automatically detects CP/M version 1.x, 2.x or 3.x

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg

;		Phone +49 40 4223247

;		Version 1.0, June 1998

OS	equ	0000h		; Warm start of CP/M
BDOS	equ	0005h		; CP/M system call
TPAtop	equ	BDOS+1		; Top of TPA memory
FCB	equ	005ch		; Standard file control block

.string	equ	 9		; Put string to console
.vers	equ	12		; Get OS version
.srcfrs	equ	17		; Search for first file entry
.usrcod	equ	32		; Select user code

tab	equ	09h
lf	equ	0ah
cr	equ	0dh
eot	equ	'$'

MAXUSR	equ	16		; Max user in CP/M 3

	ld	a,(FCB)
	ld	($FCB),a	; Set drive
	or	a		; Test any
	jp	z,Help		; .. nope, so give help
	add	a,'A'-1
	ld	($DRV),a	; Set ASCII
	ld	hl,(TPAtop)
	ld	sp,hl		; Get stack
	ld	c,.vers
	call	BDOS		; Get OS version
	ld	a,l
	cp	20h		; Test early version
	ld	de,$NOUSR
	jp	c,..string	; .. tell it
	ld	hl,'61'
	ld	($TOT.USR),hl
	ld	c,0		; Init user area
	ld	b,MAXUSR	; .. max areas
	cp	30h		; Test CP/M PLUS
	jp	nc,USR.LOOP
	ld	b,2*MAXUSR	; Double it for CP/M 2.x
	ld	hl,'23'
	ld	($TOT.USR),hl
USR.LOOP:
	push	bc
	push	hl
	ld	e,c
	ld	c,.usrcod
	call	BDOS		; Set user area
	ld	de,$USR
	call	string		; Give info
	ld	de,$FCB
	ld	c,.srcfrs
	call	BDOS		; Search for file in this area
	pop	hl
	inc	a		; -1 is no file
	ld	de,$EMPTY
	ld	hl,$EMPN
	jp	z,Empty		; .. empty
	ld	de,$OCCU
	ld	hl,$OCCN
Empty:
	call	string
	call	incASCII
	ld	hl,$USRN
	call	incASCII
	pop	bc
	inc	c		; Next user
	dec	b
	jp	nz,USR.LOOP
	ld	a,($DRV)
	ld	($STA.DRV),a
	ld	de,$STAT	; Give statistic
	jp	..string
Help:
	ld	de,$HELP
..string:
	call	string		; Give help
	jp	OS
;
; Print string on console
;
string:
	push	bc
	push	de
	push	hl
	ld	c,.string
	call	BDOS		; .. print
	pop	hl
	pop	de
	pop	bc
	ret
;
; Increment ASCII value
;
incASCII:
	inc	(hl)		; Bump user
	ld	a,(hl)
	cp	'9'+1		; Test overflow
	ret	nz		; .. nope
	ld	(hl),'0'	; Init units
	dec	hl
	ld	a,(hl)		; Test empty tens
	cp	' '
	jp	z,set.ten
	inc	(hl)		; Bump tens
	ret
set.ten:
	ld	(hl),'1'	; Init tens
	ret
	
$FCB:
	db	0,'???????????'
	ds	24
$NOUSR:
	db	'CP/M 1.x does not support user areas'
	db	cr,lf,eot
$HELP:
	db	'This small progam simply looks for free user areas'
	db	cr,lf
	db	'on a given drive.'
	db	cr,lf
	db	'Call it:'
	db	cr,lf
	db	tab,'FREE drv:'
	db	cr,lf,eot
$USR:
	db	'User Area  '
$USRN:
	db	'0 of drive '
$DRV:
	db	'x: ',eot
$OCCU:
	db	'occupied'
	db	cr,lf,eot
$EMPTY:
	db	'empty'
	db	cr,lf,eot
$STAT:
	db	cr,lf
	db	'On drive '
$STA.DRV:
	db	'x: are  '
$EMPN:
	db	'0 area(s) free and  '
$OCCN:
	db	'0 area(s) occupied (out of '
$TOT.USR:
	db	'xx)'
	db	cr,lf,eot

	end

