Memory types

Memory regions

As you know, the Game Boy's CPU has a 16-bit address space: it can access up to 65,536 bytes of memory at any time, labelled from $0000 to $FFFF. Now, although from the CPU all of these bytes are equal, the programmer needs to be aware that not all of these bytes are the same. Simple example: some of the memory is the ROM where the game code is, some of the memory is the RAM. We need to know where each is.

Memory map

Here are the different memory types; I will explain them one by one. We will also see how they're used with RGBDS.

Begins atEnds atName
$0000$7FFFROM
$8000$9FFFVRAM
$A000$BFFFSRAM
$C000$DFFFWRAM
$E000$FDFFEcho RAM
$FE00$FE9FOAM
$FEA0$FEFF"FEXX"
$FF00$FF7FIO
$FF80$FFFEHRAM

ROM

ROM is where the game's program and data are stored. RGBDS provides two memory types for that area, ROM0 ($0000-$3FFF) and ROMX ($4000-$7FFF). This is due to something called banking, but we'll forget it for now. We will instead pass the -t option to RGBLINK to make ROM0 span the entire ROM space.

ROM is, as its name suggests, read-only. (You may see writes to the ROM space in games, but that's also due to banking, so forget it for now.) It's where the CPU will be executing code from almost always. Aside from banking, which we will pretend doesn't exist for now, there isn't anything special about ROM.

VRAM

We already discussed about VRAM in the Displaying lesson, but here's a quick recap. Between $8000 and $97FF are stored the tiles; 16 ($10) bytes per tile, and 384 ($180) tiles in three "blocks" of 128 ($80) tiles each. Then are two tilemaps, one at $9800-$9BFF, and one at $9C00-$9FFF. Each tilemap is 32*32 bytes.

VRAM has a big flaw, in that it can only be accessed while the PPU is not; that is, it must be accessed during LCD modes 0, 1 and 2.

SRAM

SRAM stands for Save RAM. It's somewhat misleading, which is why some refer to it as XRAM (External RAM or Expansion RAM), but SRAM is the most common term. SRAM is RAM on the cartridge; most of the time, it's backed up by a battery and used to store save data, but some games also use it as extra work RAM (Pokémon games use SRAM as scratch buffers for sprite decompression, Super Mario Land 2 uses some of its SRAM to store variables and level data).

By default, SRAM is in a "locked" state, which, like locked VRAM, ignores writes and returns $FF when read. (Editor's note: it's actually possible that $FF may NOT be always returned. It's best to not rely on reads from SRAM.) How SRAM is unlocked depends on the MBC.

WRAM

WRAM is the console's work RAM. It's on the