include	clib.inc
include	iost.inc
include	conio.inc
include	string.inc
include	stdio.inc
include	malloc.inc
include	stdlib.inc

	extrn	IDD_DZMemory:DWORD

LINESIZE	= 64
MAXLINES	= 64
ID_COUNT 	= 11

S_MCB		STRUC
mcb_flag	db ?
mcb_owner	dw ?
mcb_size	dw ?
mcb_unused	db 3 dup(?)
mcb_name	db 8 dup(?)
S_MCB		ENDS

_DATA	SEGMENT
format	db '%05lX          %7lu byte  %04X',0
formatz	db '%8lu',0
format_9lu db '%10lu',0
o_list	dw ?
dialog	label dword
dlg_ax	dw ?
dlg_dx	dw ?
_DATA	ENDS

_TEXT	SEGMENT

normalize PROC
	mov	cl,dh
	shl	dx,4
	add	ax,dx
	adc	cl,0
	shr	cl,4
	mov	dl,cl
	mov	dh,0
	ret
normalize ENDP

readmem PROC
	push	si
	push	di
	push	bp
	mov	bx,o_list
	mov	[bx].ll_count,0
	mov	si,word ptr [bx].ll_list
	mov	ah,52h
	int	21h
	mov	di,es:[bx-2]	; segment of first memory control block
    readmem_loop:
	mov	es,di
	xor	ax,ax
	mov	bx,ax
	mov	cx,es:[bx].mcb_owner
	push	cx
	mov	dx,es:[bx].mcb_size
	call	normalize
	push	dx
	push	ax
	mov	dx,di
	xor	ax,ax
	call	normalize
	push	dx
	push	ax
	mov	ax,offset format
	mov	bx,o_list
	mov	dx,word ptr [bx].ll_list+2
	mov	bx,si
	call	@sprintf
	add	sp,10
	mov	bx,o_list
	inc	[bx].ll_count
	cmp	[bx].ll_count,MAXLINES
	je	readmem_end
	xor	bx,bx
	mov	ax,es:[bx].mcb_owner
	mov	bx,o_list
	mov	dx,word ptr [bx].ll_list+2
	dec	ax
	cmp	ax,di
	je	readmem_copy
	inc	ax
	jz	readmem_free
	cmp	ax,8
	je	readmem_dos
	cmp	ax,6
	je	readmem_next
	cmp	ax,7
	je	readmem_next
	cmp	ax,0FFF7h
	jb	readmem_next
	mov	es,dx
	mov	ax,'83'
	mov	es:[si+7],ax
	mov	ax,'M6'
	mov	es:[si+9],ax
	mov	ax,'XA'
	mov	es:[si+11],ax
	jmp	readmem_next
    readmem_free:
	mov	es,dx
	mov	ax,'RF'
	mov	es:[si+7],ax
	mov	ax,'EE'
	mov	es:[si+9],ax
	jmp	readmem_next
    readmem_dos:
	mov	es,dx
	mov	ax,'OD'
	mov	es:[si+7],ax
	mov	al,'S'
	mov	es:[si+9],al
	jmp	readmem_next
    readmem_copy:
	mov	cx,8
	mov	ax,word ptr [bx].ll_list+2
	mov	es,di
	mov	bx,cx
	mov	dx,ds
	mov	ds,ax
	push	si
	add	si,7
    readmem_cpyl:
	mov	al,es:[bx]
	inc	bx
	or	al,al
	jz	readmem_endc
	mov	[si],al
	inc	si
	dec	cx
	jnz	readmem_cpyl
    readmem_endc:
	pop	si
	mov	ds,dx
    readmem_next:
	add	si,LINESIZE
	mov	es,di
	xor	bx,bx
	add	di,es:[bx].mcb_size
	inc	di
	mov	es,di
	mov	al,es:[bx].mcb_flag
	cmp	al,4Dh
	je	readmem_loop
    readmem_end:
	pop	bp
	pop	di
	pop	si
	ret
readmem ENDP

init_list PROC
	push	si
	push	di
	push	bp
	push	ds
	cld
	mov	cx,ID_COUNT
	mov	bx,o_list
	mov	[bx].ll_numcel,0
	mov	ax,LINESIZE
	mul	[bx].ll_index
	add	ax,word ptr [bx].ll_list
	mov	dx,word ptr [bx].ll_list+2
	mov	bp,bx
	les	di,dialog
	lea	di,[di+16].to_data	; &object[0].data
	mov	ds,dx			; DS:SI to list
	mov	si,ax
    event_list_loop:
	mov	ax,7F80h
	or	es:[di-7],al		; object.flag to unused
	cmp	byte ptr [si],0		; entry in use ?
	jz	event_list_next
	and	es:[di-7],ah
	inc	[bp].ll_numcel
    event_list_next:
	mov	ax,si			; store pointer
	stosw
	mov	ax,ds
	stosw
	add	di,12			; next object
	add	si,LINESIZE		; next info line
	dec	cx
	jnz	event_list_loop
	pop	ds
	pop	bp
	pop	di
	pop	si
	ret
init_list ENDP

event_list PROC DIST
	call	init_list
	invoke	dlinit, dialog
	mov	ax,_C_NORMAL
	ret
event_list ENDP

_TEXT	ENDS

PPROC	cmmemory
local	list:S_LOBJ
	push	si
	push	di
	lea	ax,list
	mov	o_list,ax
	invoke	malloc, LINESIZE * MAXLINES
	jz	cmmemory_ernomem
	stom	list.ll_list	; pointer to list buffer
	invoke	memzero, dx::ax, LINESIZE * MAXLINES
	invoke	rsopen, IDD_DZMemory
	jz	cmmemory_free
	stom	dialog
	invoke	dlshow, dx::ax
	fpmov	list.ll_proc,event_list
	xor	ax,ax
	mov	list.ll_dcount,ID_COUNT	; number of cells (max)
	mov	list.ll_celoff,ID_COUNT	; cell offset
	mov	list.ll_dlgoff,ax	; start index in dialog
	mov	list.ll_numcel,ax	; number of visible cells
	mov	list.ll_count,ax	; total number of items in list
	mov	list.ll_index,ax	; index in list buffer
	call	readmem
	call	event_list
	int	12h
	mov	dx,1024
	mul	dx
	push	dx
	push	ax
	mov	bx,word ptr IDD_DZMemory
	mov	bx,[bx+6]
	add	bx,0E04h
	xor	cx,cx
	mov	dx,ds
	mov	ax,offset formatz
	call	@scputf
	mov	dx,_psp
	add	dx,2
	xor	ax,ax
	call	normalize
	push	dx
	push	ax
	inc	bh
	xor	cx,cx
	mov	ax,offset formatz
	call	@scputf
	add	sp,8
	mov	di,bx
	.if func(emminit)
	    mov     ah,42h
	    int     67h
	    mov     si,dx
	    mov     ax,4000h
	    mul     si		; total number of pages
	    push    dx
	    push    ax
	    mov     ax,4000h
	    mul     bx		; number of unallocated pages
	    pop	    bx
	    pop     cx
	    sub     bx,ax
	    sbb     cx,dx
	    push    cx
	    push    bx
	    add     ax,bx
	    adc     dx,cx
	    push    dx
	    push    ax
	    mov     bx,di
	    add     bh,1
	    sub     bl,2
	    sub     cx,cx
	    mov     ax,offset format_9lu
	    call    @scputf
	    add     sp,4
	    inc     bh
	    mov     ax,offset format_9lu
	    call    @scputf
	    add     sp,4
	.endif
	invoke	dllevent,dialog,addr list
	invoke	dlclose,dialog
    cmmemory_free:
	invoke	free,list.ll_list
    cmmemory_toend:
	pop	di
	pop	si
	ret
    cmmemory_ernomem:
	xor	ax,ax
	jmp	cmmemory_toend
PEND	cmmemory

	END
