Page 1 of 1

SNES stack pushing to zero page? ...and other SNES related questions

Posted: Thu Feb 20, 2020 11:50 am
by SusiKette
While debugging code I noticed that the return address for a subroutine was pushed to $00:00FE - $00:00FF instead of $00:01FE - $00:01FF. As far as I have read, SNES should use page 1 for stack just like NES, but still it seems to go to zero page. Not sure if I need to post any code, but here is all code until the subroutine:

Code: Select all

WRAM_Clear_DMA_Table:
.byte %00001000					; DMAPx
.byte $80						; BBADx
.byte <WRAM_Clear_DMA_Table+7	; A1TxL
.byte >WRAM_Clear_DMA_Table+7	; A1TxH
.byte $00						; A1Bx
.byte $FF						; DASxL
.byte $FF						; DASxH
.byte $00						; Fill Byte

RESET:
		CLC
		XCE					; Disable Emulation Mode
		SEI
		CLD
		
		.a8
		.i8
		SEP #$30
		
		LDA #$80
		STA INIDISP
		STZ NMITIMEN
		STZ MDMAEN
		STZ HDMAEN
		LDA #$01
		STA MEMSEL
		LDX #$FF
		TXS
		
		LDX #$01

Memory_Clear_Loop:
		LDY #$00
		STZ WMADDL			; Set WRAM address for DMA
		STZ WMADDM
		STX WMADDH
		
		LDA WRAM_Clear_DMA_Table,Y		; Set DMA with fixed Source
		STA DMAP0
		INY
		LDA WRAM_Clear_DMA_Table,Y			; Set DMA destination to $2180
		STA BBAD0
		INY
		LDA WRAM_Clear_DMA_Table,Y
		STA A1T0L
		INY
		LDA WRAM_Clear_DMA_Table,Y
		STA A1T0H
		INY
		LDA WRAM_Clear_DMA_Table,Y
		STA A1B0
		INY
		LDA WRAM_Clear_DMA_Table,Y			; Set DMA transfer to $FFFF bytes
		STA DAS0L
		INY
		LDA WRAM_Clear_DMA_Table,Y
		STA DAS0H
		
		LDA #$01
		STA MDMAEN			; Start DMA transfer
		DEX					; Decrement X and loop to clear the second page
		BPL Memory_Clear_Loop
		
		JSR Reset_Register_Setup
I'll be posting other questions here as they come up so I don't have to keep making new threads for them.

Re: SNES stack pushing to zero page? ...and other SNES related questions

Posted: Thu Feb 20, 2020 11:53 am
by lidnariq
SusiKette wrote:
Thu Feb 20, 2020 11:50 am
While debugging code I noticed that the return address for a subroutine was pushed to $00:00FE - $00:00FF instead of $00:01FE - $00:01FF. As far as I have read, SNES should use page 1 for stack just like NES, but still it seems to go to zero page.
If you're in Emulation (65C02) mode, that's correct. But since you're in native mode, the stack pointer is a full 16 bits.

edit: The 65816 datasheet additionally says:
In the Native mode (E=0), [...] If the Index Select Bit (X) equals one, both registers will be 8 bits wide, and the high byte is forced to zero.
and
All transfers from one register to another will result in a full 16-bit output from the source register.
so both together means that 8-bit X, set to $FF, and then TXS will result in the stack pointer holding $00FF.

Re: SNES stack pushing to zero page? ...and other SNES related questions

Posted: Fri Feb 21, 2020 3:05 am
by creaothceann
lidnariq wrote:
Thu Feb 20, 2020 11:53 am
SusiKette wrote:
Thu Feb 20, 2020 11:50 am
While debugging code I noticed that the return address for a subroutine was pushed to $00:00FE - $00:00FF instead of $00:01FE - $00:01FF. As far as I have read, SNES should use page 1 for stack just like NES, but still it seems to go to zero page.
If you're in Emulation (65C02) mode, that's correct. But since you're in native mode, the stack pointer is a full 16 bits.
In emulation mode the stack also doesn't wrap for "new" instructions.