# The cartridge Tags as in [README.md](README.md). ## The Game Pak bus - 16-bit data, multiplexed with the low 16 address bits; "the cartridge must include a circuit that latches the lower 16 address bits on non-sequential access, and that increments these bits on sequential access. Nintendo includes this circuit directly in the ROM chip" (GBATEK §GBA Cartridge ROM; `DOC`). Consequences: an S access returns the next halfword whatever address is driven ([timing.md](timing.md) "The ROM's own address counter"), the 128 KB boundary forces N ([timing.md](timing.md)), and reads past the chip return the bus's address lines (U-MEM-3). - Up to 32 MB, seen at `08`, `0A`, `0C` with wait states WS0-2. The backup bus at `0E000000` is 8-bit data, 16-bit address (GBATEK §GBA Cart Backup Flash ROM, "Accessing FLASH Memory"; `DOC`). - /IREQ (pin 31) is the Game Pak interrupt; /DREQ shares the pin (GBATEK §GBA DMA Transfers, "Game Pak DMA"; `DOC`). Removing a cartridge raises it (§GBA Cart Backup SRAM/FRAM "Note"; `DOC`). The board offers no hot-unplug; the line is driven only by the RTC's inverted /IRQ (below). - "Reading from SRAM/FRAM should be performed by code executed in WRAM only (but not by code executed in ROM)" and likewise for Flash (GBATEK; `DOC`): a statement about the shared bus, which the board's bus model satisfies by serialising accesses (HYPOTHESIS: no observable failure is modelled for code that ignores it). ## Header 192 bytes at `08000000` (GBATEK §GBA Cartridge Header; `DOC`): entry branch, Nintendo logo (compressed, checked by the BIOS: "the GBA will compare this data and lock-up itself"), title (`0A0`, 12), game code (`0AC`, 4), maker (`0B0`, 2), fixed `96h` (`0B2`), unit code, device type, version, complement check (`0BD`: `chk = -(sum of 0A0..0BC) - 19h`), multiboot entries from `0C0`. The BIOS verifies logo and checksum; the board does not (it runs the BIOS). `09C` bits 2 and 7 unlock the BIOS's FIQ/undefined handler forwarding; `09E` bits 0-1 select the BIOS's dummy-read key stream (`DOC`; observable only as bus reads during boot). ## Choosing the save device "Nintendo didn't include a backup-type entry in the ROM header, however, the required type can be detected by ID strings in the ROM-image": `EEPROM_Vnnn`, `SRAM_Vnnn`, `FLASH_Vnnn` / `FLASH512_Vnnn` (64 KB), `FLASH1M_Vnnn` (128 KB), word-aligned (GBATEK §GBA Cart Backup IDs; `DOC`). The census also finds `SRAM_F_V` (FRAM library) and `SIIRTC_V` (the RTC library). **Save census** (`python scripts/count_saves.py`, reading `$NO_INTRO_DIR/Nintendo - Game Boy Advance` in place; run 2026-11-19 on the local No-Intro set, 3,487 ROMs, none unreadable). Each ROM is counted under the first ID in the order Flash 1M, Flash 512, Flash, EEPROM, SRAM_F, SRAM: | First ID found | ROMs | Share | | --- | --- | --- | | `EEPROM_V` | 1,553 | 44.5 % | | none | 934 | 26.8 % | | `SRAM_V` | 497 | 14.3 % | | `SRAM_F_V` | 194 | 5.6 % | | `FLASH_V` | 123 | 3.5 % | | `FLASH512_V` | 99 | 2.8 % | | `FLASH1M_V` | 87 | 2.5 % | - `SIIRTC_V` (the RTC): 60 ROMs. More than one save family named: 46 ROMs (a library linked but unused; which one the game uses must come from the game's behaviour or a per-game table: U-CART-1). - ROM sizes (MiB: count): 0 (under 1 MiB): 51, 1: 73, 2: 26, 3: 15, 4: 1,130, 5: 3, 6: 3, 7: 22, 8: 1,492, 10: 1, 11: 2, 14: 1, 16: 578, 18: 1, 32: 89. 89 ROMs are 32 MB, which matters for EEPROM addressing (below). - "none" includes games with no save and games whose library predates or avoids the strings (U-CART-1). The board defaults to no device for them and takes an override. ## SRAM and FRAM 32 KB at `0E000000`-`0E007FFF`, byte access, battery-backed SRAM or FRAM accessed identically, "it should be accessed with 8 waitstates" (GBATEK §GBA Cart Backup SRAM/FRAM; `DOC`). 16/32-bit accesses: [memory-map.md](memory-map.md). Mirroring of the 32 KB inside the 64 KB window and across `0E`-`0F`: jsmolka `save/sram` "Mirror 1", "Mirror 2" (`TEST ROM`, passes on both detectors) and the dispute in U-MEM-2. ## Flash (GBATEK §GBA Cart Backup Flash ROM; `DOC`) - 64 KB or 128 KB (two 64 KB banks) in the backup window, byte access only. - Commands are byte writes to `E005555`/`E002AAA`: `AA`, `55`, then `90` (ID mode; read manufacturer at `E000000`, device at `E000001`), `F0` (leave ID mode), `80` then `AA 55 10` (erase chip) or `AA 55` + `30` at `E00n000` (erase 4 KB sector n), `A0` then one byte (write a byte, to erased memory), `B0` then the bank number at `E000000` (128 KB chips). Atmel chips write 128-byte sectors after `A0` instead. Macronix accepts `F0` at `E005555` to abort after a timeout. - Erased memory reads `FF`; completion is polled by reading the target until the data appears. - IDs (device:manufacturer): SST `D4BF` 64 KB, Macronix `1CC2` 64 KB, Panasonic `1B32` 64 KB, Atmel `3D1F` 64 KB, Sanyo `1362` 128 KB, Macronix `09C2` 128 KB, with GBATEK's timeouts (write, sector erase, chip erase in ms: SST 10/40/200, Macronix 10/2000/2000, Panasonic 10/500/500). - Which ID the board reports: games are supposed to accept any listed chip; the board offers the chip as a setting (`--save-type flash64:sst`, `flash64:macronix`, `flash64:panasonic`, `flash64:atmel`, `flash128:sanyo`, `flash128:macronix128`), defaulting to Panasonic `1B32` for 64 KB and Sanyo `1362` for 128 KB (HYPOTHESIS; any choice is a choice of cartridge, not of GBA behaviour). How long erase and write take (they are busy while polled): average timings "unknown?" in GBATEK. The board completes them at once (HYPOTHESIS, U-CART-2). Judges: jsmolka `save/flash64`, `flash128`; Nintendo's Backup LIB sample, which identifies five of the six chips by name and passes its write test on each (rung 2, [validation.md](validation.md) "Rung 2"). - As built (rung 2, `cartridge/backup.py`): Atmel takes the 128 bytes after `A0` as an erase-and-write page (DOC) and ignores the sector erase GBATEK says it lacks; a 64 KB chip ignores `B0` (HYPOTHESIS); a lone `F0` at `E005555` cancels an armed command on every chip (DOC for Macronix, HYPOTHESIS for the rest); programming can only clear bits (HYPOTHESIS: the usual Flash cell rule behind GBATEK's "must have been previously erased"). **Blind spot:** the Backup LIB sample's Flash library (`FLASH512_V131`) does not recognise Atmel's `3D1F` ("DEVICE: UNKNOWN"), so the Atmel page write has no hardware-side judge yet, only a model test. ## EEPROM (GBATEK §GBA Cart Backup EEPROM; `DOC`) - 512 bytes (6-bit address) or 8 KB (14-bit address field, low 10 bits used), in 64-bit blocks. - Connected to data bit 0 and to the top ROM address line: at `0D000000`-`0DFFFFFF` on carts of 16 MB or less, only `0DFFFF00`-`0DFFFFFF` on 32 MB carts (whose ROM ends at `09FFFEFF`). - Accessed serially, one bit per halfword, by DMA3 only ("manual transfers via LDRH/STRH won't work; probably because they don't keep /CS=LOW and A23=HIGH throughout the transfer"; `DOC`, GBATEK's own hedged reason; U-CART-3). The chip's eight pins are ROMCS, RD, WR, AD0, GND, GND, A23, VDD (GBATEK "Pin-Outs"; `DOC`). - **As built (rung 2): a fresh /CS starts a request.** The board reads GBATEK's reason as: /CS falls anew on every non-sequential cartridge access, and a write arriving with a fresh /CS begins a new request, dropping any unfinished one (HYPOTHESIS). A DMA's first cartridge access is N and the rest S ([timing.md](timing.md)), so one DMA is one request, while separate CPU stores never complete one, which is GBATEK's observation. The read stream a read request prepares survives /CS rising, since a second DMA fetches it (`DOC`: the two transfers). Found by the Backup LIB sample: its EEPROM library first touches the chip with four single CPU accesses (write 1, write 0, two reads, write 1), and with those bits kept the first real request never parsed ("VERIFY ERROR"); with the rule, all six patterns pass. The tag stays HYPOTHESIS: the sample is Nintendo's program written for the real chips (a class R judge), so passing it shows the rule is consistent with how Nintendo's library drives them, not that the chip works this way; another rule that discards the stray bits would pass too. - **As built: busy after a write.** The chip reads 0 for `EEPROM_WRITE_CYCLES` = 108,368 cycles after a write request, then 1 (GBATEK's "ca." figure; `DOC`). The sample's polling passes with it; whether the exact length matters to any program is untested. - Read: send `11`, the address (MSB first), `0`; then read 68 bits: 4 ignored, 64 data (MSB first). - Write: send `10`, the address, 64 data bits, `0`; "it'll take ca. 108368 clock cycles (ca. 6.5ms) until the old data is erased and new data is programmed"; reads return bit 0 = 1 when ready. - Size: "There seems to be no autodection mechanism, so that a hardcoded bus width must be used." The request lengths differ (read request 9 bits for 512 B, 17 for 8 KB; write 73 or 81 bits: arithmetic from GBATEK's bit counts; `DOC`), so the board can fix the size from the first request's DMA length (HYPOTHESIS: a board convention, not GBA behaviour; a per-game override remains: `--save-type eeprom512` or `eeprom8k`). A battery file of 512 or 8,192 bytes also fixes it. - The backup bus at `0E000000` on an EEPROM cartridge has no chip and reads `FF`, as with no save (HYPOTHESIS; the board had no answer there before rung 2 and crashed when the sample probed it). - The Backup LIB sample's EEPROM test says "4K EEPROM" whatever is fitted: it judges the 512-byte chip only. The 8 KB chip's 14-bit requests are checked by a model test. The AGS aging cartridges (rung 6) are 512-byte EEPROM cartridges too (the board's battery files for them are 512 bytes): they write their defaults to a blank chip, save a changed CONFIGURATION setting and read it back after a power cycle (validation.md "Rung 6"), which judges the 512-byte chip again; a judge for the 8 KB chip is still owed. - Wait states: 8,8 in WS2 (`WAITCNT=X3XXh`). ## GPIO port and the S-3511 RTC **GPIO** (GBATEK §GBA Cart I/O Port (GPIO); `DOC`): a 4-bit port in the ROM chip at `080000C4` (data, bits 0-3), `080000C6` (direction, 1 = out), `080000C8` (bit 0: 1 = readable, 0 = write-only). "In write-only mode, reads return 00h (or possible other data, if the rom contains non-zero data at that location)" (so the ROM's bytes, which are zero in the image; HYPOTHESIS: the board returns ROM data when write-only). STRB to the port is ignored "only in DS mode?" (open; U-CART-4). The chip also inverts the RTC's /IRQ onto the cartridge's /IRQ pin. **RTC wiring** (Boktai): GPIO bit 0 = SCK, bit 1 = SIO, bit 2 = CS (§GBA Cart I/O Port, "Connection Examples"; `DOC`). **Protocol** (GBATEK §GBA Cart Real-Time Clock, which points to §DS Real-Time Clock; `DOC`): - CS low with SCK high, then CS high; a command byte; parameter bytes; CS low. Each bit: SCK low with SIO = bit (when writing), SCK high, read SIO (when reading); "data is output on (or immediately after) falling edge." GBATEK recommends LSB-first for commands and parameters and gives both bit orders in its command table. - Command byte: fixed code `0110` in the high nibble (MSB-first reading of the DS table), a 3-bit register number, and bit 0/7 read (1) or write (0). - GBA registers (§GBA Cart Real-Time Clock, "Comparision of RTC Registers"): control (1 byte; NDS stat2), date-time (7 bytes: year, month, day, weekday, hour, minute, second, BCD), time (3 bytes), force reset (0 bytes; NDS stat1), force IRQ (0 bytes; NDS clock adjust), and three registers that read `FF`. - Control: bit 1 "IRQ duty/hold related?", bit 3 per-minute IRQ (30 s duty), bit 5 unknown, bit 6 24-hour mode, bit 7 power-off flag (read-only, cleared on read). `82h` after a battery short, `00h` after force reset. - Hour: AM/PM in bit 7 on the GBA (bit 6 on the DS). - Force reset: all registers `00` except day and month `01`; both strobe registers act on any access, read or write. - Which game reads it: 60 ROMs carry `SIIRTC_V` (census). Rung 2's judge is Pokémon Emerald accepting the clock (handoff-brief.md). - Open: the S-3511's exact behaviour where it differs from the DS's S-35180 (GBATEK: "(almost) the same"), what invalid BCD writes do, the IRQ pulse shapes (U-CART-5). **As built (rung 2, `cartridge/rtc.py`)**: - The command byte has one order on the wire. GBATEK's "Fwd" column (LSB first) and "Rev" column (MSB first) describe the same eight bits: sent first are the fixed `0`, `1`, `1`, `0`, then the register number most significant bit first, then read/write (`DOC`, by reading the two columns against each other). Parameters go LSB first (`DOC`: both conventions GBATEK names agree on data). The chip samples SIO on SCK's rising edge and drives it after the falling edge (`DOC`). - GBA registers by their "Rev" numbers: 0 force reset, 1 control, 2 date and time, 3 time, 6 force IRQ; 4, 5 and 7 read `FF` (`DOC`). - Hour in 24-hour mode carries the PM flag in bit 7 for 12-23 h (`DOC`: the DS's 24 h rule, with the flag "moved from hour.bit6 (NDS) to hour.bit7 (GBA)"). Control's writable bits are 1, 3, 5, 6; bit 7 (power off) is read-only and cleared by a read (`DOC`). - A write of an impossible date or time is ignored whole (HYPOTHESIS, U-CART-5). - Force IRQ and the per-minute IRQ are not modelled: the cartridge /IRQ is not wired to the Game Pak interrupt (open, U-CART-5). - The GPIO data register reads back written levels on output pins and the chip's SIO on an input pin; SCK, CS and bit 3 read 0 as inputs (HYPOTHESIS: GBATEK gives no read-back rule). In write-only mode the three halfwords read the ROM's own data (`DOC`). The port answers at the same cartridge address in all three ROM windows (HYPOTHESIS). - **Fitting:** the RTC and port are fitted when `SIIRTC_V` is in the ROM, word-aligned (OBSERVED in the ROM images: the RTC library's ID string, 60 ROMs; HYPOTHESIS that it means the chip is there), or by `--rtc on|off`. - **The clock** (board convention): set at power-on to the host's local time, or `--rtc-start`, and counted in emulated time, one second per 2^24 cycles, so a run is repeatable for a given start. A fresh cartridge starts with control `40h` (24-hour mode, power flag clear: a cartridge whose battery is good; HYPOTHESIS); `RTC(battery_lost=True)` gives GBATEK's `82h` after a battery short. - Judge: Pokémon Emerald accepts the clock (main menu, not "The internal battery has run dry"), and shows that message with the clock removed ([validation.md](validation.md) "Rung 2"). ## Battery files A board convention (`cartridge/battery.py`; `--save FILE.sav` reads it if present and writes it when the run ends): - `FILE.sav`: the save chip's memory as a raw image and nothing else: 32,768 bytes (SRAM/FRAM), 65,536 or 131,072 (Flash, bank 0 then bank 1), 512 or 8,192 (EEPROM, 64-bit blocks in address order, each block's bytes in the order the chip sends them, most significant bit first). This is the layout `.sav` files commonly have, so they move between emulators. No file for a cartridge without a chip; a short file fills the start of the chip and leaves the rest erased (`FF`). - `FILE.rtc` (cartridges with the clock): JSON with the clock's date and time when written, its control register, its weekday offset and the host's time then. Loading adds the host time that has passed, as the cartridge's own battery keeps the chip counting. ## Not in scope (listed for later) Solar sensor (GPIO, a 74LV4040 counter and comparator, Boktai), tilt sensor (Yoshi), gyro and rumble (WarioWare Twisted, Drill Dozer), e-Reader, DACS debug memory, flash carts and cheat devices (GBATEK §GBA Cart Solar Sensor ... §GBA Cheat Devices; `DOC`).