How the Memory Map works exactly

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
How the Memory Map works exactly
by on (#242094)
I'm having one of those moments where you think you know something, then the more you think about it the more you think you don't know anything, and I keep going around in circles and spent too many hours googling without solid results, so I thought I'd just ask the experts. My background is mainly in Electrical Engineering, but I also have a lot of experience in programming, including Assembly. So I can understand a decent bit of this.

I'm currently writing up an entry on my website to help people understand how the SNES cartridge works, without getting into the muddy details of it all, since I doubt I'd get everything correct anyway, and I still feel like I'm somewhat of a beginner myself despite keeping this as a main hobby of mine for over 2 years now. But I'm hung up on how to word a specific paragraph (that I could probably leave out but it'll bug me if I do).

Firstly, what's the difference between shadowed memory and mirrored memory? I see these terms around a lot online. I'm pretty sure mirrored memory just means it's copied into a different bank to be used by the SNES without having to change banks, benefiting the programmer. And I've also seen that only some memory areas can be used as FastROM. But I'm still unsure about what shadowed memory is - or if I should even care about what it is.

My main question though is how the SNES memory map correlates to the wiring of the EPROM. I get how the mappers work for enabling the RAM and everything, that part I can understand. But I'm thinking about the address lines on the EPROM and how they connect to the cartridge connector. According to the HiROM memory map I found that tepples made (which really cleans up what I see in the manual), the ROM is stored from banks 0xC0 to 0xFF.

Image

BA7 isn't connected to the EPROM, though. I get that this is the SNES memory map, and not the cartridge, and I see that the total area is 4MB, but how does the SNES know to move the ROM data to these later banks, instead of treating it like it was in LoROM mode? I know the header defines what Mode the ROM should be read in, does that mean there are instructions internal to the SNES that tells it where the data should go? And how does the SNES retrieve this data at the correct positions? If the SNES is addressing say 0x70:FFFF, that region is blank in the memory map (or at the very least, not allocated as ROM storage), but the EPROM will still have game information at that address. Or does the SNES know to read the information from the EPROM at this address, and then move it to the corresponding main ROM area in the 0xC0-0xFF banks?

I hope this is making sense. And I hope it's something really simple I'm overlooking. Once I'm done writing up my guide, I was going to post it here for some critique, but I feel like I need to understand this better first. Thanks in advance, and I hope I'm not making a fool of myself :D
Re: How the Memory Map works exactly
by on (#242096)
When data in ROM shows up at a particular place in the SNES memory map, that's because the physical cartridge is wired that way. The HiROM area starts at 0xC00000, or in binary, 0b110000000000000000000000. Some piece of logic on the cartridge (not a CPU that executes instructions, probably just some wires and a few transistors) is looking at the top two bits of the address bus, and if they're both set, forwarding (some) of the address lines to the ROM on the cart.

For example, if the SNES requests 0xC01234, it's easy enough to use the top few bits to decide "yes, this is a request I want to answer", and send the other 22 bits (if it's a 32Mbit ROM) off to the EPROM to figure out *how* to answer. Or if it's an 8Mbit ROM, you only need the bottom 20 address bits, and the middle bits can be ignored. Then two addresses that only differ by ignored bits will retrieve the same data, which is how mirroring works.

The SNES header that emulators use to guess the memory mapping doesn't actually do anything on a real SNES. Perhaps Nintendo used it as a way to verify that the right ROM was soldered to the right board, but it's got nothing to do with how real hardware actually worked.
Re: How the Memory Map works exactly
by on (#242099)
poorstudenthobbyist wrote:
I'm pretty sure mirrored memory just means it's copied into a different bank to be used by the SNES without having to change banks, benefiting the programmer. And I've also seen that only some memory areas can be used as FastROM. But I'm still unsure about what shadowed memory is - or if I should even care about what it is.

With the SNES I've always only seen "mirrored". It just means that at least one address bit is ignored during the translation of a SNES address to a hardware address. The result is that several SNES addresses end up pointing to the same physical address.

For example: registers $2140 to $2143 are mirrored throughout $2140..$217F:

0010000101000000 = $2140
0010000101000001 = $2141
0010000101000010 = $2142
0010000101000011 = $2143 ...
0010000101111111 = $217F

For this region the hardware simply ignores bits 5..2 of the SNES address.

poorstudenthobbyist wrote:
how does the SNES know to move the ROM data to these later banks, instead of treating it like it was in LoROM mode? I know the header defines what Mode the ROM should be read in, does that mean there are instructions internal to the SNES that tells it where the data should go? And how does the SNES retrieve this data at the correct positions?

Data isn't moved at all, since that would imply that it would take some amount of time. The mapping is simply a consequence of the cartridge's board layout, so it's active as soon as the SNES is turned on.

poorstudenthobbyist wrote:
If the SNES is addressing say 0x70:FFFF, that region is blank in the memory map (or at the very least, not allocated as ROM storage), but the EPROM will still have game information at that address.

No, a SNES address is in a completely different address space than a ROM address. (And if a SNES memory region is not mapped to the cartridge, the cartridge won't even see the read/write access.)
Re: How the Memory Map works exactly
by on (#242101)
Ok, so it's as I suspected, it's all handled by the physical wiring of the cart. At least I have that much down, that'swhy LoROM made sense to me. And that explanation of mirroring makes total sense.

So, when BA6 and BA7 are high, that puts it in the upper banks of the memory map. Ok. But, BA6 and BA7 don't go anywhere on the cartridge, except to BA6 goes to A22 on the SNES Mask ROM pinout and plays a role in turning on the RAM chip (and BA7 doesn't go anywhere to my knowledge). And the /OE pin on HiROM boards is tied to the /CART pin (pin 49) on the connector. So then how does the cartridge know to answer only when BA6 and BA7 are 1's?
Re: How the Memory Map works exactly
by on (#242105)
Mirror = shows up somewhere else
Shadow = it overlaying something and hence casts a shadow upon something

The SNES has no Shadow, Commodore 64 does, IBM PCs do, and they probably picked up the term Shadow from the PC BIOS and just used it.

Its not 100% as it is mapped on the cart. The SNES does have some internal logic, to handle the RAM mirroring, the IO mirroring and the SRAM mirroring and the WRAM enable.

The only real difference is that in HIROM you divide it into 64K banks not 32K banks.
Code:
So in LoROM
00:0000 000000000000000000000000 => 011111100000000000000000 WRAM En
00:8000 000000001000000000000000 => 000000000000000000000000 Cart En
c0:0000 110000000000000000000000 => 000000000000000000000000 Cart En
c0:8000 110000001000000000000000 => 000000000000000000000000 Cart En
while in HiROM
00:0000 000000000000000000000000 => 011111100000000000000000 WRAM En
00:8000 000000001000000000000000 => 000000001000000000000000 Cart En
c0:0000 110000000000000000000000 => 000000000000000000000000 WRAM En
c0:8000 110000001000000000000000 => 000000001000000000000000 Cart En
Re: How the Memory Map works exactly
by on (#242106)
poorstudenthobbyist wrote:
Firstly, what's the difference between shadowed memory and mirrored memory? I see these terms around a lot online. I'm pretty sure mirrored memory just means it's copied into a different bank to be used by the SNES without having to change banks, benefiting the programmer. And I've also seen that only some memory areas can be used as FastROM. But I'm still unsure about what shadowed memory is - or if I should even care about what it is.

"Shadowing" and "mirroring" are terms used by different subcultures of retrocomputing for the same phenomenon: two different sets of addresses to which a circuit that responds identically. It happens when one or more address lines aren't connected to decoders (circuits that generate an enable signal) or to a circuit's address lines.

poorstudenthobbyist wrote:
how the SNES memory map correlates to the wiring of the EPROM. I get how the mappers work for enabling the RAM and everything, that part I can understand. But I'm thinking about the address lines on the EPROM and how they connect to the cartridge connector.

ROM A14-A0 always connects to CPU A14-A0. The connection of higher ROM address lines (A22-A15) depends on how the cartridge is wired. This differs among different cartridges, but the vast majority of Super NES cartridges fall into one of the two following patterns:

  • In HiROM (mode $21), ROM A21-A15 connect to CPU A21-A15. The mirroring occurs because CPU A23 and A22 (also called BA7-BA6) aren't connected to ROM.
  • In LoROM (mode $20), ROM A21-A15 connect to CPU A22-A16. Mirroring occurs because CPU A23 (also called BA7) isn't connected to ROM, and A15 may additionally cause mirroring in cartridges without SRAM.

poorstudenthobbyist wrote:
how does the SNES know to move the ROM data to these later banks, instead of treating it like it was in LoROM mode?

There's a ROM enable signal on the cart edge called /CART that's active (low) when the bank is $40-$7D or $80-$FF or when the address is $8000-$FFFF (and the bank is not $7E-$7F).

poorstudenthobbyist wrote:
If the SNES is addressing say 0x70:FFFF, that region is blank in the memory map (or at the very least, not allocated as ROM storage)

The memory map you linked is from the programmer's point of view, not an electrical engineer's. It is not intended to be an exhaustive description of every address in the system. As such, it doesn't show shadows/mirrors of the ROM on which a programmer isn't expected to rely. In typical cartridges, $400000-$7DFFFF behaves as a mirror of $C00000-$FDFFFF. But because there is no advantage to the programmer to access HiROM through that address range, it's not labeled as such on this particular memory map.

As for the other mirrors in most HiROM cartridges:

  • $808000-$BFFFFF is a mirror of the second 32K of each bank of the ROM: $808000-$80FFFF matches $C08000-$C0FFFF, $818000-$81FFFF matches $C18000-$C1FFFF, etc. This is useful in order to have the second half of a ROM bank and the system area in the same bank.
  • $008000-$BFFFFF is also a mirror of the second 32K of each bank of the ROM. In particular, $008000-$00FFFF matches $C08000-$C0FFFF. This is useful because the CPU always reads interrupt vectors (including the important /RESET) from bank 0, namely $00FFE0-$00FFFF.
  • $400000-$7DFFFF is a mirror of $C00000-$FDFFFF, which is the entire ROM except for the last 128 KiB. This has no advantage over just using $C00000-$FDFFFF because reads from $400000-$7DFFFF always use slow timing (6 master clocks per access) rather than fast timing (8 master clocks per access)

poorstudenthobbyist wrote:
how does the cartridge know to answer only when BA6 and BA7 are 1's?

The cartridge does not in fact "know to answer only when BA6 and BA7 are 1's." It answers when /CART is low. The fact that it appears to is an artifact of the choice to draw the particular memory map image that you linked from the programmer's point of view.
Re: How the Memory Map works exactly
by on (#242107)
Oziphantom wrote:
The SNES does have some internal logic, to handle the RAM mirroring, the IO mirroring and the SRAM mirroring and the WRAM enable.

relevant link
Re: How the Memory Map works exactly
by on (#242108)
Ok, I think you've filled in the missing information I was looking for. I did not previously know when /CART was active, I guess I had assumed it was active whenever the SRAM wasn't being used, though thinking about it more I'm not sure why I had assumed that. It wouldn't make sense for the SNES to activate the ROM during periods when it didn't need it (or when it would create a bus conflict). This makes so much more sense now.

I kept finding forum posts here about how HiROM and LoROM are unhelpful when talking about how the SNES memory works, because the memory map is just a consequence of how the cartridge is wired. Now I understand why. I think I was confusing myself because I was also reading information about emulators, which I now understand would present problems when trying to read ROM files since emulators would have to account for all types of boards.

As always, thanks everyone for being patient! This was very helpful.
Re: How the Memory Map works exactly
by on (#242113)
tepples wrote:
"Shadowing" and "mirroring" are terms used by different subcultures of retrocomputing for the same phenomenon: two different sets of addresses to which a circuit that responds identically.

While true for 'mirroring', I've always known 'shadowing' to be the act of copying a set of data and then using that instead of the original. In the case of the IBM compatible it was used to copy the BIOS from slow ROM to faster RAM (consuming precious conventional memory in the process). In the case of the C64, people would make a shadow copy of the BASIC and KERNAL ROMS if they needed those routines but wanted to switch out the character generator or make temporary changes to said ROMS. In both cases the shadow copies reside in the same places within their memory maps (at least in the case of the C64, I'm just guessing for PC).