Skip to content

Commit 307b504

Browse files
avivaceihonen
andauthored
Revise the explanation of the cartridge header (#429)
Co-authored-by: Anton Ihonen <[email protected]> Co-authored-by: Antonio Vivace <[email protected]>
1 parent 5e96d49 commit 307b504

File tree

2 files changed

+148
-141
lines changed

2 files changed

+148
-141
lines changed

src/Power_Up_Sequence.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Rely on it at your own risk.
207207
### Common remarks
208208

209209
The console's WRAM and HRAM are random on power-up.
210-
[Different models tend to exhibit different patterns](https://twitter.com/CasualPkPlayer/status/1409752977812852736?s=19), but they are random nonetheless, even depending on factors such as the ambient teperature.
210+
[Different models tend to exhibit different patterns](https://twitter.com/CasualPkPlayer/status/1409752977812852736), but they are random nonetheless, even depending on factors such as the ambient temperature.
211211
Besides, turning the system off and on again has proven reliable enough [to carry over RAM from one game to another](https://www.youtube.com/watch?v=xayxmTLljr8), so it's not a good idea to rely on it at all.
212212

213213
Emulation of uninitialized RAM is inconsistent: some emulators fill RAM with a constant on startup (typically $00 or $FF), some emulators fully randomize RAM, and others attempt to reproduce the patterns observed on hardware.

src/The_Cartridge_Header.md

Lines changed: 147 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,79 @@
11
# The Cartridge Header
22

3-
An internal information area is located at $0100-014F in each cartridge.
4-
It contains the following values:
3+
Each cartridge contains a header, located at the address range `$0100``$014F`.
4+
The cartridge header provides the following information about the game itself and the hardware it expects to run on:
55

66
## 0100-0103 - Entry Point
77

8-
After displaying the Nintendo Logo, the built-in boot procedure jumps to
9-
this address ($0100), which should then jump to the actual main program
10-
in the cartridge. Usually this 4 byte area contains a `nop` instruction,
11-
followed by a `jp $0150` instruction. But not always.
8+
After displaying the Nintendo logo, the built-in [boot ROM](<#Power-Up Sequence>) jumps to the address `$0100`, which should then jump to the actual main program in the cartridge.
9+
Most commercial games fill this 4-byte area with a [`nop` instruction](https://rgbds.gbdev.io/docs/v0.5.2/gbz80.7/#NOP) followed by a [`jp $0150`](https://rgbds.gbdev.io/docs/v0.5.2/gbz80.7/#JP_n16).
1210

1311
## 0104-0133 - Nintendo Logo
1412

15-
These bytes define the bitmap of the Nintendo logo that is displayed
16-
when the Game Boy gets turned on. The hex dump of this bitmap is:
13+
This area contains a bitmap image that is displayed when the Game Boy is powered on.
14+
It must match the following (hexadecimal) dump, otherwise [the boot ROM](<#Power-Up Sequence>) won't allow the game to run:
15+
1716
```
18-
CE ED 66 66 CC 0D 00 0B 03 73 00 83 00 0C 00 0D
19-
00 08 11 1F 88 89 00 0E DC CC 6E E6 DD DD D9 99
20-
BB BB 67 63 6E 0E EC CC DD DC 99 9F BB B9 33 3E
17+
CE ED 66 66 CC 0D 00 0B 03 73 00 83 00 0C 00 0D
18+
00 08 11 1F 88 89 00 0E DC CC 6E E6 DD DD D9 99
19+
BB BB 67 63 6E 0E EC CC DD DC 99 9F BB B9 33 3E
2120
```
22-
The Game Boy's boot procedure verifies the content of this bitmap
23-
(after it has displayed it), and LOCKS ITSELF UP if these bytes are
24-
incorrect. A CGB verifies only the first half ($18 bytes of) the bitmap, but
25-
others (for example a Game Boy pocket) verify all $30 bytes, as does the
26-
Game Boy Advance.
21+
22+
The way the pixels are encoded is as follows: ([more visual aid](https://github.com/ISSOtm/gb-bootroms/blob/2dce25910043ce2ad1d1d3691436f2c7aabbda00/src/dmg.asm#L259-L269))
23+
24+
- The bytes `$0104``$011B` encode the top half of the logo while the bytes `$011C``$0133` encode the bottom half.
25+
- For each half, each nibble encodes 4 pixels (the MSB corresponds to the leftmost pixel, the LSB to the rightmost); a pixel is lit if the corresponding bit is set.
26+
- The 4-pixel "groups" are laid out top to bottom, left to right.
27+
- Finally, the monochrome models upscale the entire thing by a factor of 2 (leading to somewhat chunky pixels).
28+
29+
The Game Boy's boot procedure [first displays the logo and then checks](<#Bypass>) that it matches the dump above.
30+
If it doesn't, the boot ROM **locks itself up**.
31+
32+
The CGB and later models [only check the top half of the logo](Power_Up_Sequence.html?highlight=half#behavior) (the first `$18` bytes).
2733

2834
## 0134-0143 - Title
2935

30-
Title of the game in UPPER CASE ASCII. If it is less than 16 characters
31-
then the remaining bytes are filled with $00 bytes. When inventing the CGB,
32-
Nintendo has reduced the length of this area to 15 characters, and some
33-
months later they had the fantastic idea to reduce it to 11 characters
34-
only. The new meaning of the ex-title bytes is described below.
36+
These bytes contain the title of the game in upper case ASCII.
37+
If the title is less than 16 characters long, the remaining bytes should be padded with `$00`s.
38+
39+
Parts of this area actually have a different meaning on later cartridges, reducing the actual title size to 15 (`$0134``$0142`) or 11 (`$0134``$013E`) characters; see below.
3540

3641
## 013F-0142 - Manufacturer Code
3742

38-
In older cartridges this area has been part of the Title (see above), in
39-
newer cartridges this area contains an 4 character uppercase
40-
manufacturer code. Purpose and Deeper Meaning unknown.
43+
In older cartridges these bytes were part of the Title (see above).
44+
In newer cartridges they contain a 4-character manufacturer code (in uppercase ASCII).
45+
The purpose of the manufactorer code is unknown.
4146

4247
## 0143 - CGB Flag
4348

44-
In older cartridges this byte has been part of the Title (see above). In
45-
CGB cartridges the upper bit is used to enable CGB functions. This is
46-
required, otherwise the CGB switches itself into Non-CGB-Mode. Typical
47-
values are:
48-
```
49-
80h - Game supports CGB functions, but also works on old Game Boys.
50-
C0h - Game works on CGB only (physically the same as $80).
51-
```
52-
Values with Bit 7 set, and either Bit 2 or 3 set, will switch the
53-
Game Boy into a special non-CGB-mode called "PGB mode".
49+
In older cartridges this byte was part of the Title (see above).
50+
The CGB and later models interpret this byte to decide whether to enable Color mode ("CGB Mode") or to fall back to monochrome compatibility mode ("Non-CGB Mode").
51+
52+
Typical values are:
53+
54+
Value | Meaning
55+
------|----------------------------------------------------------------------------------------------------
56+
`$80` | The game supports CGB enhancements, but is backwards compatible with monochrome Game Boys
57+
`$C0` | The game works on CGB only (the hardware ignores bit 6, so this really functions the same as `$80`)
5458

55-
TODO: research and document PGB modes...
59+
Values with bit 7 and either bit 2 or 3 set will switch the Game Boy into a special non-CGB-mode called "PGB mode".
60+
61+
::: tip Research needed
62+
63+
The PGB mode is not well researched or documented yet.
64+
Help is welcome!
65+
66+
:::
5667

5768
## 0144-0145 - New Licensee Code
5869

59-
Specifies a two-character ASCII licensee code, indicating the company or
60-
publisher of the game. These two bytes are used in newer games only
61-
(games that have been released after the SGB has been invented). Older
62-
games are using the header entry at `$014B` instead.
70+
This area contains a two-character ASCII "licensee code" indicating the game's publisher.
71+
It is only meaningful if the [Old Licensee Code](<#014B — Old Licensee Code>) is exactly `$33` (which is the case for essentially all games made after the SGB was released); otherwise, the old code must be considered.
6372

6473
Sample licensee codes:
6574

6675
Code | Publisher
67-
-----|-----------
76+
-----|-------------------------
6877
`00` | None
6978
`01` | Nintendo R&D1
7079
`08` | Capcom
@@ -129,143 +138,141 @@ Code | Publisher
129138

130139
## 0146 - SGB Flag
131140

132-
Specifies whether the game supports SGB functions, common values are:
133-
134-
- `$00`: No SGB functions (Normal Game Boy or CGB only game)
135-
- `$03`: Game supports SGB functions
136-
137-
The SGB disables its SGB functions if this byte is set to a value other than `$03`.
141+
This byte specifies whether the game supports SGB functions.
142+
The SGB will ignore any [command packets](<#Command Packet Transfers>) if this byte is set to a value other than `$03` (typically `$00`).
138143

139144
## 0147 - Cartridge Type
140145

141-
Specifies which Memory Bank Controller (if any) is used in the
142-
cartridge, and if further external hardware exists in the cartridge.
143-
144-
Code | Type
145-
-----|------
146-
$00 | ROM ONLY
147-
$01 | MBC1
148-
$02 | MBC1+RAM
149-
$03 | MBC1+RAM+BATTERY
150-
$05 | MBC2
151-
$06 | MBC2+BATTERY
152-
$08 | ROM+RAM [^rom_ram]
153-
$09 | ROM+RAM+BATTERY [^rom_ram]
154-
$0B | MMM01
155-
$0C | MMM01+RAM
156-
$0D | MMM01+RAM+BATTERY
157-
$0F | MBC3+TIMER+BATTERY
158-
$10 | MBC3+TIMER+RAM+BATTERY [^mbc30]
159-
$11 | MBC3
160-
$12 | MBC3+RAM [^mbc30]
161-
$13 | MBC3+RAM+BATTERY [^mbc30]
162-
$19 | MBC5
163-
$1A | MBC5+RAM
164-
$1B | MBC5+RAM+BATTERY
165-
$1C | MBC5+RUMBLE
166-
$1D | MBC5+RUMBLE+RAM
167-
$1E | MBC5+RUMBLE+RAM+BATTERY
168-
$20 | MBC6
169-
$22 | MBC7+SENSOR+RUMBLE+RAM+BATTERY
170-
$FC | POCKET CAMERA
171-
$FD | BANDAI TAMA5
172-
$FE | HuC3
173-
$FF | HuC1+RAM+BATTERY
146+
This byte indicates what kind of hardware is present on the cartridge — most notably its [mapper](#MBCs).
147+
148+
Code | Type
149+
------|--------------------------------
150+
`$00` | ROM ONLY
151+
`$01` | MBC1
152+
`$02` | MBC1+RAM
153+
`$03` | MBC1+RAM+BATTERY
154+
`$05` | MBC2
155+
`$06` | MBC2+BATTERY
156+
`$08` | ROM+RAM [^rom_ram]
157+
`$09` | ROM+RAM+BATTERY [^rom_ram]
158+
`$0B` | MMM01
159+
`$0C` | MMM01+RAM
160+
`$0D` | MMM01+RAM+BATTERY
161+
`$0F` | MBC3+TIMER+BATTERY
162+
`$10` | MBC3+TIMER+RAM+BATTERY [^mbc30]
163+
`$11` | MBC3
164+
`$12` | MBC3+RAM [^mbc30]
165+
`$13` | MBC3+RAM+BATTERY [^mbc30]
166+
`$19` | MBC5
167+
`$1A` | MBC5+RAM
168+
`$1B` | MBC5+RAM+BATTERY
169+
`$1C` | MBC5+RUMBLE
170+
`$1D` | MBC5+RUMBLE+RAM
171+
`$1E` | MBC5+RUMBLE+RAM+BATTERY
172+
`$20` | MBC6
173+
`$22` | MBC7+SENSOR+RUMBLE+RAM+BATTERY
174+
`$FC` | POCKET CAMERA
175+
`$FD` | BANDAI TAMA5
176+
`$FE` | HuC3
177+
`$FF` | HuC1+RAM+BATTERY
174178

175179
[^rom_ram]:
176-
No licensed cartridge makes use of this option. Exact behaviour is unknown.
180+
No licensed cartridge makes use of this option. The exact behavior is unknown.
177181

178182
[^mbc30]:
179-
MBC3 with RAM size 64 KByte refers to MBC30, used only in _Pocket Monsters Crystal Version_ for Japan.
183+
MBC3 with 64 KiB of SRAM refers to MBC30, used only in _Pocket Monsters: Crystal Version_ (the Japanese version of _Pokémon Crystal Version_).
180184

181185
## 0148 - ROM Size
182186

183-
Specifies the ROM Size of the cartridge. Typically calculated as "N such that 32 KiB
184-
<< N".
185-
186-
Code | Size | Amount of banks
187-
-----|-----------|-----------------
188-
$00 | 32 KByte | 2 (No ROM banking)
189-
$01 | 64 KByte | 4
190-
$02 | 128 KByte | 8
191-
$03 | 256 KByte | 16
192-
$04 | 512 KByte | 32
193-
$05 | 1 MByte | 64
194-
$06 | 2 MByte | 128
195-
$07 | 4 MByte | 256
196-
$08 | 8 MByte | 512
197-
$52 | 1.1 MByte | 72 [^weird_rom_sizes]
198-
$53 | 1.2 MByte | 80 [^weird_rom_sizes]
199-
$54 | 1.5 MByte | 96 [^weird_rom_sizes]
187+
This byte indicates how much ROM is present on the cartridge.
188+
In most cases, the ROM size is given by `32 KiB × (1 << <value>)`:
189+
190+
Value | ROM size | Number of ROM banks
191+
------|-----------|----------------------
192+
`$00` | 32 KiB | 2 (no banking)
193+
`$01` | 64 KiB | 4
194+
`$02` | 128 KiB | 8
195+
`$03` | 256 KiB | 16
196+
`$04` | 512 KiB | 32
197+
`$05` | 1 MiB | 64
198+
`$06` | 2 MiB | 128
199+
`$07` | 4 MiB | 256
200+
`$08` | 8 MiB | 512
201+
`$52` | 1.1 MiB | 72 [^weird_rom_sizes]
202+
`$53` | 1.2 MiB | 80 [^weird_rom_sizes]
203+
`$54` | 1.5 MiB | 96 [^weird_rom_sizes]
200204

201205
[^weird_rom_sizes]:
202206
Only listed in unofficial docs. No cartridges or ROM files using these sizes are known.
203-
As the other ROM sizes are all powers of 2, these are likely inaccurate. The source for these
204-
values is unknown.
207+
As the other ROM sizes are all powers of 2, these are likely inaccurate.
208+
The source of these values is unknown.
205209

206210
## 0149 - RAM Size
207211

208-
Specifies the size of the external RAM in the cartridge (if any).
212+
This byte indicates how much RAM is present on the cartridge, if any.
209213

210-
Code | Size | Comment
211-
-----|--------|---------
212-
$00 | 0 | No RAM [^mbc2]
213-
$01 | - | Unused [^2kib_sram]
214-
$02 | 8 KB | 1 bank
215-
$03 | 32 KB | 4 banks of 8 KB each
216-
$04 | 128 KB | 16 banks of 8 KB each
217-
$05 | 64 KB | 8 banks of 8 KB each
214+
If the [cartridge type](<#0147 - Cartridge Type>) does not include "RAM" in its name, this should be set to 0.
215+
This includes MBC2, since its 512 × 4 bits of memory are built directly into the mapper.
218216

219-
[^mbc2]:
220-
When using a MBC2 chip, $00 must be specified as the RAM Size, even though
221-
the MBC2 includes a built-in RAM of 512 x 4 bits.
217+
Code | SRAM size | Comment
218+
------|-----------|-----------------------
219+
`$00` | 0 | No RAM
220+
`$01` | – | Unused [^2kib_sram]
221+
`$02` | 8 KiB | 1 bank
222+
`$03` | 32 KiB | 4 banks of 8 KiB each
223+
`$04` | 128 KiB | 16 banks of 8 KiB each
224+
`$05` | 64 KiB | 8 banks of 8 KiB each
222225

223226
[^2kib_sram]:
224-
Listed in various unofficial docs as 2KB. However, a 2KB RAM chip was never used in a cartridge.
225-
The source for this value is unknown.
227+
Listed in various unofficial docs as 2 KiB.
228+
However, a 2 KiB RAM chip was never used in a cartridge.
229+
The source of this value is unknown.
226230

227-
Various "PD" ROMs ("Public Domain" homebrew ROMs generally tagged "(PD)"
228-
in the filename) are known to use the $01 RAM Size tag, but this is believed
229-
to have been a mistake with early homebrew tools and the PD ROMs often don't use
230-
cartridge RAM at all.
231+
Various "PD" ROMs ("Public Domain" homebrew ROMs, generally tagged with `(PD)` in the filename) are known to use the `$01` RAM Size tag, but this is believed to have been a mistake with early homebrew tools, and the PD ROMs often don't use cartridge RAM at all.
231232

232233
## 014A - Destination Code
233234

234-
Specifies if this version of the game is supposed to be sold in Japan,
235-
or anywhere else. Only two values are defined.
235+
This byte specifies whether this version of the game is intended to be sold in Japan or elsewhere.
236236

237-
- $00: Japanese
238-
- $01: Non-Japanese
237+
Only two values are defined:
238+
239+
Code | Destination
240+
------|------------------------------
241+
`$00` | Japan (and possibly overseas)
242+
`$01` | Overseas only
239243

240244
## 014B - Old Licensee Code
241245

242-
Specifies the games company/publisher code in range $00-FF. A value of
243-
$33 signals that the New Licensee Code (in header bytes \$0144-0145) is
244-
used instead. (Super Game Boy functions won't work if \<\> \$33.) A list
245-
of licensee codes can be found
246-
[here](https://raw.githubusercontent.com/gb-archive/salvage/master/txt-files/gbrom.txt).
246+
This byte is used in older (pre-SGB) cartridges to specify the game's publisher.
247+
However, the value `$33` indicates that the [New Licensee Code](<#0144-0145 - New Licensee Code>) must be considered instead.
248+
(The SGB will ignore any [command packets](<#Command Packet Transfers>) unless this value is `$33`.)
249+
250+
Here is [a list of known Old Licensee Codes](https://raw.githubusercontent.com/gb-archive/salvage/master/txt-files/gbrom.txt).
247251

248252
## 014C - Mask ROM Version number
249253

250-
Specifies the version number of the game. That is usually $00.
254+
This byte specifies the version number of the game.
255+
It is usually `$00`.
251256

252257
## 014D - Header Checksum
253258

254-
Contains an 8 bit checksum across the cartridge header bytes $0134-014C.
255-
The boot ROM computes `x` as follows:
259+
This byte contains an 8-bit checksum computed from the cartridge header bytes `$0134``$014C`.
260+
The boot ROM computes the checksum as follows:
256261

257-
```
258-
x = 0
259-
i = $0134
260-
while i <= $014C
261-
x = x - [i] - 1
262+
```c
263+
uint8_t checksum = 0;
264+
for (uint16_t address = 0x0134; address <= 0x014C; address++) {
265+
checksum = checksum - rom[address] - 1;
266+
}
262267
```
263268

264-
If the byte at $014D does not match the lower 8 bits of `x`, the boot ROM will lock up,
265-
and the cartridge program **won't run**.
269+
The boot ROM verifies this checksum.
270+
If the byte at `$014D` does not match the lower 8 bits of `checksum`, the boot ROM will lock up and the program in the
271+
cartridge **won't run**.
266272

267273
## 014E-014F - Global Checksum
268274

269-
Contains a 16 bit checksum (upper byte first) across the whole cartridge
270-
ROM. Produced by adding all bytes of the cartridge (except for the two
271-
checksum bytes). The Game Boy doesn't verify this checksum.
275+
These bytes contain a 16-bit (big-endian) checksum simply computed as the sum of
276+
all the bytes of the cartridge ROM (except these two checksum bytes).
277+
278+
This checksum is not verified, except by Pokémon Stadium's "GB Tower" emulator (presumably to detect Transfer Pak errors).

0 commit comments

Comments
 (0)