What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.
sir_archibald
Posts: 5
Joined: Sat Sep 06, 2025 10:45 am

What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by sir_archibald »

Hello.

I'm writing my own NES emulator.

I'm trying to implement NROM-256 (Super Mario Bros.).

So far my emulator is able to read the reset handler and set the program counter to the beginning of the program ROM at 0x8000 and process the first handful of instructions.

Where I'm running into a problem is the instruction at 0x8018 -- LDA $07D7.

This instruction is referencing the 2KB internal RAM.

I don't know what to write to the accumulator because I don't know what value is stored at that address.

I know that the first 0x01FF bytes contain the zero page and the stack, and I have the appropriate data structures written for both of those, but I have no clue what values are stored in the rest of the bytes upon powering up the system. Is it safe to say that they're all initialized to 0x00 or 0xFF? The wikipedia page mentions using that address space to store cartridge RAM, but NROM-256 doesn't use any.
User avatar
Quietust
Posts: 2028
Joined: Sun Sep 19, 2004 10:59 pm

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by Quietust »

On power-on, the console's internal RAM is uninitialized - the exact values you get depend on the physical characteristics of the RAM chip (e.g. transistor dimensions on the silicon wafer) as well as other factors such as temperature.

In practice, most emulators initialize RAM to either all $00, all $FF, special patterns (such as alternating $0F / $F0 or $55 / $AA), or completely random values. No matter which one you choose, you're probably going to run into problems with games that fail to properly initialize RAM on startup (but that's not your problem, since those games will likely have similar problems on real hardware).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
User avatar
Dwedit
Posts: 5257
Joined: Fri Nov 19, 2004 7:35 pm

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by Dwedit »

Final Fantasy 1 uses uninitialized memory which determining which monster you encounter, and it somewhat reliably ends up being an FF value on many systems.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
creaothceann
Posts: 863
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by creaothceann »

sir_archibald wrote: Sat Sep 06, 2025 11:07 am The wikipedia page mentions using that address space to store cartridge RAM
What wikipedia page? It's almost certainly wrong, afaik.
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
Bavi_H
Posts: 250
Joined: Sun Mar 03, 2013 1:52 am
Location: Texas, USA

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by Bavi_H »

Note: Some games will remember a high score even after a reset. Or some games remember if you have beat the game once and offer a "second quest" and will still remember that fact after a reset. (Super Mario Bros. does both.)

In order to do things like that, after a reset (or power up), the code will have to check some kind of RAM location to see if it has an expected value. If the value matches what is expected, the code assumes all the things have valid values. If the value doesn't match what is expected, the code will set all the things to their beginning valid values.

In Super Mario Bros., the instruction you encountered (8018 LDA $07D7,X) is the beginning of this kind of check.

In a previous discussion about Super Mario Bros. on the Discord server, I wrote the following:
I peeked at a Super Mario Bros. disassembly. In the reset routine, it looks like it confirms both of the following:
  • that every digit of the TOP score is a digit from 0 to 9,
and
  • that a specific location ($07FF) contains a specific value ($A5).
If the check passes, the game keeps the top score and some other values (the continue world, the flag that enables the second quest and ability to select a world on the title screen, and the last game's score and coins).
If the check fails, it sets location $07FF to $A5, and clears all the other locations.
sir_archibald
Posts: 5
Joined: Sat Sep 06, 2025 10:45 am

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by sir_archibald »

creaothceann wrote: Sun Sep 07, 2025 3:11 am
sir_archibald wrote: Sat Sep 06, 2025 11:07 am The wikipedia page mentions using that address space to store cartridge RAM
What wikipedia page? It's almost certainly wrong, afaik.
Here's the page:

https://www.nesdev.org/wiki/CPU_memory_map

Specifically this sentence:

Games may divide up the rest however the programmer deems useful. See Sample RAM map for an example allocation strategy for this RAM. Most commonly, $0200-$02FF is used for the OAM buffer to be copied to PPU OAM during vblank.

Although upon reading that sentence again, and considering that there is already a discrete address space for the cartridge RAM, it looks like you're right.
sir_archibald
Posts: 5
Joined: Sat Sep 06, 2025 10:45 am

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by sir_archibald »

Bavi_H wrote: Sun Sep 07, 2025 6:21 am Note: Some games will remember a high score even after a reset. Or some games remember if you have beat the game once and offer a "second quest" and will still remember that fact after a reset. (Super Mario Bros. does both.)

In order to do things like that, after a reset (or power up), the code will have to check some kind of RAM location to see if it has an expected value. If the value matches what is expected, the code assumes all the things have valid values. If the value doesn't match what is expected, the code will set all the things to their beginning valid values.

In Super Mario Bros., the instruction you encountered (8018 LDA $07D7,X) is the beginning of this kind of check.

In a previous discussion about Super Mario Bros. on the Discord server, I wrote the following:
I peeked at a Super Mario Bros. disassembly. In the reset routine, it looks like it confirms both of the following:
  • that every digit of the TOP score is a digit from 0 to 9,
and
  • that a specific location ($07FF) contains a specific value ($A5).
If the check passes, the game keeps the top score and some other values (the continue world, the flag that enables the second quest and ability to select a world on the title screen, and the last game's score and coins).
If the check fails, it sets location $07FF to $A5, and clears all the other locations.
Yeah, I was wondering what purpose that instruction serves. Thank you for the explanation.
Bavi_H
Posts: 250
Joined: Sun Mar 03, 2013 1:52 am
Location: Texas, USA

Re: What Values Are Stored in the Internal RAM Beyond the Zero Page and Stack?

Post by Bavi_H »

sir_archibald wrote: Sat Sep 06, 2025 11:07 amLDA $07D7. [...]
I don't know what to write to the accumulator because I don't know what value is stored at that address. [...]
The wikipedia page mentions using that address space to store cartridge RAM, but NROM-256 doesn't use any.
creaothceann wrote: Sun Sep 07, 2025 3:11 amWhat wikipedia page?
sir_archibald wrote: Sun Sep 07, 2025 7:19 amHere's the page: https://www.nesdev.org/wiki/CPU_memory_map
Note: if you look again, the table on the NesDev wiki page CPU memory map includes:

"$6000–$7FFF Usually cartridge RAM, when present."

Perhaps you might have misread that as $0600 - $07FF.


(Also note: Wiki is not the same as Wikipedia. A wiki is a kind of site that anyone can edit. Wikipedia is a specific wiki that is an online encyclopedia. The NesDev wiki is a different wiki that has information about NES development.)