CPU Stack Implementation Issues

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
CPU Stack Implementation Issues
by on (#30196)
Hi.

I'm currently in the process of writing a NES emulator. Recently I've been looking at the Dissembled Super Mario (Very helpful. Thanks doppelganger! :)). It appears to me that the code is writing directly to the stack memory in the InitializeMemory routine which suggests that i've got the wrong idea about implementing part of the memory.

I currently have the stack set up so that any writes to that part of memory results in the data being stored at the location specified by the stack pointer (The SP is then decremented). Reads read data from the top of the stack and then increment the SP.

I'm thinking now that this is completely wrong and that I should be allowing reads and writes to any part of the stack memory without manipulating the SP and that the only means of using the stack as a stack are via the stack instructions (PHA, PHP etc).

Does this new approach sound correct?

Thank you very much :).

by on (#30197)
It is possible to adress $100-$1ff like any other memory by the CPU, and to read and write to it as if this wasn't reserved for the stack.
pha is somewhat equivalent to "sta $100,s" then "dec s", while pla does "inc s" then "lda $100,s". Hope this clears things up.

by on (#30198)
I can not imagine where you came up with the former approach :), but the newer approach sounds correct.

by on (#30200)
Dwedit wrote:
I can not imagine where you came up with the former approach :), but the newer approach sounds correct.


Yeah, I'm a bit embarrassed to be honest :oops: .

Still, now hopefully I can stop SMB from crashing on me.

Thanks to you both :D .