NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/memdump
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/io.i
*--------------------------------------
PAGE.LEN		.EQ 23
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPTR1			.BS 2
ZPPTR2			.BS 2
MEM.COUNT		.BS 1
USED.COUNT		.BS 1
LINE.COUNT		.BS 1
bSTOP			.BS 1

ZPReadAux		sta IO.SETREADAUX
				lda (ZPPTR1),y
				sta IO.CLRREADAUX
				rts
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 #64					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.MSG1X			.DA MSG1X
L.MSG2			.DA MSG2
L.MSG3			.DA MSG3
L.MSG.CRLF		.DA MSG.CRLF
				.DA 0
*--------------------------------------
CS.INIT			clc
CS.INIT.RTS		rts
*--------------------------------------
CS.RUN			ldx #ZPCodeLen-1

.1				lda ZPCode,x
				sta ZPReadAux,x
				dex
				bpl .1

				>LEA.G MemStat
				>SYSCALL GetMemStat

				jsr CS.RUN.INIT
*--------------------------------------
CS.RUN.LOOP		inc MEM.COUNT			skip slot 0

				jsr CS.RUN.CheckStop
				bcs CS.INIT.RTS

.1				lda (ZPPTR1)
				bpl .2

				inc USED.COUNT

.2				jsr CS.RUN.PRINTMEM
				lda ZPPTR1
				clc
				adc #S.MEM
				sta ZPPTR1
				bcc .3
				inc ZPPTR1+1

.3				lda MEM.COUNT
				>CMP.G MemStat+S.MSTAT.MLast
				bne CS.RUN.LOOP

				jsr CS.RUN.SUMMARY

				jsr CS.RUN.MSTAT

				jsr CS.RUN.PrintCRLF

				jsr CS.RUN.INIT
*--------------------------------------
CS.RUN.LOOPX	inc MEM.COUNT			skip slot 0

				jsr CS.RUN.CheckStop
				bcs CS.INIT.RTS

.1				ldy #S.MEM.F
				jsr ZPReadAux
				bpl .2

				inc USED.COUNT

.2				jsr CS.RUN.PRINTMEMX

				lda ZPPTR1
				clc
				adc #S.MEM
				sta ZPPTR1
				bcc .3
				inc ZPPTR1+1

.3				lda MEM.COUNT
				>CMP.G MemStat+S.MSTAT.XLast
				bne CS.RUN.LOOPX

				jsr CS.RUN.SUMMARY

				jsr CS.RUN.XSTAT

				lda #0
				sec
				rts
*--------------------------------------
CS.RUN.INIT		>LDYAI Mem.Table+S.MEM	skip slot 0
				>STYA ZPPTR1

				>PUSHW L.MSG0
				>PUSHBI 0
				jsr CS.RUN.PrintF

				stz MEM.COUNT
				stz USED.COUNT
				rts
*--------------------------------------
CS.RUN.SUMMARY 	jsr CS.RUN.PrintCRLF

				>PUSHW L.MSG2
				>PUSHB USED.COUNT
				>PUSHB MEM.COUNT
				>PUSHBI 2
				jmp CS.RUN.PrintF
*--------------------------------------
CS.RUN.MSTAT	jsr CS.RUN.PrintCRLF

				>PUSHW L.MSG3
				>PUSHW.G MemStat+S.MSTAT.MH
				>PUSHW.G MemStat+S.MSTAT.MF
				>PUSHW.G MemStat+S.MSTAT.ML
				>LDA.G MemStat+S.MSTAT.MF
				sec
				>SBC.G MemStat+S.MSTAT.ML
				pha
				>LDA.G MemStat+S.MSTAT.MF+1
				>SBC.G MemStat+S.MSTAT.ML+1
				ply
				>PUSHYA
				>PUSHBI 8
				jmp CS.RUN.PrintF
*--------------------------------------
CS.RUN.XSTAT	jsr CS.RUN.PrintCRLF

				>PUSHW L.MSG3
				>PUSHW.G MemStat+S.MSTAT.XH
				>PUSHW.G MemStat+S.MSTAT.XF
				>PUSHW.G MemStat+S.MSTAT.XL
				>LDA.G MemStat+S.MSTAT.XF
				sec
				>SBC.G MemStat+S.MSTAT.XL
				pha
				>LDA.G MemStat+S.MSTAT.XF+1
				>SBC.G MemStat+S.MSTAT.XL+1
				ply
				>PUSHYA
				>PUSHBI 8
				jmp CS.RUN.PrintF
*--------------------------------------
CS.RUN.PRINTMEM	>PUSHW L.MSG1

				>PUSHB MEM.COUNT

				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

				ldy #S.MEM.OWNERPID
				lda (ZPPTR1),y
				tax
				>PUSHA

				ldy #S.MEM.REFCNT
				lda (ZPPTR1),y
				>PUSHA

				ldy #S.MEM.PTR+1
				lda (ZPPTR1),y
				>PUSHA
				dey
				lda (ZPPTR1),y
				>PUSHA

				ldy #S.MEM.LEN+1
				lda (ZPPTR1),y
				>PUSHA
				dey
				lda (ZPPTR1),y
				>PUSHA

				lda (ZPPTR1)			Get flags
				bpl .12

				txa
				jsr CS.RUN.CHECKPS
				bcc .12

				jsr CS.RUN.PrintINV

.12				>PUSHBI 9

				>SYSCALL PrintF

				lda (ZPPTR1)
				and #S.MEM.F.CODE
				bne CS.RUN.PRINTMEM.BIN

CS.RUN.PRINTMEM.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				>PUSHW L.MSG1.STR
				tya
				beq .2					Starting with 0...PrintHEX

				>PUSHA					len
				>PUSHW ZPPTR2			String
				>PUSHBI 3

				jmp CS.RUN.PrintF

.2				>PUSHW L.MSG1.HEX

				ldy #0

.3				>PUSHB (ZPPTR2),y
				iny
				cpy #16
				bne .3

				>PUSHBI 16

				jmp CS.RUN.PrintF

CS.RUN.PRINTMEM.BIN
				>PUSHW L.MSG1.BIN

				ldy #S.MEM.BIN
				lda (ZPPTR1),y
				>SYSCALL GetMemPtr
				>PUSHYA
				>PUSHBI 2
				jmp CS.RUN.PrintF
*--------------------------------------
CS.RUN.PRINTMEMX
				>PUSHW L.MSG1X

				>PUSHB MEM.COUNT

				ldy #S.MEM.F			Get flags
				jsr ZPReadAux

				ldy #MEM.FLAGS+7
				ldx #7

.20				lsr
				pha
				lda #'-'
				bcc .30

				lda MSG.FLAGSX,x

.30				sta (pData),y
				pla
				dey
				dex
				bpl .20

				>PUSHEA.G MEM.FLAGS

				ldy #S.MEM.OWNERPID
				jsr ZPReadAux
				tax
				>PUSHA

				ldy #S.MEM.REFCNT
				jsr ZPReadAux
				>PUSHA

				ldy #S.MEM.PTR+1
				jsr ZPReadAux
				>PUSHA
				dey
				jsr ZPReadAux
				>PUSHA

				ldy #S.MEM.LEN+1
				jsr ZPReadAux
				>PUSHA
				dey
				jsr ZPReadAux
				>PUSHA

				ldy #S.MEM.F
				jsr ZPReadAux			Get flags
				bpl .12

				txa
				jsr CS.RUN.CHECKPS
				bcc .12

				jsr CS.RUN.PrintINV

.12				>PUSHBI 9

				jmp CS.RUN.PrintF
*--------------------------------------
CS.RUN.CHECKPS	ldx #0

.1				cmp PSs.PID,x
				beq .8

				inx
				cpx #K.PS.MAX
				bne .1

				rts						CS

.8				clc
				rts
*--------------------------------------
CS.RUN.CheckStop
				lda bSTOP
				beq .2

.1				>SLEEP

				>SYSCALL GetChar
				bcs .9

				cmp #3					Ctrl-C
				beq .9					CS

				stz bSTOP

				clc
				rts

.2				ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs .9

				tay
				bne .8					no char

				>SYSCALL GetChar
				bcs .9

				cmp #3					Ctrl-C
				beq .9					CS

				cmp #$13				Ctrl-S
				bne .8

				dec bSTOP

.8				clc
.9				rts
*--------------------------------------
CS.RUN.PrintINV	>PUSHW L.MSG1.INV
				>PUSHBI 0
				>SYSCALL PrintF
				rts
*--------------------------------------
CS.RUN.PrintCRLF
				>PUSHW L.MSG.CRLF
				>PUSHBI 0

CS.RUN.PrintF	>SYSCALL PrintF
				bcs .9

				inc LINE.COUNT
				lda LINE.COUNT
				cmp #PAGE.LEN
				bcc .8

				dec bSTOP
				stz LINE.COUNT
				clc
.8
.9				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			clc
				rts
*--------------------------------------
CS.END
ZPCode			sta IO.SETREADAUX
				lda (ZPPTR1),y
				sta IO.CLRREADAUX
				rts
ZPCodeLen		.EQ *-ZPCode
*--------------------------------------
MSG0			.CZ "hMem Flags    PID REF PTR  LEN   BINPATH/DATA\r\n"
MSG1.INV		.CZ "\e[7m"
MSG1			.CZ " %h  %s %3d %3d %H %5D "
MSG1.BIN		.CZ "\e[7m%s\e[0m\r\n"
MSG1.STR		.CZ "[%03d:%s]\e[0m\r\n"
MSG1.HEX		.CZ "?HEX:%h%h.%h%h.%h%h.%h%h.%h%h.%h%h.%h%h.%h%h\e[0m\r\n"
MSG1X			.CZ " %h  %s %3d %3d %H %5D\e[0m\r\n"
MSG2			.CZ "Allocated hMem:%d, Total:%d\r\n"
MSG3			.CS "High Memory: %H, "
				.CS "Free ULimit: %H, "
				.CS "Low : %H, "
				.CS "Free : %5D Bytes."
MSG.CRLF		.CZ "\r\n"
MSG.FLAGS		.CS "UZXAfcds"
MSG.FLAGSX		.CS "UZXAidep"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
MemStat			.BS S.MSTAT
MEM.FLAGS		.BS 9
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/memdump.s
ASM
