;
; $VER: DFree.a V1.2 recoded in 100% assembly
;
; Started: 03/07/1996 (month/day/year)
;
; Original by Max Francis, coded in C (6200 bytes!)
; USAGE: dfree <device/name>
;
; 100% assembly recoded version by Mauro Panigada (FREEWARE)
; USAGE: dfree <device/name>
;
; Added ReadArgs parser with: DEVICE/A
; Shorter than previous my own assembly version and better:
; autorelocable and can be a resident program (pure).
;
; STORY: DFree V1.0 (02/12/1996) simply a remake from C version.
;        DFree V1.2 (03/07/1996) see before.
;
; GOOD QUALITY: Differently from shorter DSpace V1.2 (freeware), DFree
; works with 68000 (no odd addresses, and uses utility.library for
; 32bit operations and not the 68020 mulu.l istruction), and it
; calculate the best method to rapresent the free space (bytes, Kilo or
; Mega). Is it enough?
; It is now PURE, you can make it RESIDENT!
;

		incdir	"dh0:progr/assem/include/"
		include	"exec/types.i"
		include	"exec/memory.i"
		include	"exec/libraries.i"
		include	"exec/exec_lib.i"
		include	"dos/dos.i"
		include	"dos/dosextens.i"
		include	"dos/dos_lib.i"
		include	"utility/utility_lib.i"


		bra.s	start

		dc.b	"$VER: DFree V1.2 recoded in 100% assembly "
		dc.b	"by Mauro Panigada",0
		even

LIBVERSION	EQU	37

dosbase		EQU	0
utilbase	EQU	dosbase+4
rdargs		EQU	utilbase+4
array		EQU	rdargs+4
sarg		EQU	array+4
targ		EQU	sarg+4
idstruct	EQU	targ+4
myarg		EQU	idstruct+id_SIZEOF
VARSLEN		EQU	myarg+4

start		moveq	#0,d7
		move.l	4.w,a6
		moveq	#VARSLEN,d0
		move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
		jsr	_LVOAllocVec(a6)
		move.l	d0,a5
		tst.l	d0
		beq	exit0
		lea	dosname(pc),a1
		moveq	#LIBVERSION,d0
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,dosbase(a5)
		beq	exit
vaicosi		lea	utilname(pc),a1
		moveq	#LIBVERSION,d0
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,utilbase(a5)
		beq	exit
		move.l	dosbase(a5),a6
		lea	linepars(pc),a0
		move.l	a0,d1
		lea	array(a5),a0
		move.l	a0,d2
		moveq	#0,d3
		jsr	_LVOReadArgs(a6)
		move.l	d0,rdargs(a5)
		beq	helpscreen
		move.l	array(a5),d0
		beq	helpscreen
		move.l	array(a5),d1
		moveq	#ACCESS_READ,d2
		jsr	_LVOLock(a6)
		move.l	d0,d7
		beq	lockerror
		lea	idstruct(a5),a0
		move.l	a0,d2
		move.l	d0,d1
		jsr	_LVOInfo(a6)
		tst.l	d0
		beq	infoerror
		lea	idstruct(a5),a4
		move.l	id_NumBlocks(a4),d3
		move.l	id_NumBlocksUsed(a4),d4
		move.l	id_BytesPerBlock(a4),d5
		sub.l	d4,d3
		move.l	utilbase(a5),a6
		move.l	d3,d0
		move.l	d5,d1
		jsr	_LVOUMult32(a6)
		move.l	dosbase(a5),a6
		move.l	d0,d6
		lea	targ(a5),a0
		cmp.l	#(5*1048576),d6
		bhi	megafree
		cmp.l	#1024,d6
		blo	bytefree
kappafree	lea	kappa.msg(pc),a1
		move.l	a1,(a0)
		move.l	d6,d0
		move.l	#1024,d1
		bra.s	shwrite0
bytefree	lea	byte.msg(pc),a1
		move.l	a1,(a0)
		bra.s	shwrite
megafree	lea	mega.msg(pc),a1
		move.l	a1,(a0)
		move.l	#1048576,d1
shwrite0	move.l	utilbase(a5),a6
		move.l	d6,d0
		jsr	_LVOUDivMod32(a6)
		move.l	d0,d6
		move.l	dosbase(a5),a6
shwrite		lea	sarg(a5),a0
		move.l	d6,(a0)
		lea	string(pc),a0
		move.l	a0,d1
		lea	array(a5),a0
		move.l	a0,d2
		jsr	_LVOVPrintf(a6)
exit		move.l	d7,d1
		beq.s	closenext1
		jsr	_LVOUnLock(a6)
closenext1	move.l	rdargs(a5),d1
		beq.s	closenext2
		jsr	_LVOFreeArgs(a6)
closenext2	move.l	dosbase(a6),a1
		movea.l	4.w,a6
		jsr	_LVOCloseLibrary(a6)
		move.l	utilbase(a6),a1
		jsr	_LVOCloseLibrary(a6)
		move.l	a5,a1
		jsr	_LVOFreeVec(a6)
exit0		rts

helpscreen	lea	help.msg(pc),a0
		lea	myarg(a5),a1
		lea	arg.msg(pc),a2
		move.l	a2,(a1)
		move.l	a1,d2
writerror	move.l	a0,d1
		jsr	_LVOVPrintf(a6)
		bra	exit

lockerror	lea	lock.msg(pc),a0
		lea	array(a5),a1
		move.l	a1,d2
		bra.s	writerror

infoerror	lea	info.msg(pc),a0
		lea	array(a5),a1
		move.l	a1,d2
		bra.s	writerror

;--------------------------------------------------------------------------
dosname		dc.b	"dos.library",0
		even
utilname	dc.b	"utility.library",0
		even
help.msg	dc.b	"DFree V1.2 recoded in 100%% assembly"
		dc.b	" by Mauro Panigada (03/07/1996)",10
		dc.b	"Original version coded in C by Max Francis",10
		dc.b	"USAGE: %s <DEVICE/DISK>",10
		dc.b	"Show free disk space on DEVICE or DISK "
		dc.b	"specified.",10,0
		even
lock.msg	dc.b	"Can't lock %s",10,0
		even
info.msg	dc.b	"Can't examine %s",10,0
		even
arg.msg		dc.b	"DFree",0
		even
string		dc.b	"%s %ld%s free.",10,0
		even
byte.msg	dc.b	" bytes",0
		even
mega.msg	dc.b	"M",0
kappa.msg	dc.b	"K",0
linepars	dc.b	"DEVICE/A",0

		END