
; *********************************************************
; **
; ** MemInfo - An Avail replacement with added information
; **
; ** V1.0 - (14/08/96) by Henri Veisterä
; **
; *********************************************************

	opt	o+,d-,c+
	
	incdir dh0:devpac/include/
	include exec/memory.i
	include exec/execbase.i

CallEXEC MACRO
	move.l	_ExecBase(a4),a6
	jsr	_LVO\1(a6)
	ENDM

CallDOS MACRO
	move.l	_DOSBase(a4),a6
	jsr	_LVO\1(a6)
	ENDM

; ** Allocate and initialize work memory

	link	a4,#-work_SIZEOF
	move.l	sp,a4
	move.l	#work_SIZEOF/4-1,d0
zip	clr.l	(a4)+
	dbf	d0,zip
	move.l	sp,a4

; ** We need OS 2.0 (V36)

	move.l	$4.w,_ExecBase(a4)
	lea	dosname(pc),a1
	moveq	#36,d0
	CallEXEC OpenLibrary
	move.l	d0,_DOSBase(a4)
	bne.b	start
	add.l	#work_SIZEOF,a4
	unlk	a4
	moveq	#20,d0
	rts

start	CallDOS Output
	move.l	d0,outfile(a4)

; ** Read in the command line arguments

	lea	template(pc),a0
	move.l	a0,d1
	lea	array(a4),a0
	move.l	a0,d2
	moveq	#0,d3
	CallDOS ReadArgs
	tst.l	d0
	beq	cl_dos
	lea	array(a4),a0
	move.l	(a0)+,chip(a4)
	move.l	(a0)+,fast(a4)
	move.l	(a0)+,total(a4)
	move.l	(a0)+,flush(a4)
	move.l	d0,d1
	CallDOS FreeArgs

; ** Act according to given arguments

	tst.w	flush(a4)
	beq.b	noflush

; ** FLUSH memory
	
	moveq	#0,d2
allo	move.l	#$7FFFFFF0,d0
	moveq	#1,d1
	CallEXEC AllocVec
	move.l	d0,a1
	CallEXEC FreeVec
	addq.w	#1,d2
	cmp.w	#$A,d2
	bcs.b	allo

noflush	tst.w	chip(a4)
	beq.b	nochip

; ** Show TOTAL CHIP or FAST values

dofast	moveq	#0,d3
	move.l	_ExecBase(a4),a6
	lea	MemList(a6),a0
	move.l	LH_HEAD(a0),a2
cdoall	cmp.b	#NT_MEMORY,LN_TYPE(a2)
	bne.b	cnext
	tst.w	total(a4)
	bne.b	addit
	move.w	MH_ATTRIBUTES(a2),d0
	tst.w	fast(a4)
	beq.b	tchip
	btst	#MEMB_FAST,d0
	bra.s	test
tchip	btst	#MEMB_CHIP,d0
test	beq.b	cnext
addit	add.l	MH_FREE(a2),d3
cnext	move.l	LN_SUCC(a2),a2
	cmp.l	#0,a2
	bne.b	cdoall

	lea	outline2(a4),a5

	move.l	d3,d0
	moveq	#2,d1
	bsr	printn
	
	clr.b	(a5)+
	lea	outline2(a4),a0
	bsr	rprint
	bra	cl_dos

nochip	tst.w	fast(a4)
	bne.b	dofast
	
	tst.w	total(a4)
	bne.b	dofast
	
; ** Display the standard output

	lea	outline2(a4),a5

	lea	header(pc),a0
	bsr	rprint
	
; ** Go through memory list nodes

	move.l	_ExecBase(a4),a6
	lea	MemList(a6),a0
	move.l	LH_HEAD(a0),a2
doall	cmp.b	#NT_MEMORY,LN_TYPE(a2)
	bne	next
	move.l	MH_LOWER(a2),d0
	bsr	printhex
	move.l	MH_UPPER(a2),d0
	bsr	printhex

	move.b	LN_PRI(a2),d0
	ext.w	d0
	ext.l	d0
	moveq	#-1,d1
	bsr	printn

	move.l	MH_UPPER(a2),d0
	sub.l	MH_LOWER(a2),d0
	asr.l	#8,d0
	asr.l	#2,d0
	moveq	#0,d1
	add.l	d0,maximum(a4)
	bsr	printn

	move.l	MH_UPPER(a2),d0
	sub.l	MH_LOWER(a2),d0
	sub.l	MH_FREE(a2),d0
	asr.l	#8,d0
	asr.l	#2,d0
	moveq	#0,d1
	add.l	d0,inuse(a4)
	bsr	printn
	
	move.l	MH_FREE(a2),d0
	asr.l	#8,d0
	asr.l	#2,d0
	moveq	#0,d1
	add.l	d0,free(a4)
	bsr	printn

	lea	space(pc),a0
	bsr	print

	moveq	#0,d1
	move.l	MH_FIRST(a2),a0
getmax	cmp.l	MC_BYTES(a0),d1
	bge.b	nomax
	move.l	MC_BYTES(a0),d1
nomax	move.l	(a0),a0
	cmp.l	#0,a0
	bne.b	getmax
	move.l	d1,d0
	asr.l	#8,d0
	asr.l	#2,d0
	cmp.l	largest(a4),d0
	blt.b	tunne
	move.l	d0,largest(a4)
tunne	moveq	#0,d1
	bsr	printn
	
	move.w	MH_ATTRIBUTES(a2),d0
	btst	#MEMB_PUBLIC,d0
	beq.b	noze1
	move.b	#'P',(a5)+
	bra.s	yeze1
noze1	move.b	#'-',(a5)+
yeze1	btst	#MEMB_CHIP,d0
	beq.b	noze2
	move.b	#'C',(a5)+
	bra.s	yeze2
noze2	move.b	#'-',(a5)+
yeze2	btst	#MEMB_FAST,d0
	beq.b	noze3
	move.b	#'F',(a5)+
	bra.s	yeze3
noze3	move.b	#'-',(a5)+
yeze3	btst	#MEMB_LOCAL,d0
	beq.b	noze4
	move.b	#'L',(a5)+
	bra.s	yeze4
noze4	move.b	#'-',(a5)+
yeze4	btst	#MEMB_24BITDMA,d0
	beq.b	noze5
	move.b	#'D',(a5)+
	bra.s	yeze5
noze5	move.b	#'-',(a5)+
yeze5	btst	#MEMB_KICK,d0
	beq.b	noze6
	move.b	#'K',(a5)+
	bra.s	yeze6
noze6	move.b	#'-',(a5)+
yeze6	move.b	#' ',(a5)+

	move.l	LN_NAME(a2),a0
	bsr.s	print

	lea	return(pc),a0
	bsr.s	print

	move.b	#0,(a5)+
	lea	outline2(a4),a0
	bsr	rprint

	lea	outline2(a4),a5

next	move.l	LN_SUCC(a2),a2
	cmp.l	#0,a2
	bne	doall

; ** We are done, print out totals

	lea	totall(pc),a0
	bsr.s	print
	moveq	#0,d1
	move.l	maximum(a4),d0
	bsr	printn
	moveq	#0,d1
	move.l	inuse(a4),d0
	bsr	printn
	moveq	#0,d1
	move.l	free(a4),d0
	bsr	printn
	lea	space(pc),a0
	bsr.s	print
	moveq	#0,d1
	move.l	largest(a4),d0
	bsr	printn
	lea	return(pc),a0
	bsr.s	print
	move.b	#0,(a5)+
	lea	outline2(a4),a0
	bsr.s	rprint

; ** Exit the program
	
cl_dos	move.l	_DOSBase(a4),a1
	CallEXEC CloseLibrary
	add.l	#work_SIZEOF,a4
	unlk	a4
	moveq	#0,d0
	rts

; ** Subroutines to print out numbers, hex or fstrings

print	move.l	a0,-(sp)
nosep	tst.b	(a0)+
	bne.b	nosep
	sub.l	(sp),a0
	move.l	a0,d3
	subq.l	#2,d3
	cmp.l	#-1,d3
	bgt.b	dsd
	moveq	#0,d3
dsd	move.l	(sp)+,a1
	lea	outline2e(a4),a3
juku	cmp.l	a3,a5
	bgt.b	full
	move.b	(a1)+,(a5)+
	dbf	d3,juku
full	rts

rprint	move.l	a0,-(sp)
osep	tst.b	(a0)+
	bne.b	osep
	sub.l	(sp),a0
	move.l	a0,d3
	subq.l	#1,d3
	move.l	(sp)+,d2
	move.l	outfile(a4),d1
	CallDOS Write
	rts

printhex
	lea	outline(a4),a0
	addq.l	#8,a0
	moveq	#7,d2
	move.l	d0,d1
allnum	and.l	#$f,d0
	cmp.b	#10,d0
	bcs.b	numba
	sub.b	#10,d0
	add.b	#'A',d0
	bra.b	letta
numba	add.b	#'0',d0
letta	move.b	d0,-(a0)
	ror.l	#4,d1
	move.l	d1,d0
	dbf	d2,allnum
	move.b	#' ',8(a0)
	move.b	#0,9(a0)
	lea	outline(a4),a0
	bsr	print
	rts
	
printn	movem.l	a0-a3,-(sp)
	tst.w	d1
	beq.b	klo
	cmp.l	#2,d1
	bne.b	four
	lea	jlong(pc),a0
	bra.s	lo
four	lea	long(pc),a0
	bra.s	lo
klo	lea	longk(pc),a0
lo	lea	dstream(a4),a1
	move.l	d0,(a1)
	lea	putter(pc),a2
	lea	outline(a4),a3
	CallEXEC RawDoFmt
	lea	outline(a4),a0
	bsr	print
	movem.l	(sp)+,a0-a3
	rts

putter	move.b	d0,(a3)+
	rts

; ** Internal vars and buffers used

	STRUCTURE workmem,0
		LONG	_DOSBase
		LONG	_ExecBase
		LONG	outfile
		LONG	maximum
		LONG	inuse
		LONG	free
		LONG	largest
		LONG	chip
		LONG	fast
		LONG	total
		LONG	flush
		STRUCT	dstream,2*4
		STRUCT	array,4*4
		STRUCT	outline2,120
		LABEL	outline2e
		STRUCT	outline,12
		LABEL	work_SIZEOF

; ** Internal strings

dosname		dc.b	"dos.library",0
jlong		dc.b	"%ld",10,0
long		dc.b	"%4.4ld ",0
longk		dc.b	"%5.5ldk ",0
header		dc.b	"Lower    Upper     Pri    Max In-Use   Free Largest Attrib Name",10,0
totall		dc.b	10,"        total:         ",0
template	dc.b	"CHIP/S,FAST/S,TOTAL/S,FLUSH/S",0
version		dc.b	"$VER: MemInfo 1.0 (15.8.96)",10,13,0
return		dc.b	10,0
space		dc.b	" ",0
	
	end
