NEW
  PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF BIN/MEMDUMP
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/KERNEL.I
*--------------------------------------
PAGELEN			.EQ 22
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPTR1			.BS 2
ZPPTR2			.BS 2
ZPPSPtr			.BS 2
ZS.END
				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #32					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG0			.DA MSG0
L.MSG1.INV		.DA MSG1.INV
L.MSG1			.DA MSG1
L.MSG1.BIN		.DA MSG1.BIN
L.MSG1.STR		.DA MSG1.STR
L.MSG1.HEX		.DA MSG1.HEX
L.MSG2			.DA MSG2
L.MSG3			.DA MSG3
L.MSG4			.DA MSG4
L.MSG5			.DA MSG5
L.MSG.FREE		.DA MSG.FREE
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			>LEA.G MemStat
				>SYSCALL GetMemStat

				>LDYAI Mem.Table+S.MEM
				>STYA ZPPTR1

				>LDYA L.MSG0
				>SYSCALL puts
				
				>INC.G MEM.COUNT		skip slot 0

CS.RUN.LOOP		>LDA.G bSTOP
				beq .1
				
.10				>SYSCALL GetChar
				cmp #3					Ctrl-C ?
				beq CS.RUN.LOOP.RTS
				cmp #C.SPACE
				bne .10
				
				>STZ.G bSTOP
				
.1				lda (ZPPTR1)
				bpl .2
				
				>INC.G USED.COUNT
				
.2				jsr CS.RUN.PRINTMEM
				
				lda ZPPTR1
				clc
				adc #S.MEM
				sta ZPPTR1
				bcc .3
				
				inc ZPPTR1+1
				
.3				>INC.G MEM.COUNT
				>CMP.G MemStat+S.MSTAT.MLast
				beq CS.RUN.FOOTER
				
				bra CS.RUN.LOOP
				
				sec
CS.RUN.LOOP.RTS	rts
				
CS.RUN.FOOTER	>SYSCALL GetChar
				bcs CS.RUN.LOOP.RTS
				
				>PUSHB.G MEM.COUNT
				>PUSHB.G USED.COUNT
				>PUSHBI 2
				>LDYA L.MSG2
				>SYSCALL printf

				>PUSHW.G MemStat+S.MSTAT.MH
				>PUSHBI 2
				>LDYA L.MSG3
				>SYSCALL printf

				>PUSHW.G MemStat+S.MSTAT.MF
				>PUSHBI 2
				>LDYA L.MSG4
				>SYSCALL printf

				>PUSHW.G MemStat+S.MSTAT.ML
				>PUSHBI 2
				>LDYA L.MSG5
				>SYSCALL printf
				
				sec
				>LDA.G MemStat+S.MSTAT.MF
				>SBC.G MemStat+S.MSTAT.ML
				pha
				>LDA.G MemStat+S.MSTAT.MF+1
				>SBC.G MemStat+S.MSTAT.ML+1
				ply
				
				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.FREE
				>SYSCALL printf
				lda #0
				sec				
				rts
*--------------------------------------
CS.RUN.PRINTMEM	ldy #S.MEM.LEN+1

				ldx #4
				
.13				lda (ZPPTR1),y
				>PUSHA
				dey
				dex
				bne .13
				
				ldy #S.MEM.REFCNT
				lda (ZPPTR1),y
				>PUSHA
				
				ldy #S.MEM.OWNERPID
				lda (ZPPTR1),y
				>PUSHA
				
				lda (ZPPTR1)			Get flags
				
				ldy #MEM.FLAGS+7
				ldx #7
				
.20				lsr
				pha
				lda #'-'
				bcc .30
				lda MSG.FLAGS,x
				
.30				sta (pData),y
				pla
				dey
				dex
				bpl .20					
				
				>PUSHEA.G MEM.FLAGS				
				
				>PUSHB.G MEM.COUNT
				
				lda (ZPPTR1)			Get flags
				bpl .12
				
				ldy #S.MEM.OWNERPID
				lda (ZPPTR1),y
				jsr CS.RUN.CHECKPS
				bcc .12
				
				>PUSHBI 0
				>LDYA L.MSG1.INV
				>SYSCALL printf
	
.12				>PUSHBI 9
				>LDYA L.MSG1
				>SYSCALL printf
	
				lda (ZPPTR1)
				and #S.MEM.F.CODE
				bne CS.RUN.PRINTMEM.BIN
				
****** DATA				
				ldy #S.MEM.PTR
				lda (ZPPTR1),y
				sta ZPPTR2
				iny
				lda (ZPPTR1),y
				sta ZPPTR2+1
				
				ldy #$0
				
.1				lda (ZPPTR2),y			Check if it looks like a printable string
				beq .11
				bmi .2
				cmp #$20
				bcc .2					not printable....->hex
				iny
				bne .1

.11				tya
				beq .2					Startig with 0...PrintHEX
				
				>PUSHW ZPPTR2			String
				tya						Len
				>PUSHA
				>PUSHBI 3
				>LDYA L.MSG1.STR
				bra CS.RUN.PRINTMEM.OUT

.2				ldy #15

.3				>PUSHB (ZPPTR2),y
				dey
				bpl .3
				
				>PUSHBI 16
				>LDYA L.MSG1.HEX			
				bra CS.RUN.PRINTMEM.OUT
				
CS.RUN.PRINTMEM.BIN
				
				ldy #S.MEM.BIN
				lda (ZPPTR1),y
				>SYSCALL GetMemPtr
				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG1.BIN
				
CS.RUN.PRINTMEM.OUT
				>SYSCALL printf
				
				>INC.G LINE.COUNT
				cmp #PAGELEN
				beq .9
				rts
				
.9				lda #$FF
				>STA.G bSTOP
				
				inc
				>STA.G LINE.COUNT
				
				rts
*--------------------------------------
CS.RUN.CHECKPS	ldx #0
				
.1				cmp PS.Table.PID,x
				beq .8
				inx
				cpx K.PS.MAX
				bne .1
				rts						CS

.8				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			clc
				rts
*--------------------------------------
CS.END
MSG0			.AZ "hMem Flags    PID REF PTR   LEN   BINPATH/DATA"
MSG1.INV		.AZ "\e[7m"
MSG1			.AZ "$%h  %s %3d %3d $%H %5D "
MSG1.BIN		.AZ "{%s}\e[0m\r\n"
MSG1.STR		.AZ "[%03d:%s]\e[0m\r\n"
MSG1.HEX		.AZ "?HEX:%h%h.%h%h.%h%h.%h%h.%h%h.%h%h.%h%h.%h%h\e[0m\r\n"
MSG2			.AZ "\r\nAllocated hMem:%d, Total:%d\r\n"
MSG3			.AZ "High Memory:   $%H\r\n"
MSG4			.AZ "Free ULimit:   $%H\r\n"
MSG5			.AZ "Low Memory:    $%H\r\n"
MSG.FREE		.AZ "\r\nFree Memory:   %D Bytes.\r\n"
MSG.FLAGS		.AS "UZNAfcds"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
LINE.COUNT		.BS 1
MEM.COUNT		.BS 1
USED.COUNT		.BS 1
bSTOP			.BS 1
MemStat			.BS S.MSTAT
MEM.FLAGS		.BS 9
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/MEMDUMP.S
ASM
