Difference between revisions of "Memory Map"

From GbdevWiki
Jump to: navigation, search
m
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Pandocs|memory-map}}
 +
 
The gameboy has a 16bit address bus, that is used to address [[ROM]], [[RAM]], and [[I/O registers]].
 
The gameboy has a 16bit address bus, that is used to address [[ROM]], [[RAM]], and [[I/O registers]].
  
Line 11: Line 13:
 
|3FFF
 
|3FFF
 
|16KB ROM bank 00
 
|16KB ROM bank 00
|From cartridge, fixed bank
+
|From cartridge, usually a fixed bank
 
|-
 
|-
 
|4000
 
|4000
 
|7FFF
 
|7FFF
 
|16KB ROM Bank 01~NN  
 
|16KB ROM Bank 01~NN  
|From cartridge, switchable bank via [[Memory_Bank_Controllers|MBC]]
+
|From cartridge, switchable bank via [[Memory_Bank_Controllers|MBC]] (if any)
 
|-
 
|-
 
|8000
 
|8000
 
|9FFF
 
|9FFF
 
|8KB Video RAM (VRAM)
 
|8KB Video RAM (VRAM)
|Switchable bank 0/1 in CGB mode
+
|Only bank 0 in Non-CGB mode
 +
Switchable bank 0/1 in CGB mode
 
|-
 
|-
 
|A000
 
|A000
Line 35: Line 38:
 
|D000
 
|D000
 
|DFFF
 
|DFFF
|4KB Work RAM bank 1~N
+
|4KB Work RAM (WRAM) bank 1~N
|Switchable bank 1~7 in CGB mode
+
|Only bank 1 in Non-CGB mode
 +
Switchable bank 1~7 in CGB mode
 
|-
 
|-
 
|E000
 
|E000
 
|FDFF
 
|FDFF
|Mirror of C000~DDFF (ECHO)
+
|Mirror of C000~DDFF (ECHO RAM)
 
|Typically not used
 
|Typically not used
 
|-
 
|-
 
|FE00
 
|FE00
 
|FE9F
 
|FE9F
|Sprite attribute table ([[OAM]])
+
|Sprite attribute table ([[Video_Display#VRAM_Sprite_Attribute_Table_.28OAM.29|OAM]])
 
|
 
|
 
|-
 
|-
Line 65: Line 69:
 
|FFFF
 
|FFFF
 
|FFFF
 
|FFFF
|[Interrupts|Interrupt] Enable Register
+
|[[Interrupts]] Enable Register (IE)
 
|
 
|
 
|}
 
|}
Line 73: Line 77:
 
*RST commands: 0000,0008,0010,0018,0020,0028,0030,0038
 
*RST commands: 0000,0008,0010,0018,0020,0028,0030,0038
 
*Interrupts: 0040,0048,0050,0058,0060
 
*Interrupts: 0040,0048,0050,0058,0060
However, the memory may be used for any other purpose in case that your program doesn't use any (or only some) RST commands or interrupts. RST commands are 1-byte opcodes that work similiar to CALL opcodes, except that the destination address is fixed.
+
However, the memory may be used for any other purpose in case that your program doesn't use any (or only some) RST commands or interrupts. RST commands are 1-byte opcodes that work similar to CALL opcodes, except that the destination address is fixed. Since they are only 1 byte large, they are also slightly faster.
  
 
== Cartridge Header in first ROM bank ==
 
== Cartridge Header in first ROM bank ==
Line 79: Line 83:
  
 
== External Memory and Hardware ==
 
== External Memory and Hardware ==
The areas from 0000-7FFF and A000-BFFF may be used to connect external hardware. The first area is typically used to address ROM (read only, of course), cartridges with [[Memory Bank Controllers|Memory Bank Controllers (MBCs)]] are additionally using this area to output data (write only) to the MBC chip. The second area is often used to address external RAM, or to address other external hardware ([[RTC|Real Time Clock]], etc). External memory is often battery buffered, and may hold saved game positions and high score tables (etc.) even when the gameboy is turned of, or when the cartridge is removed. For specific information read the chapter about Memory Bank Controllers.
+
The areas from 0000-7FFF and A000-BFFF may be used to connect external hardware. The first area is typically used to address ROM (read only, of course), cartridges with [[Memory Bank Controllers|Memory Bank Controllers (MBCs)]] are additionally using this area to output data (write only) to the MBC chip. The second area is often used to address external RAM, or to address other external hardware ([[RTC|Real Time Clock]], etc). External memory is often battery buffered, and may hold saved game positions and high score tables (etc.) even when the Game Boy is turned off, or when the cartridge is removed. For specific information read the chapter about Memory Bank Controllers.
 +
 
 +
== Echo RAM ==
 +
The memory range E000-FDFF is a mirror (or "echo") of WRAM, both for reading and writing. For example, writing to $E123 will modify both $C123 and $E123. It is recommended to avoid using this memory range anyways.
 +
This memory range's behavior has been confirmed on all grey GBs as well as on CGB and GBA. Some emulators (such as VisualBoyAdvance <1.8) don't emulate Echo RAM. It is possible to check if Echo RAM is properly emulated by writing to WRAM (avoid values 00 and FF) and checking if said value is mirrored in Echo RAM.
 +
 
 +
== FEA0-FEFF range ==
 +
This range is very poorly documented. It doesn't even have a name ! From my experience, this stays 00 on DMG, and alternates between 00 and seemingly random values on CGB.

Latest revision as of 21:11, 4 April 2020

The copy of Pan Docs hosted on this wiki is considered deprecated.

Pan Docs is now officially hosted on gbdev.io as a living document. Please go to https://gbdev.io/pandocs/ to read Pan Docs or to https://github.com/gbdev/pandocs to contribute.

Click here to go to this section of Pan Docs in the new location: https://gbdev.io/pandocs/#memory-map

The gameboy has a 16bit address bus, that is used to address ROM, RAM, and I/O registers.

General Memory Map

Start End Description Notes
0000 3FFF 16KB ROM bank 00 From cartridge, usually a fixed bank
4000 7FFF 16KB ROM Bank 01~NN From cartridge, switchable bank via MBC (if any)
8000 9FFF 8KB Video RAM (VRAM) Only bank 0 in Non-CGB mode

Switchable bank 0/1 in CGB mode

A000 BFFF 8KB External RAM In cartridge, switchable bank if any
C000 CFFF 4KB Work RAM (WRAM) bank 0
D000 DFFF 4KB Work RAM (WRAM) bank 1~N Only bank 1 in Non-CGB mode

Switchable bank 1~7 in CGB mode

E000 FDFF Mirror of C000~DDFF (ECHO RAM) Typically not used
FE00 FE9F Sprite attribute table (OAM)
FEA0 FEFF Not Usable
FF00 FF7F I/O Registers
FF80 FFFE High RAM (HRAM)
FFFF FFFF Interrupts Enable Register (IE)

Jump Vectors in first ROM bank

The following addresses are supposed to be used as jump vectors:

  • RST commands: 0000,0008,0010,0018,0020,0028,0030,0038
  • Interrupts: 0040,0048,0050,0058,0060

However, the memory may be used for any other purpose in case that your program doesn't use any (or only some) RST commands or interrupts. RST commands are 1-byte opcodes that work similar to CALL opcodes, except that the destination address is fixed. Since they are only 1 byte large, they are also slightly faster.

Cartridge Header in first ROM bank

The memory at 0100-014F contains the cartridge header. This area contains information about the program, its entry point, checksums, information about the used MBC chip, the ROM and RAM sizes, etc. Most of the bytes in this area are required to be specified correctly. For more information read the chapter about The Cartridge Header.

External Memory and Hardware

The areas from 0000-7FFF and A000-BFFF may be used to connect external hardware. The first area is typically used to address ROM (read only, of course), cartridges with Memory Bank Controllers (MBCs) are additionally using this area to output data (write only) to the MBC chip. The second area is often used to address external RAM, or to address other external hardware (Real Time Clock, etc). External memory is often battery buffered, and may hold saved game positions and high score tables (etc.) even when the Game Boy is turned off, or when the cartridge is removed. For specific information read the chapter about Memory Bank Controllers.

Echo RAM

The memory range E000-FDFF is a mirror (or "echo") of WRAM, both for reading and writing. For example, writing to $E123 will modify both $C123 and $E123. It is recommended to avoid using this memory range anyways. This memory range's behavior has been confirmed on all grey GBs as well as on CGB and GBA. Some emulators (such as VisualBoyAdvance <1.8) don't emulate Echo RAM. It is possible to check if Echo RAM is properly emulated by writing to WRAM (avoid values 00 and FF) and checking if said value is mirrored in Echo RAM.

FEA0-FEFF range

This range is very poorly documented. It doesn't even have a name ! From my experience, this stays 00 on DMG, and alternates between 00 and seemingly random values on CGB.