# Thumb `0xDExx` / `0xE8xx` on a real GBA A hardware test ROM for user decision 4 (`docs/handoff-brief.md`, items C7 and C8 in `docs/undocumented-behavior.md`). The ARMv4T documents call these Thumb encodings undefined, and no hardware result is known. Emulators disagree about them: | Encoding | SingleStepTests / NanoBoyAdvance | mGBA | ares | | --- | --- | --- | --- | | `0xDE00–0xDEFF` (B with condition 1110) | branch | undefined trap | undefined trap | | `0xE800–0xEFFF` (ARMv5's BLX suffix) | nothing (R15 not advanced) | no-op for `E800–E83F`, trap above | undefined trap | The core takes the undefined trap for both ranges **provisionally**. What this ROM shows on a GBA decides. `thumb_undefined.gba` (3 KiB, SHA-256 `fb226f92…e636`) is built locally and not committed: its header must carry the cartridge logo, which is Nintendo's artwork, so the build copies it from a cartridge image you supply ("Building"). ## Running it on hardware 1. Copy `thumb_undefined.gba` to a flash cart (EverDrive, EZ-Flash, or any cart that runs homebrew). If the cart asks for a save type, choose **SRAM** (the ROM carries `SRAM_V113`). 2. Boot it on a GBA, GBA SP or Game Boy Player **with the normal Nintendo logo boot**. The ROM relies on the retail BIOS's undefined-instruction handler (see "How it works"). Don't use a cart's "skip BIOS" or soft-reset mode, and don't run it on a DS or 3DS. 3. The 12 rows appear in about a second after the logo, then the ROM sits still. Photograph the screen. 4. Optionally, copy the `.sav` back and decode it: `python tests/hw/thumb_undefined/run_referees.py --decode thumb_undefined.sav`. 5. Fill in "Hardware result" below. ## Reading the screen One row per probe: the opcode, the outcome box, then three hex numbers. | Box | Letter | Meaning | | --- | --- | --- | | green | **F** | ran as a no-op: the next instruction ran next, in NOP time | | blue | **B** | a taken branch: into the landing sled, to itself (`DEFE`), or to the next instruction in branch time (`DEFF`) | | red | **T** | the undefined-instruction trap: the retail BIOS's trap loop was seen | | yellow | **X** | something else: the third number is where the CPU was found | | magenta | **?** | fell through, but in a time that matches neither the NOP nor the B control | | grey | **P** | pending: this probe never finished (the machine hung in it) | The numbers after the box: - **Cycles** (hex): measured from the probe to the next instruction. For F and B rows only. - **r7**: how many times the instruction before the probe ran. 1 is normal; hundreds means the trap loop. - **Address** (hex): for F or B, the next or target instruction. For T or X, where the timer IRQ found the CPU. The first three rows are controls and must read **F, B, T**: - `46C0` (MOV R8,R8) must be F. - `E7FF` (B to the next instruction) must be B. - `E7F000F0` (an ARM encoding ARMv4 reserves as undefined) must be T. If a control is wrong, the run says nothing about the probes. On real hardware F should be 0008 cycles and B 000A, as on both emulators below. The probes: - `DE00`, `DE01` and `DE7F` branch to three different sled entries. - `DEFE` would branch to itself. - `DEFF` would branch to the next instruction, so only its time can tell a branch from a no-op. - `E800`, `E83F`, `E840` and `EFFF` sit either side of the `E83F` boundary mGBA shows when run (below). ## How it works - **The BIOS loop.** The retail BIOS's undefined handler (vector `0x04`), with cartridge byte `0x9C` not `0xA5`, saves R14_und at `0x03007FEC` and returns with `SUBS PC, R14, #4`. For a Thumb instruction that lands 2 bytes *before* the faulting one. So on a GBA a Thumb undefined instruction loops forever, re-running the instruction before it (verified by disassembling the BIOS, SHA-1 `300c20df…`). - **The counter.** Each probe sits right after `ADDS R7, #1`, so a trap shows as r7 climbing, and R14_und = probe + 2 at `0x03007FEC` confirms it. - **The escape.** Timer 0 is armed for 4096 cycles before each probe with its IRQ enabled. Code that finishes stops it first. Code that doesn't is caught by the IRQ handler, which records the interrupted PC and CPSR and returns to a landing routine. - **Pending first.** Each row is drawn grey before its probe runs, and every record goes to SRAM, pending first. So even a total hang identifies the probe that caused it. - **Branch targets.** - For `DExx`, the probe sits before a sled of 128 `ADDS R6, #1`. The count left in r6 gives the landing point. - For `E8xx`, LR points into 64 KiB of zeroed EWRAM, where zeros are a harmless no-op in both ARM and Thumb state. - So a BL-suffix or BLX-suffix reading of `E8xx` would show as X, with an address near `0x02020000 + offset * 2`. The CPSR's T bit (in SRAM) says which state it was in. - **Timing.** The probes run from IWRAM: no wait states, no prefetch buffer. Timer 1 counts cycles across each probe. With Cult-of-GBA's free BIOS the ARM control hangs (row 3 stays grey): its undefined handler differs. The ROM is for the retail BIOS only. ## What the referees showed (2026-10-21, retail BIOS) `python tests/hw/thumb_undefined/run_referees.py` runs gba-python's headless drivers and saves `referee-mgba.png` and `referee-nba.png`. | Probe | mGBA `fd9dc5c` | NanoBoyAdvance 1.8.3 | | --- | --- | --- | | `46C0` control | F, 8 cycles | F, 8 | | `E7FF` control | B, 10 cycles | B, 10 | | `E7F000F0` control | T | T | | `DE00` | T (r7 = 931, R14_und = probe + 2) | B to probe + 4 | | `DE01` | T | B to probe + 6 | | `DE7F` | T | B to probe + 258 | | `DEFE` | T | B to itself | | `DEFF` | T | B (10 cycles, as the B control) | | `E800` | F, 8 cycles | X at probe + 260 | | `E83F` | F, 8 cycles | X at probe + 260 | | `E840` | T | X at probe + 260 | | `EFFF` | T | X at probe + 260 | So the ROM separates every behaviour in dispute. - NanoBoyAdvance's X (run closed box): R15 is not advanced past the probe, so the following B computes its target 2 bytes short and lands on the sled's final B. - No real branch can land there, because `DExx` reaches probe + 258 at most. ## Hardware result | Probe | Outcome | Cycles | r7 | Address | | --- | --- | --- | --- | --- | | `46C0` | | | | | | `E7FF` | | | | | | `E7F000F0` | | | | | | `DE00` | | | | | | `DE01` | | | | | | `DE7F` | | | | | | `DEFE` | | | | | | `DEFF` | | | | | | `E800` | | | | | | `E83F` | | | | | | `E840` | | | | | | `EFFF` | | | | | Console (model, and GBA SP or Game Boy Player): ____ Date: ____ Cart: ____ ## Building ARM_NONE_EABI_BIN=DIR GBA_LOGO_FROM=CART.gba python tests/hw/thumb_undefined/build.py builds with an arm-none-eabi toolchain (the Arm GNU Toolchain 14.3.rel1 that gba-python pins was used; `--toolchain DIR` also works). The header logo is copied from bytes 0x04-0x9F of any GBA cartridge image you own (`--logo-from`, or `GBA_LOGO_FROM`) and checked against its SHA-256. The build is reproducible: with that toolchain it gives `fb226f92…e636` byte for byte. The files: - `crt0.s`: the header and start-up. - `probes.s`: the probe blocks, the landing routine and the IRQ handler. - `main.c`: the probe list, classification, screen and SRAM. - `gba.ld`: the linker script. ## SRAM layout (version 1) Offset 0 holds `"TUND"`, then the version, the probe count, and `'D'` once every probe has finished. A 32-byte record per probe starts at `0x10 + 32 * i`, all little-endian: | Bytes | Field | | --- | --- | | 0–3 | opcode | | 4 | outcome letter | | 5 | state (0 Thumb, 1 ARM) | | 6–7 | cycles | | 8–11 | address | | 12–15 | caught CPSR | | 16–19 | R14_und from `0x03007FEC` | | 20–23 | r7 | | 24–27 | r6 | | 28–31 | the probe's return path (1 fell, 2 sled, 3 caught) |