# Memory map Tags as in [README.md](README.md). "GBATEK §X" is the section titled X in the pinned GBATEK. ## Regions The address bus is 28 bits wide for the CPU's purposes: `10000000`-`FFFFFFFF` is unused (GBATEK §GBA Memory Map, "upper 4bits of address bus unused"; `DOC`). | Range | Region | Size | Bus | CPU reads | CPU writes | Cycles 8/16/32 (default) | Source | | --- | --- | --- | --- | --- | --- | --- | --- | | `00000000`-`00003FFF` | BIOS ROM | 16 KB | 32 | 8/16/32, protected | none | 1/1/1 | GBATEK §GBA Memory Map; `DOC` | | `00004000`-`01FFFFFF` | unused | | | open bus | ignored | 1 (HYPOTHESIS) | GBATEK §GBA Unpredictable Things | | `02000000`-`02FFFFFF` | EWRAM (on-board) | 256 KB, mirrored | 16 | 8/16/32 | 8/16/32 | 3/3/6 | GBATEK §GBA Memory Map; `DOC` | | `03000000`-`03FFFFFF` | IWRAM (on-chip) | 32 KB, mirrored | 32 | 8/16/32 | 8/16/32 | 1/1/1 | `DOC` | | `04000000`-`040003FE` | I/O | | 32 | 8/16/32 | 8/16/32 | 1/1/1 | `DOC` | | `05000000`-`05FFFFFF` | palette RAM | 1 KB, mirrored | 16 | 8/16/32 | 16/32 (8: see below) | 1/1/2 (+1 on contention) | `DOC` | | `06000000`-`06FFFFFF` | VRAM | 96 KB, mirrored in 128 KB | 16 | 8/16/32 | 16/32 (8: see below) | 1/1/2 (+1 on contention) | `DOC` | | `07000000`-`07FFFFFF` | OAM | 1 KB, mirrored | 32 | 8/16/32 | 16/32 (8 ignored) | 1/1/1 (+1 on contention) | `DOC` | | `08000000`-`09FFFFFF` | Game Pak ROM, wait state 0 | up to 32 MB | 16 | 8/16/32 | GPIO only | 5/5/8 (N), see [timing.md](timing.md) | `DOC` | | `0A000000`-`0BFFFFFF` | Game Pak ROM, wait state 1 | same ROM | 16 | | | | `DOC` | | `0C000000`-`0DFFFFFF` | Game Pak ROM, wait state 2 (EEPROM lives here) | same ROM | 16 | | | | `DOC` | | `0E000000`-`0FFFFFFF` | Game Pak backup (SRAM, Flash) | 64 KB window | 8 | 8 (16/32: see below) | 8 | 5 | `DOC` | GBATEK's cycle column is the default wait-state setting; "plus 1 cycle if GBA accesses video memory at the same time" for OAM, palette and VRAM (§GBA Memory Map, "Timing Notes"; `DOC`). CowBite and gbadoc list palette RAM's port as 16-bit and OAM's as 32-bit, agreeing. Only DMA3 and the CPU reach the Game Pak ROM; the backup bus is CPU-only, bytewise (GBATEK §GBA Memory Map, "GamePak Memory"; §GBA DMA Transfers; `DOC`). DMA0's source and DMA0-2's destinations are limited to internal memory (27-bit address), DMA1-3 sources and DMA3's destination to 28 bits (§GBA DMA Transfers; `DOC`). ## Mirrors | Region | Mirror rule | Source | | --- | --- | --- | | EWRAM | every 256 KB across `02xxxxxx` | GBATEK §Memory Mirrors; gbadoc memory.md; `DOC`. mGBA suite "EWRAM mirror load/store" (`TEST ROM`) | | IWRAM | every 32 KB across `03xxxxxx` (so `03FFFFFC` is `03007FFC`, the BIOS's IRQ-handler pointer) | GBATEK §Memory Mirrors, §BIOS Interrupt handling; `DOC` | | Palette | every 1 KB across `05xxxxxx` | `DOC`; mGBA suite "Palette mirror" | | VRAM | 128 KB pattern repeated across `06xxxxxx`: 64 KB BG, 32 KB OBJ, then the OBJ 32 KB again at `06018000`-`0601FFFF` | GBATEK §Memory Mirrors; gbadoc; `DOC`. mGBA suite "VRAM OBJ mirror (2)" | | VRAM, bitmap modes 3-5 | `06018000`-`0601BFFF` reads and writes "do not work": writes discarded, reads return 0 in hw-test's run (`M3 18000h: 0x0`) | hw-test `ppu/vram-mirror` README and source (passes on GBA and GBA SP, not on DS/3DS); `TEST ROM` | | OAM | every 1 KB across `07xxxxxx` | `DOC` | | BIOS, Game Pak ROM, I/O | not mirrored, except the word at `04000800`, repeated every 64 KB (`04010800`, ...) | GBATEK §Memory Mirrors, §GBA I/O Map; gbadoc; `DOC` | | Game Pak ROM in three windows | the same ROM at `08`, `0A`, `0C` (different wait states) | `DOC` | | Backup (SRAM) | GBATEK: the 64 KB area mirrored across `0E000000`-`0FFFFFFF`, a 32 KB chip twice inside it | GBATEK §Memory Mirrors (`DOC`); **disputed**: jsmolka's unsafe/README says this "passes on No$GBA and mGBA but not on real hardware" (with a flash cart, which may be the cause). [undocumented-behavior.md](undocumented-behavior.md) U-MEM-2 | The mirroring of VRAM's last 32 KB is explained by hw-test's README as "VRAM accesses above `06010000` (text modes) or `06014000` (bitmap modes) are always passed through the OBJ engine", which in bitmap modes cannot reach the bank at `06010000`-`06013FFF` (`TEST ROM` prose, hypothesis about the mechanism in the author's words). ## Access widths and alignment - **Little-endian** always (GBATEK §GBA Memory Map, "Data Format"; `DOC`). - **Misaligned loads** are the CPU's concern (rotation of LDR, LDRH; LDRSH's byte form): the core does them. The memory returns the aligned unit; the core passes the full address and "aligning it is the memory's job" (arm7tdmi-python `ARM7TDMI` docstring). mGBA suite's memory tests list every region's expected value for every width and misalignment (src/memory.c; `TEST ROM`). - **8-bit writes to video memory** (GBATEK §GBA Unpredictable Things, "Writing 8bit Data to Video Memory"; `DOC`): to BG VRAM (`06000000`-`0600FFFF`, or `06013FFF` in bitmap modes) and palette RAM the byte is written to both halves of the halfword (`[addr & ~1] = data * 0x101`); to OBJ VRAM (`06010000`, or `06014000` in bitmap modes, upward) and OAM the write is ignored. Judges: jsmolka `memory/video_strb`, alyosha-tas `memory/PAL_8_bit_writes`, `VRAM_8_bit_writes`. - **Backup bus at 16/32 bits** (§GBA Unpredictable Things, "Accessing SRAM Area by 16bit/32bit"; `DOC`): a read returns the byte at the address times `0x0101` (LDRH) or `0x01010101` (LDR); a write stores only the byte `(data ROR (address * 8)) & 0xFF` at the address. Judge: jsmolka `save/sram` ("Load half", "Load word", "Store half/word position"), mGBA suite "SRAM load/store". - **Game Pak ROM writes** reach the ROM chip's GPIO port at `080000C4`-`080000C9` ([cartridge.md](cartridge.md)); elsewhere nothing changes (HYPOTHESIS for a plain ROM: there is nothing to write). ## Open bus "Most of the below is caused by 'traces' from previous operations which have used the databus" (GBATEK §GBA Unpredictable Things; `DOC`). All values below are 32-bit; a narrower read takes its byte or halfword from the 32-bit value at the address's alignment. **CPU reads of unused memory** (`00004000`-`01FFFFFF`, `10000000`-`FFFFFFFF`, and `02`/`03` when disabled through `04000800`) return the most recently prefetched opcode (§GBA Unpredictable Things, "Reading from Unused Memory"; `DOC`): | State and where the code runs | 32-bit value | | --- | --- | | ARM | `[$+8]` | | Thumb in EWRAM, palette, VRAM, Game Pak ROM | LSW = `[$+4]`, MSW = `[$+4]` | | Thumb in BIOS or OAM | at a 4-aligned opcode: LSW = `[$+4]`, MSW = `[$+6]`; otherwise LSW = `[$+2]`, MSW = `[$+4]` | | Thumb in IWRAM (GBA, SP, Micro) | at a 4-aligned opcode: LSW = `[$+4]`, MSW = OldHI; otherwise LSW = OldLO, MSW = `[$+4]`, where Old = `[$+2]` unless the previous opcode was itself a load, then the halves of the data it read | GBATEK's note: it is the CPU pipeline's prefetch, not the Game Pak prefetch buffer. Its "Theoretically, this might also change if a DMA transfer occurs" is answered by the tests below. The board models this as a **bus latch**: the last 32-bit value on the CPU's data bus (an opcode fetch's word, both halves of a 16-bit fetch on a 16-bit bus, a data read), which unused reads return (HYPOTHESIS for the model; it reproduces every row of the table by construction of the 16- vs 32-bit buses; judges below). **Unused and write-only I/O** "works like above Unused Memory when the entire 32bit memory fragment is Unused (eg. 0E0h) and/or Write-Only (eg. DMA0SAD). And otherwise, returns zero if the lower 16bit fragment is readable" (§GBA Unpredictable Things; `DOC`). mGBA suite's "I/O read tests" list each register's readable bits (src/io-read.c; `TEST ROM`). **DMA and the bus latch** (hardware tests; `TEST ROM`): - Each DMA channel has its own 32-bit latch. A 16-bit DMA read puts the halfword in both halves of its latch; a 16-bit DMA write takes the latch's low or high half by the destination's alignment (hw-test `dma/latch`: `DMA LATCH 0/1/2`). - A DMA read from below `02000000` (the BIOS, unused space) does not update its latch: the old latch value is written (hw-test `dma/latch`, "technically we only test 0x00000000"). - A DMA read of an unreadable I/O address does update the latch, with the CPU's open-bus value (the opcode prefetched before the DMA started: `BUS LATCH 0x46C046C0` after a Thumb NOP sled; hw-test `dma/latch`). - "DMA updates the cpu bus. In particular, DMA (16 bit) from 0x04010000 updates cpu open bus (both 16 bit halves.) 32 bit DMA from addresses it cannot access do not update the DMA bus, but do copy it to the cpu bus. 16 bit DMA from these addresses copy the read half of the DMA bus to both halves of the cpu bus. However 16 bit cpu reads from unused memory ... do not update the cpu bus" (alyosha-tas `Bus/ReadMe.txt`; `TEST ROM`, judged by `Bus/*.gba`). **Game Pak ROM beyond the chip**: "Because Gamepak uses the same signal-lines for both 16bit data and for lower 16bit halfword address, the entire gamepak ROM area is effectively filled by incrementing 16bit values (Address/2 AND FFFFh)" when no cartridge is inserted (GBATEK §GBA Unpredictable Things; `DOC`). The mGBA suite expects the same beyond the end of its own ROM ("ROM out-of-bounds load" at `092468AC` reads `0x3456`, a 32-bit read `0x34573456`; src/memory.c; `TEST ROM`). jsmolka's unsafe/README reports the opposite on hardware ("passes on mGBA but not on No$GBA or real hardware", run from a flash cart whose ROM is larger than the test). [undocumented-behavior.md](undocumented-behavior.md) U-MEM-3. ## BIOS read protection "The GBA allows to read opcodes or data only if the program counter is located inside of the BIOS area. If the program counter is not in the BIOS area, reading will return the most recent successfully fetched BIOS opcode (eg. the opcode at [00DCh+8] after startup and SoftReset, the opcode at [0134h+8] during IRQ execution, and opcode at [013Ch+8] after IRQ execution, and opcode at [0188h+8] after SWI execution)" (GBATEK §GBA Unpredictable Things; `DOC`). - The board keeps a **BIOS latch**: the last word fetched from the BIOS as an opcode while the PC was in it; BIOS reads with the PC outside return it (`DOC`, model as stated). - The addresses GBATEK names belong to Nintendo's BIOS. With Cult-of-GBA's BIOS the values differ; a judge expecting Nintendo's values passes only with Nintendo's image (`HYPOTHESIS` that every such judge was taken with Nintendo's BIOS; jsmolka `bios/bios.gba` fails on both detectors with the free BIOS and passes with the retail one: `OBSERVED`, [validation.md](validation.md)). - "PC in the BIOS" is judged at which point of the pipeline (the fetch address, or the executing instruction's address)? GBATEK does not say. [undocumented-behavior.md](undocumented-behavior.md) U-MEM-1. Judges: jsmolka `bios`, alyosha-tas `bios`, `Bus/DMA_Outside_Bios_Bus`, `timing/dma_from_bios`, Hades-Tests `bios-openbus` (run only). - DMA reading the BIOS: see "DMA and the bus latch" (the latch is not updated; the old value is written). ## I/O map The full register list is GBATEK §GBA I/O Map (`DOC`); [video.md](video.md), [sound.md](sound.md), [timing.md](timing.md) and [cartridge.md](cartridge.md) define each block. Summary: | Range | Block | | --- | --- | | `000`-`056` | LCD: DISPCNT, green swap (`002`), DISPSTAT, VCOUNT, BG0-3CNT, scroll, affine, windows, MOSAIC, BLDCNT/BLDALPHA/BLDY | | `060`-`0A7` | sound: the Game Boy channels' registers, SOUNDCNT_L/H/X, SOUNDBIAS, wave RAM (`090`-`09F`), FIFO_A (`0A0`), FIFO_B (`0A4`) | | `0B0`-`0DF` | DMA0-3: SAD, DAD, CNT_L, CNT_H | | `100`-`10F` | TM0-3: CNT_L (counter/reload), CNT_H | | `120`-`12B`, `134`-`15B` | serial: SIODATA/SIOMULTI, SIOCNT, SIODATA8/SIOMLT_SEND, RCNT, JOYCNT, JOY_RECV, JOY_TRANS, JOYSTAT | | `130`-`133` | KEYINPUT (read-only, 0 = pressed, bits 0-9), KEYCNT | | `200`-`20B` | IE, IF (write 1 to acknowledge), WAITCNT, IME | | `300`, `301` | POSTFLG (byte), HALTCNT (byte, write-only) | | `410` | undocumented: the BIOS writes `0xFF`, purpose unknown | | `800` | undocumented internal memory control, mirrored every 64 KB | **Internal memory control `04000800`** (GBATEK §GBA System Control; `DOC`): reset value `0D000020` "by hardware". Bit 0: disable 32K+256K WRAM ("when off: empty/prefetch"; GBATEK quotes endrift that bit 0 swaps `00000000`-`01FFFFFF` with `02000000`-`03FFFFFF` while keeping BIOS protection: two statements, not reconciled). Bits 1-2: unknown, read/write. Bit 3: disable the CGB boot ROM. Bit 5: enable 256K WRAM (when off: the 32K WRAM mirrored there). Bits 24-27: EWRAM wait control, `n` = 0-14 selects 15-n wait states (default 13 = 2 waits = 3/3/6 cycles; 14 = 1 wait = 2/2/4, which locks up a GBA Micro; 15 = lockup). Bits 28-31: unknown, read/write. Not present on the DS in GBA mode. The board models bits 0, 5 and 24-27 at the documented level; the rest as storage (HYPOTHESIS: no judge). **POSTFLG `04000300`** (GBATEK §GBA System Control; `DOC`): the BIOS sets it to 1 after the first boot; a later jump to the reset vector with it set takes the debug path. hw-test `haltcnt` shows a halfword write of 0 to `04000300` (CpuSet) enters HALT and leaves POSTFLG reading 1 (`POSTFLG: PASS 1`; `TEST ROM`). Whether POSTFLG can be cleared by any write is not established (U-SYS-1). **HALTCNT `04000301`**: bit 7 = 0 halt, 1 stop ([timing.md](timing.md) "HALT and STOP"). ## Reset state | Item | Value | Source | | --- | --- | --- | | CPU | Supervisor mode, ARM, I and F set, PC = 0, refill fetches at 0 and 4 | ARM TRM (DDI 0029G) §2.10, §3.10 via the core's `reset()` docstring; `DOC` | | WAITCNT | `0000` (ROM 4/2 waits, SRAM 4, prefetch off) | GBATEK §GBA System Control, §GBA Cartridge ROM; `DOC` | | `04000800` | `0D000020` | GBATEK §GBA System Control; `DOC` | | KEYINPUT | `03FF` (nothing pressed) | GBATEK §GBA Keypad Input; `DOC` | | SOUNDBIAS | the BIOS sets `0200` ("default setting is 0200h") | GBATEK §GBA Sound Control Registers; `DOC`. Its value before the BIOS runs: unknown (HYPOTHESIS 0) | | Everything else | zero, except what the BIOS writes | HYPOTHESIS; a board that boots the BIOS inherits the BIOS's settings | | Memory | EWRAM, IWRAM, VRAM, OAM, palette "initialized to zero by the BIOS" | gbadoc memory.md (`DOC`); contents at power-on before the BIOS: unknown (HYPOTHESIS 0) | **Starting without the BIOS's boot** (the "skip" start in tests/smoke_set.txt): the state the BIOS leaves when it jumps to `08000000` must be documented from evidence before the board offers it. What is documented: SP_svc = `03007FE0`, SP_irq = `03007FA0`, SP_usr/sys = `03007F00`, system mode at the entry point (GBATEK §GBA Cartridge Header "With all entry points, the CPU is initially set into system mode"; §BIOS Interrupt handling; SoftReset in §BIOS Reset Functions; `DOC`), POSTFLG = 1, the RAM ranges SoftReset clears. Everything else (DISPCNT, SOUNDBIAS, timers, the BIOS latch value, the prefetch state) is HYPOTHESIS until measured against a boot with the retail BIOS on the board itself: the archived brief records that NanoBoyAdvance's own skip "leaves state that breaks some games" (handoff-brief.md). Plan: take the skip state from the board's own retail-BIOS boot at the moment it reaches `08000000`, and record it as `FITTED` (U-SYS-2).