
	
.equ MemOff 10

;Usage:
;        jsr MemDump
;        word $CEED      ;Address
;        byte $3          ;Lines

MemDump:
		php
		Mode16bit
			pha
			phy	
			phx 
			phd
			
			lda #0
			tad
			
		Mode8bit	
                ;jsr Monitor
                tsx
                lda SPpage+MemOff,x
                sta t_RetAddrL
				
				;jsr Monitor
				
                ;jsr Monitor
                lda SPpage+MemOff+1,x
                sta t_RetAddrH
				
                ;jsr Monitor

                ldy #1
                lda (t_RetAddr),Y
                sta t_MemdumpL
                ;jsr Monitor
        
                iny
                lda (t_RetAddr),Y
                sta t_MemdumpH
				iny
                lda (t_RetAddr),Y
                sta t_MemdumpU
				
                ;jsr Monitor
        
                iny
                lda (t_RetAddr),Y
                
                tax  
                jsr MemDumpDirectB	;Get Xpos

                tsx
                inc SPpage+MemOff,x
                inc SPpage+MemOff,x
                inc SPpage+MemOff,x
				inc SPpage+MemOff,x
				
				
                lda SPpage+MemOff,x
                cmp #3
                bcs MemDump_NoIncSpH
				inc SPpage+MemOff+1,x
MemDump_NoIncSpH
	Mode16bit
			pld
			plx 
			ply	
			pla
			plp
		.ACCU 8;assume M:1	;Accumulator 8 bit
		.INDEX 8 ;assume X:1	;XY 8 bit
		
        rts

MemDumpDirect		;Dump X bytes from MemdumpHL
        ldy #8
MemDumpDirectB
		lda t_MemdumpU
        sta t_MemdumpBU
        jsr printhex
        lda t_MemdumpH
        sta t_MemdumpBH
        jsr printhex
        lda t_MemdumpL
        sta t_MemdumpBL
        jsr printhex

        lda #':'
        jsr PrintChar
        jsr NewLine

        ldy #0
MemDump_Again
		;jsr Monitor

        tya
        pha
MemDump_AgainHex
                lda [t_MemdumpL],y

                jsr printhex
                lda #' '
                jsr PrintChar
                iny
                tya
                and #%00000111
                bne MemDump_AgainHex
        pla
        tay
MemDump_AgainChar
        lda [t_MemdumpBL],y
        cmp #32
        Bcc MemDump_BadChar
        cmp #128
        Bcs MemDump_BadChar
MemDump_AgainCharRet:
        jsr PrintChar

        iny
        tya
        and #%00000111
        bne MemDump_AgainChar
		.ifndef BuildSNS
		.ifndef BuildNES
		.ifndef BuildPCE
			jsr NewLine
		.endif
		.endif
		.endif
MemDump_CarryOn
		
        dex
        bne MemDump_Again
			
        rts
MemDump_BadChar
        lda #'.'
        jmp MemDump_AgainCharRet
		
		
		
Monitor ;WARNING: Must reset 8/16 bit mode after running!
		php
		Mode16bit
			pha
			phd
			phy	
			phx 
			pha
				lda #0
				tcd
			pla
		Mode8bit	
		 
	 ;jsr MemDump				;Dump an address to screen
		;dw $01E0      		;Address to show
		 ;db $4          		;Lines to show
		 ;jmp *
	
			pha
				lda #'c'
				jsr printMonitorchar
			pla
			jsr printMonitorhex16
		
			lda #'x'
			jsr printMonitorchar
			Mode16bit
				lda 1,s
			Mode8bit	
			jsr printMonitorhex16

			lda #'y'
			jsr printMonitorchar
			Mode16bit
				lda 3,s
			Mode8bit	
			jsr printMonitorhex16

		
			lda #'s'
			jsr printMonitorchar
			Mode16bit
				tsc
				clc
				adc #11
			Mode8bit
			jsr printMonitorhex16
			
			jsr NewLine
			
			lda #'f'
			jsr printMonitorchar

			
			lda 9,s
			
			jsr printMonitorhex
			

			lda #'p';
			jsr printMonitorchar

			
			tsx
			txa
			clc
			adc #8
			pha
					tax
					lda SPpage+$01,x
					jsr printhex
			pla
			tax
			lda SPpage+$00,x
			jsr printMonitorhex
			
			
			lda #'d';
			jsr PrintChar
			lda #'b';
			jsr printMonitorchar		
			phb
			pla
			jsr printMonitorhex
					
			lda #'p';
			jsr PrintChar
			lda #'b';
			jsr printMonitorchar		
			phk
			pla
			jsr printMonitorhex		
			
			lda #'d';
			jsr PrintChar
			lda #'p';
			jsr printMonitorchar	
			Mode16bit
				lda 5,s
			Mode8bit		
			
			jsr printMonitorhex16
		.ifndef BuildSNS
			jsr NewLine
		.endif
			
		
		Mode16bit
			plx 
			ply	
			pld
			pla
			plp
		.ACCU 8;assume M:1	;Accumulator 8 bit
		.INDEX 8 ;assume X:1	;XY 8 bit
        rts
printMonitorchar
        jsr PrintChar
        lda #':'
        jmp PrintChar
printMonitorhex16:		
		xba
		jsr printhex
		xba
printMonitorhex:
        jsr printhex
        lda #' '
        jmp PrintChar
       


