SRAM

From Glitch City Wiki
Jump to navigation Jump to search
Wikipedia also has an article about SRAM.

In Game Boy games, the SRAM (static RAM, also known as the cartridge RAM) is a special section of the RAM that can retain data when the console is powered off. It is usually used to store save data, but may also be used for other purposes (such as sprite decompression). As the name "cartridge RAM" suggests, the SRAM is on the game cartridge, so that each cartridge can have its own save data. The SRAM still needs power to retain data (although it needs much less power than other types of RAM does); it is typically powered by a battery on the cartridge.

The SRAM is associated with the address range $A000–$BFFF, an 8KB memory region. However, the SRAM is usually banked, meaning that the game can switch between multiple 8KB regions called "banks", so more than 8KB of data in total can be stored. The SRAM is also usually disabled ("locked") by default, preventing all read and write operations to it until it is enabled ("unlocked"). Both bank switching and SRAM locking is handled by the memory bank controller (MBC) on the cartridge, and can be controlled by writing to specific memory address ranges that are otherwise mapped to the ROM[1].

MBC interface

There are multiple types of MBCs, but they mostly have the same behavior regarding SRAM:

  • Trying to write to any address in the range $0000–$1FFF enables or disables the SRAM. Any value with 0xA in the lower 4 bit enables the SRAM, and any other value disables it.
    • Normally, the value written to enable the SRAM is 0x0A, and the value written to disable it is 0x00.
    • The Nintendo 3DS Virtual Console emulates this behavior inaccurately. Instead of checking whether value & 0x0F == 0x0A, it checks whether value & 0x0A == 0x0A, which means that any value with 0xA, 0xB, 0xE or 0xF in the lower 4 bit will enable the SRAM.
    • When the SRAM is disabled, attempts to read from $A000–$BFFF will return open bus values (usually 0xFF, but not always), and attempts to write to $A000–$BFFF will be ignored[2].
  • Trying to write to any address in the range $4000–$5FFF switches the SRAM bank (if the SRAM is banked). Usually bank numbers in the range $00–$03 are supported.
More research is needed for this article.

Reason given: Writing values outside the range 0x00–0x03 to $4000–$5FFF seems to do nothing, probably because English games use MBC3, where $4000–$5FFF is also used to select the RTC register. But are all high bits checked (e.g. maybe 0x05 does nothing but 0x81 would still switch to bank 1?), and would the behavior be different on Japanese Red/Green, which use MBC1?



Attribution

  • CasualPokePlayer posted the buggy code in the 3DS Virtual Console's implementation of SRAM locking to the GCRI Discord.
  • TimoVM verified the SRAM locking/unlocking logic on both a real Game Boy console and the Virtual Console.

References