# Disassembly `arm7tdmi_python.disasm` turns ARM words and Thumb halfwords into text without touching the CPU. It classifies every opcode by the handler name the core's decode tables (`_tables.py`, generated by `scripts/gen_tables.py`) give it, the same lookup `ARM7TDMI.step()` makes. So the text always says what the core will run, including for the encodings the documents leave undefined. ```python from arm7tdmi_python.disasm import disassemble, disassemble_arm, disassemble_thumb disassemble_arm(0xE59F1004, 0x08000000).listing # 'LDR R1, [PC, #0x4] ; =[0x0800000C]' disassemble_thumb(0xF000, 0x08000000, 0xF810).text # a BL pair, combined # 'BL 0x08000024' disassemble(memory.__getitem__, pc, thumb=True) # from a side-effect-free byte peek ``` An `Instruction` has `address`, `data` (2 or 4 bytes), `mnemonic`, `operands`, `comment`, `thumb` and `target` (the absolute branch target of B, BL and Bcc). `text` is the assembly and `listing` adds the comment after `;`. ## Syntax ARM's own pre-UAL syntax is used, as in the data sheet (ARM DDI 0029E, chapters 4 and 5) and the ARMv4T Architecture Reference Manual. This follows the family convention of using the manufacturer's notation: Zilog's for the Z80, Motorola's for the 6800 and 68000. - Upper case. The condition comes before `S` and before the size: `ADDEQS`, `LDREQB`, `LDRNESH`, `STRGTH`, `LDRBT`. - R13–R15 are written `SP`, `LR`, `PC`. - Block transfers use the addressing-mode names (`LDMIA`, `STMDB`), not the stack names, even with SP as the base. - MSR writes its field mask as letters in the order f, s, x, c (`CPSR_fc`). A mask of 0 prints `CPSR`, with the comment "no fields". - Immediates are hexadecimal (`#0x1F`). Shift amounts are decimal (`LSL #3`). Immediate `LSR #0` and `ASR #0` print as `#32`, and `ROR #0` prints as `RRX`, because that is what they do (DS 4.5.2, U10). - A rotated immediate prints as its value, `#0xFF000000`. When the encoding is not the one with the smallest rotation it prints `#imm8, rot`, for example `MOV R0, #0x3F, 30`. The rotation decides the shifter's carry for a flag-setting logical operation, so two encodings of the same value are not the same instruction (DS 4.5.3). - A compare with Rd = R15 prints `TEQP` etc., ARM's old name for the form that also copies SPSR to CPSR (U13). - A post-indexed zero offset prints `[Rn], #0x0`: `[Rn]` alone is the pre-indexed encoding, so the two stay apart. - A post-indexed LDC/STC writes back only when W = 1 (DS 4.15, unlike LDR/STR, which always write back when post-indexed), and DS 4.15 writes that as a trailing `!`: `LDC p1, c1, [R0], #0x10!`. Without the `!` the base is kept. - Branch targets are absolute addresses. - A PC-relative load prints its operand (`[PC, #0x4]`) and puts the address it reads in the comment (`=[0x0800000C]`). `ADD Rd, PC, #n` does the same with the address it computes (`=0x08000018`). Tests check these addresses against the ones the core actually drives. - Thumb uses the same names. A Thumb data-processing instruction sets flags without an `S`, as in DS chapter 5. Format 8 loads are written `LDRSB` and `LDRSH`, as in the ARMv4T ARM, rather than the data sheet's `LDSB`/`LDSH`. `ADD SP, #-0x8` is format 13 with the sign bit set. - A Thumb BL first half followed by a second half is one 4-byte `BL target`, which is how assemblers write it. Either half alone prints as `BL.HI #imm` or `BL.LO #imm`, and the comment says what that half does. The core runs the halves as two instructions (U18), so a step record shows a half. ### Encodings ARMv4T does not define These print as what the ARM7TDMI does with them, and the comment names the docs item. | Encoding | Prints as | Comment | | --- | --- | --- | | ARM undefined space (`cond 011x … xxx1 xxxx`) | `UND{cond}` | trap to 0x04 | | ARM condition `1111` | the instruction with `NV` (`LDRNVB`) | never executes (U19) | | ARMv5 LDRD/STRD (halfword space, L = 0, SH = 1x) | `LDR{cond}D`, `STR{cond}D` | no transfer on the ARM7TDMI (U21) | | CDP/LDC/STC, and MRC/MCR to a coprocessor other than 14 | the instruction | no coprocessor answers on the GBA: undefined trap | | MRC/MCR p14 | the instruction | CP14 (U17) | | Thumb `0xDExx` | `UND #imm` | trap, provisional; the corpus and NBA run closed box branch (C7) | | Thumb `0xE8xx` | `UND #imm` | trap, provisional; ARMv5 BLX suffix (C8) | | Thumb `0xBExx` | `UND #imm` | trap; ARMv5 BKPT | | Thumb `BX` with H1 = 1 | `BX Rs` | a plain BX on the ARM7TDMI (U19) | | Thumb hi-register ADD/CMP/MOV with H1 = H2 = 0 | the instruction | runs on the low registers (U19) | When an encoding's should-be-zero or should-be-one bits differ from the documented pattern, but the core's table still sends it to a handler, the comment says "nonstandard encoding". This covers MRS, MSR, BX, MUL/MLA, SWP, register-offset halfword transfers, MOV/MVN with an Rn field, compares with an Rd field, and Thumb BX with bits 2:0 set. The core decodes these as its tables do, which the SingleStepTests corpus (run as data) passes with; other disassemblers reject them or read them as ARMv5 instructions. ## Checks `tests/test_disasm.py` runs in the quick loop: - **Fixed text** for about 100 encodings across every class. - **Every Thumb halfword** disassembles, and the table's undefined ones print `UND`. - **Every ARM decode index** (4,096) disassembles under every condition, and its mnemonic belongs to the family of the handler the core runs. - **The PC-relative comments** equal the address the core reads when it runs the instruction. `scripts/disasm_judge.py` compares the text with three other disassemblers and assembles it back with GNU as. The disassemblers are not judges: no hardware decides how an instruction is written down, and the decode's own judges are the data sheet and the core's tables. They are **detectors** for text a second reader would not write. The detectors are: - **mGBA** (MPL-2.0, closed box): the pinned tree's decoder built behind an MPL-2.0 driver kept and built outside this repository since 2026-11-25 (`$EMU_TOOLS_GPL/tools/mgba/build/arm-disasm/mgba_disasm`; `python scripts/build_referees.py` has it built); its output is compared, its source is not read. - **MAME**: `unidasm -arch arm7` / `arm7thumb` from MAME 0.285, the version of the pinned tree. The binary comes from Debian's `mame-tools`. - **GNU objdump**: `arm-none-eabi-objdump -m armv4t -M reg-names-std` from the Arm GNU Toolchain 14.3.rel1 that gba-python pins (`../gba-python/reference/tools/…/bin`, read in place; `--binutils DIR` points elsewhere, and both binutils checks are skipped when it is absent). Each Thumb halfword is followed by a zero halfword, so one objdump reads as the first half of a 32-bit Thumb-2 instruction does not swallow the next. Coverage: - All 65,536 Thumb halfwords, against all three tools. - 32,768 combined BL pairs, against mGBA and objdump (MAME does not combine pairs). - A seeded sample of 131,072 ARM words, against all three: 32 per decode index, 24 of them with condition AL. The **round trip** is the one check here with a right answer: for every standard encoding in the same sets, our text goes through `arm-none-eabi-as` (`.syntax divided`, `.arch armv4t`) and must give back the same bits. Each side is reduced to a canonical form: lower case, R13–R15 as names, numbers as integers, and register lists as sets. Then: - **Syntax rules** rewrite notation that differs without meaning anything different. - **Disagreement rules** name the remaining differences in meaning. Anything left is *unexplained*, and the script exits 1. ### Results (2026-11-05) The run took about 30 seconds and left **no unexplained differences**. | Tool, state | Encodings | Equal as printed | Equal after syntax rules | Different meaning | | --- | ---: | ---: | ---: | ---: | | mGBA, Thumb | 65,536 | 52,384 | 12,768 | 384 | | mGBA, Thumb BL pairs | 32,768 | 32,768 | 0 | 0 | | mGBA, ARM | 131,072 | 85,745 | 16,224 | 29,103 | | MAME, Thumb | 65,536 | 50,824 | 9,400 | 5,312 | | MAME, ARM | 131,072 | 107,313 | 12,938 | 10,821 | | objdump, Thumb | 65,536 | 33,557 | 23,973 | 8,006 | | objdump, Thumb BL pairs | 32,768 | 32,768 | 0 | 0 | | objdump, ARM | 131,072 | 80,138 | 33,421 | 17,513 | MAME's ARM counts moved since 2026-11-04 (111,347 / 8,904) only because ours now writes LDC/STC write-back with DS 4.15's `!` (see below); MAME leaves it out, and a syntax rule reads the two as the same. **Syntax rules** (each name is a function in the script): - **Both tools** - `plain_immediate`: ours `#imm8, rot` against their value. - `zero_offset`: `[Rn, #0]` against `[Rn]`. This also covers ours `[Rn, #-0x0]`: we keep the U bit visible. - `psr_fields`: field letters in a different order. - `compare_p`: the tools ignore a compare's Rd. - `thumb_aliases`: - PUSH/POP written as STMDB SP!/LDMIA SP!; - `ADD SP, #-n` written as `SUB SP, #n`; - `LSR #32` and `ASR #32` written with the raw `#0`; - `ADD Rd, Rs, #0` written as `MOV Rd, Rs`. - **mGBA only** - `unsigned_immediate`: mGBA prints a 32-bit immediate signed. - `condition_dropped`: mGBA prints condition NV as unconditional. - `resolve_pc_relative`: mGBA prints the load address in brackets. - `mgba_bl_half`: mGBA prints a lone BL half as a bare `bl`. - `undefined_ill`: mGBA calls UND `ill`. - **MAME and objdump** - `ual_coprocessor_post`: ours `[Rn], #off!` for a post-indexed LDC/STC with write-back, theirs `[Rn], #off`. MAME prints W = 0 the same way, so its text cannot tell the two apart; objdump prints W = 0 as ARMv5's option field (a difference in meaning, below). - **MAME only** - `mame_pc_relative`: MAME prints the load address bare, and `ADD Rd, PC` as `ADR`. - `mame_bl_half`: MAME's `BL (HI)` shows the offset shifted into place. - `mame_two_operand`: MAME drops a source register equal to the destination. - `mame_condition_last`: MAME puts the condition after the size (`LDRSBMI`), as UAL does. - `mame_stack_names`: MAME writes `LDMFD` etc. with SP as the base. - `mame_shift_mnemonic`: MAME writes `LSL Rd, Rm, #n` for a shifted MOV. - `mame_cdp_opc2`: MAME leaves out CDP's opcode 2 when it is 0. - `mame_empty_mask`: MAME writes `CPSR_` for MSR with no fields. - **objdump only** (it writes UAL, ARM's later unified syntax) - `ual_mnemonic`: the condition last (`ADDEQS` is `addseq`, `LDREQB` `ldrbeq`, `STCEQL` `stcleq`), `SWI` as `svc`, and LDM/STM without `IA` (objdump keeps it when there is a condition). - `ual_aliases`: PUSH/POP for STMDB/LDMIA with `SP!`, `LSL Rd, Rm, #n` (and `RRX`) for a shifted MOV, `nop` for `MOV R0, R0` and Thumb `MOV R8, R8`, and `movs Rd, Rs` for Thumb `LSL Rd, Rs, #0`. - `objdump_thumb_flags`: UAL marks a flag-setting Thumb instruction with `S` (`adds`, `lsls`, `muls Rd, Rs, Rd`) and a 16-bit branch with `.n`. - `objdump_thumb_ldm`: UAL writes a Thumb LDMIA whose base is in the list without `!`; format 15 is one encoding either way. - `objdump_coprocessor_opc2`: objdump always prints MRC/MCR's opcode 2. - `objdump_mrc_flags`: MRC to R15 sets the flags; UAL writes that Rd `APSR_nzcv`. - `objdump_rotation`: objdump keeps the `#imm8, rot` form for some instructions; its side is brought to the value as ours is. - `objdump_undefined`: its `` and `udf` are ours `UND`. - Also used from the lists above: `plain_immediate`, `unsigned_immediate`, `mame_empty_mask`, `zero_offset`, `psr_fields`, `thumb_aliases`. The script brings objdump's own notation to the others' before comparing: it drops the `@` comment, writes `70 <.data+0x70>` targets as `0x70`, and writes coprocessor operands `15`/`cr0`/`{0}` as `p15`/`c0`/`0` (except LDC/STC's `{n}`, which is ARMv5's option field). `zero_offset` also takes objdump's `#-0` and the tools' `[Rn]` for a post-indexed zero. MAME also runs a long multiply's mnemonic into its first register (`UMLALGESR6`) and leaves a stray `]` after a halfword transfer's resolved address. The script repairs both before comparing. **Differences in meaning:** | Class | mGBA | MAME | Who is right | | --- | ---: | ---: | --- | | Should-be bits differ: the tool rejects (`ill`, `INVALID`) or reads as ARMv5TE (`QADD`, `SMLALxy`, `BLX`) what the core's table decodes | 3,568 | 1,739 | The core decodes as its tables do, and the SingleStepTests corpus agrees. No GBA software uses these. Ours shows the core's meaning and flags the encoding. | | No coprocessor disassembly in mGBA (`ill`) | 24,576 | | mGBA does not disassemble coprocessor instructions at all. | | ARMv5 LDRD/STRD (U21) | 544 | | mGBA prints `ill`. Ours names the encoding and says it moves nothing. | | Post-indexed transfer with PC as the base, printed as a literal address | 527 | 492 | Ours. Post-indexed means the address is the base itself (DS 4.9), but both tools print base + offset as if it were pre-indexed. | | Thumb BX with H1 = 1 | 16 | 16 | Ours. alyosha-tas `thumb/blx` (hardware) shows a plain BX. mGBA's disassembler says `ill`, and MAME says `BLX` (ARMv5) or `INVALID`. | | Thumb `0xBExx` | 256 | | mGBA prints ARMv5's BKPT; the core traps. | | Thumb undefined space | | 2,816 | MAME prints `INVALID`, as ours prints `UND`. They count as different only because the words differ. | | Thumb `0xE8xx` (C8) | | 2,048 | MAME decodes ARMv5's BLX suffix. The core traps provisionally, and the hardware test ROM decides (decision 4). | | Thumb `0xDExx` and `0xBExx` (C7) | | 256 | MAME prints BKPT for both. The core traps provisionally. | | Thumb hi-register ADD with H1 = H2 = 0 | | 64 | MAME rejects it. NBA and the corpus run it on the low registers (U19); alyosha-tas `thumb/cpy` (hardware) shows the same for MOV. | | ARM undefined space (bit 4 set in a register-offset transfer) | | 8,075 | MAME prints a transfer shifted by a register, which ARMv4T does not have. mGBA agrees with ours that these are undefined. | | ARM condition NV | | 588 | MAME decodes ARMv5's use of the space (BLX, PLD, LDC2 …). On the ARM7TDMI it never executes. | | MSR immediate with mask bit 17 or 18 set | | 39 | MAME swaps the names of bits 17 and 18 in the immediate form only. Bit 17 is `x` and bit 18 is `s` (ARMv4T ARM). mGBA and MAME's own register form agree with ours. | **Differences in meaning, objdump.** `-m armv4t` does not stop objdump from decoding later architectures, so most of its differences are ARMv5 to ARMv8-M readings of space that is undefined, reserved or should-be-zero on the ARM7TDMI. | Class | Thumb | ARM | Who is right | | --- | ---: | ---: | --- | | Should-be bits differ: rejected (``) or read as a later instruction | 128 | 7,363 | As for mGBA and MAME above. | | Thumb `0xE800`–`0xFFFF` on its own: the first half of a 32-bit Thumb-2 instruction (`and.w`, `vhadd.s8` …). This covers lone BL halves and `0xE8xx` (C8). | 5,792 | | The ARM7TDMI has no 32-bit Thumb instructions; BL's halves run as two (U18). Combined pairs agree 32,768/32,768. | | Thumb undefined space: CBZ, SXTH, REV, BKPT, HLT, UDF (ARMv5–v8-M) | 2,052 | | The core traps (C7, C8). | | Empty register list: objdump marks it UNPREDICTABLE (`stmia`) | 18 | | The core runs it (U2). | | Thumb BX with H1 = 1: ARMv5's BLX | 16 | | Ours; alyosha-tas `thumb/blx` (hardware) shows a plain BX. | | LDC/STC post-indexed with W = 0: ARMv5's unindexed form `[Rn], {option}` | | 4,034 | Ours by DS 4.15 for ARMv4T: P = 0 is post-indexed, W = 0 keeps the base. ARMv5 reused W = 0 for the option form. | | Coprocessors 10 and 11 (and a few others): VFP/MVE syntax (`vldr`, `vldmia`), or rejected where VFP leaves a gap | | 3,889 | ARMv4T has no VFP; on the GBA no coprocessor answers. | | Condition NV: ARMv5's BLX/PLD/xxx2, or rejected | | 1,654 | Never executes on the ARM7TDMI (U19). | | ARMv5 LDRD/STRD (U21): ARMv5TE's LDRD/STRD | | 541 | No transfer on the ARM7TDMI. | | Pre-indexed write-back with PC as the base: objdump drops the `!` | | 17 | Ours: W = 1 is in the word. | | Pre-indexed LDC/STC write-back with offset 0: objdump drops the `!` | | 15 | Ours: W = 1 is in the word. | ### Round trip through GNU as (2026-11-05) Our text, given to `arm-none-eabi-as`, must give back the original bits. The text needs only three changes first, all mechanical: - a branch's absolute target becomes `. + offset`; - a post-indexed LDC/STC: ours `[Rn], #off!` (W = 1) is GNU's `[Rn], #off`; ours `[Rn], #off` (W = 0, up) is GNU's option field `[Rn], {off/4}`, which is the same bits; - Thumb `ADD SP, #-n` is GNU's `SUB SP, #n` (GNU reads `#-0` as +0). A line GNU as rejects is replaced by its raw bytes and the file is assembled again, so every instruction keeps its address. | Set | Encodings | Not standard, left out | Exact | Named class | Rejected, known | Failures | | --- | ---: | ---: | ---: | ---: | ---: | ---: | | Thumb, every halfword | 65,536 | 9,648 | 55,742 | 128 | 18 | 0 | | Thumb BL pairs | 32,768 | 0 | 32,768 | 0 | 0 | 0 | | ARM sample | 131,072 | 22,825 | 101,983 | 2,155 | 4,109 | 0 | **Left out** as not standard ARMv4T, so no assembler is expected to produce them: undefined (Thumb 5,120, ARM 8,192), nonstandard should-be bits (224, 12,167), lone BL halves (4,096), the reserved Thumb forms of U19 (208), condition NV (1,654), ARMv5 LDRD/STRD (541), and halfword transfers post-indexed with W set, which DS 4.10 says not to use (271). **Named classes** (standard encodings that do not come back exact): - *Two spellings* (128, Thumb): `ADD/SUB Rd, Rd, #imm3` is format 2, and GNU as picks format 3 (`#imm8`) for the same text. Both set the same flags and result; ours keeps the three-operand form, which is how the data sheet writes format 2. - *No GNU spelling* (2,036, ARM): post-indexed LDC/STC down without write-back (2,011; UAL has no such form and its option field is up only); MSR immediate with an explicit rotation (22); MSR with an empty field mask (3), where GNU reads a bare `CPSR` as `CPSR_fc`. - *Coprocessor 9* (119, ARM): GNU as reads LDC/STC on p9 as ARMv8.2's half-precision `VLDR`/`VSTR`, so a pre-indexed offset assembles to half the bits. **Rejected, known** (GNU as refuses the text): - R15 where the data sheet says it must not be used (3,973): multiply operands, PC as an index register, PC as a written-back base. These are UNPREDICTABLE by the data sheet, and the core decodes them as the corpus shows (OBSERVED). - Coprocessor 9 offsets of 0x200 or more (133), the same p9 reading. - `ADD Rd, PC, #imm` with imm ≥ 0x80000000 (3): GNU turns it into a PC-relative fixup and rejects the result. - An empty register list (18, Thumb), which GNU as cannot write (U2). None of these is a bug in our disassembler. Getting here found six, all fixed and covered by tests: - the MRS check ignored bit 25; - the SWP check ignored bits 23, 21 and 20; - the MUL/MLA check ignored bit 22; - LDC/STC with P = 0 printed ARMv5's unindexed form, where DS 4.15 defines P = 0 as post-indexed. The binutils round trip found the other two, both cases of two encodings printing the same text: - a post-indexed LDC/STC printed W = 0 and W = 1 alike; W = 1 now has DS 4.15's `!`; - a post-indexed zero offset printed `[Rn]`, the same as the pre-indexed encoding; it now prints `[Rn], #0x0`.