# Start here: the ARM7TDMI for a pure-Python core Everything a builder needs to know about the CPU before reading the cycle tables. Sources are cited by short name: | Name | Document | Tier | | --- | --- | --- | | TRM | ARM7TDMI (Rev 3) Technical Reference Manual, ARM DDI 0029G, 2001 (`reference/docs/arm7tdmi-trm-DDI0029G.pdf`) | manufacturer documentation | | DS | ARM7TDMI Data Sheet, ARM DDI 0029E, 1995 (`reference/docs/arm7tdmi-datasheet-DDI0029E.pdf`) | manufacturer documentation | | GBATEK | Martin Korth's GBA/NDS reference, ARM CPU chapters (`reference/docs/gbatek.htm`) | community documentation, mostly hardware-tested | | NBA | NanoBoyAdvance's ARM core (GPL-3.0), built from `reference/src/NanoBoyAdvance/` as a closed-box referee; its source is not read | emulator (OBSERVED) | | SST | SingleStepTests ARM7TDMI (`reference/src/SingleStepTests-ARM7TDMI/`) | emulator-derived (from an NBA fork) | The TRM is the newest ARM document for this core but dropped the instruction set chapters; the DS (an earlier issue of the same document number) has them (DS chapters 4 and 5). ARM's architecture manual (ARM DDI 0100, "ARM ARM") is the formal ARMv4T definition; no ARM-hosted copy was found, so it is cited, not pinned. Where the DS and the TRM differ, the TRM is newer. ## What the ARM7TDMI is A 32-bit RISC core, architecture **ARMv4T**: the ARM instruction set (32-bit instructions, every one conditional) plus **Thumb** (16-bit instructions that expand to ARM operations), with the long multiply ("M") and the debug logic ("D", "I": EmbeddedICE). It has a three-stage pipeline (fetch, decode, execute), a von Neumann bus (code and data share one bus, one access per cycle) and no cache, MMU or write buffer. TRM 1.1–1.3. In the GBA it runs at 16.78 MHz (2^24 Hz) inside the AGB CPU chip. Everything the GBA adds (memory map, waitstates, the cartridge prefetch buffer, DMA, interrupts' sources, HALT) is the board's. ## Registers and modes 37 registers, 16 visible at a time plus CPSR (TRM 2.6, DS 3.7): | Mode | CPSR[4:0] | Banked registers | SPSR | | --- | --- | --- | --- | | User | `10000` | none | none | | FIQ | `10001` | R8–R14 | SPSR_fiq | | IRQ | `10010` | R13, R14 | SPSR_irq | | Supervisor | `10011` | R13, R14 | SPSR_svc | | Abort | `10111` | R13, R14 | SPSR_abt | | Undefined | `11011` | R13, R14 | SPSR_und | | System | `11111` | none (uses User's) | none | - R13 is the stack pointer by convention, R14 the link register (written by BL and exception entry), R15 the PC. - Any other mode value is invalid; what the core does with one is U15. - The Thumb state sees R0–R7 directly, and R8–R15 only through a few instructions (hi-register ADD/CMP/MOV, BX) plus implicit SP/LR/PC uses. **CPSR** (and each SPSR): N Z C V at bits 31–28, I (IRQ disable) bit 7, F (FIQ disable) bit 6, T (Thumb state) bit 5, mode bits 4–0. Bits 27–8 are reserved (ARMv4T has no Q bit at 27). MSR writes them in the CPSR and the SPSRs alike in the corpus (7,667 corpus cases leave reserved CPSR bits set); whether the silicon keeps them is U16. Software must not change T with MSR (DS 3.8 "the software must never change the state of the T bit"); what happens if it does is U16, still open: the core decodes bits 15:0 of the two ARM words the pipeline still holds, then fetches halfwords from the MSR's address + 12 (HYPOTHESIS, no hardware test). User mode cannot change I, F, T or the mode (MSR writes only the flags there). ## The two instruction sets ARM (DS 4.1 table 4-1): branch (B, BL, BX), data processing (16 opcodes, with a barrel-shifted second operand), PSR transfer (MRS, MSR), multiply (MUL, MLA, UMULL, UMLAL, SMULL, SMLAL), single transfer (LDR, STR, byte and word, with T forms), halfword and signed transfer (LDRH, STRH, LDRSB, LDRSH), block transfer (LDM, STM), swap (SWP, SWPB), SWI, coprocessor (CDP, LDC, STC, MCR, MRC) and the undefined space. Every ARM instruction has a 4-bit condition in bits 31–28; condition `1111` is reserved on ARMv4 (U19). Thumb (DS chapter 5): 19 formats. Only the conditional branch (format 16) is conditional. BL is two instructions (format 19, U18). There are no Thumb coprocessor, swap, PSR, multiply-long or LDM-with-PC-into-PC-via-LDM forms; POP {PC} loads the PC. The decode used by every referee: ARM by the 12 bits `bits[27:20]:bits[7:4]` (4,096 entries), Thumb by `bits[15:6]` (1,024 entries). The tables are built from the DS's encoding diagrams; the corpus's 45 file names are the classes it covers (validation.md). ## The pipeline and what R15 reads as Fetch, decode, execute: while instruction *n* executes, *n+1* is decoded and *n+2* is fetched. Consequences (DS 3.4, 4.x "Using R15 as an operand"): - Reading R15 as an operand gives the address of the instruction **plus 8** in ARM state, **plus 4** in Thumb. - With a register-specified shift (the instruction has an extra cycle), R15 as an operand reads **plus 12** (U9). - STR and STM storing R15 store **plus 12** (U8). - Thumb PC-relative loads and ADD Rd, PC use `(PC + 4) AND NOT 2`. - Writing R15 (branches, data processing with Rd = 15, loads into PC, BX) flushes the pipeline: two refill fetches (N at the target, S at target+L) before the next instruction executes. Exceptions are ignored during the refill (TRM 6.5). The pipeline is visible to software only through self-modifying code (the two prefetched opcodes are not refetched) and through the bus. SST stores the two prefetched opcodes as `pipeline` and the type of the next fetch as `access`; the core keeps both as state. ## Bus cycles Four cycle types, from the nMREQ and SEQ pins (TRM 3.3, table 3-1): | Type | nMREQ | SEQ | Meaning | | --- | --- | --- | --- | | N | 0 | 0 | nonsequential: address unrelated to the previous cycle's | | S | 0 | 1 | sequential: same address as the previous I cycle, or +4 (word) / +2 (halfword) from the previous access | | I | 1 | 0 | internal: no transfer; the memory system must not commit | | C | 1 | 1 | coprocessor register transfer; never happens on the GBA | Address-class signals that matter here: A[31:0], MAS[1:0] (width: byte, halfword, word), nRW (write), nOPC (opcode fetch), nTRANS (user-mode access: low in User mode and for LDRT/STRT), LOCK (SWP), TBIT. **Merged IS** (TRM 3.3.4): during an I cycle the core already drives the address of the next access, which is then an S cycle to that same address. In the TRM's tables this is every I cycle that is followed by a fetch at `pc+3L`. The GBA does not exploit it: see C1 in undocumented-behavior.md and the contract in handoff-brief.md. **Bursts** (TRM 3.3.2): only word reads (code, LDM), word writes (STM) and halfword reads (Thumb code). Bytes are never sequential. **The TRM's tables print nMREQ and SEQ one row early**, because the pins are pipelined: the values in row *k* give the type of cycle *k+1* (TRM 6.1). timing.md has every table converted so that each row carries its own type. ## Exceptions | Vector | Exception | Mode entered | I | F | R14 holds (ARM / Thumb) | | --- | --- | --- | --- | --- | --- | | `0x00` | Reset | Supervisor | 1 | 1 | unpredictable | | `0x04` | Undefined instruction | Undefined | 1 | – | PC+4 / PC+2 | | `0x08` | SWI | Supervisor | 1 | – | PC+4 / PC+2 | | `0x0C` | Prefetch abort | Abort | 1 | – | PC+4 / PC+4 | | `0x10` | Data abort | Abort | 1 | – | PC+8 / PC+8 | | `0x14` | (reserved) | – | – | – | – | | `0x18` | IRQ | IRQ | 1 | – | PC+4 / PC+4 | | `0x1C` | FIQ | FIQ | 1 | 1 | PC+4 / PC+4 | TRM 2.8, tables 2-3 and 2-4. PC is the address of the BL/SWI/undefined instruction, or of the instruction that was not executed because the interrupt was taken. Entry: R14_mode ← return address, SPSR_mode ← CPSR, CPSR mode ← new, T ← 0, I ← 1 (and F ← 1 for reset and FIQ), PC ← vector, refill. Priority (TRM 2.8.10): Reset > Data abort > FIQ > IRQ > Prefetch abort > Undefined/SWI. The GBA never asserts ABORT (no MMU, no bus errors), so the aborts are never taken there; the core implements them only if a later board needs them. nFIQ is tied high on the GBA (GBATEK "Fast Interrupt (FIQ)": "there is no way to generate a FIQ by hardware"), so FIQ mode is reachable only by MSR. ## What the core does not own - **Waitstates and clocks.** Every callback is one CPU cycle; the board adds the memory's waitstates (GBA WAITCNT, 8/16/32-bit bus widths) and decides how S after I is timed. - **The GBA's cartridge prefetch buffer**, which is a board device that watches opcode fetches. - **The memory map**, open bus (the value an unmapped read returns is the last prefetched opcode on the GBA: a board fact, GBATEK "GBA Unpredictable Things"), BIOS read protection. - **The BIOS.** SWIs vector to `0x08` like any ARM7TDMI; whatever is there is the board's. - **HALT/STOP** (the GBA stops the CPU's clock), **DMA** (the GBA's DMA takes the bus between CPU cycles), **the interrupt controller** (IE/IF/IME). - **The IRQ input synchronizer delay**: the core samples a level the board presents (handoff-brief.md). ## Where to go next [timing.md](timing.md) for every cycle, then [undocumented-behavior.md](undocumented-behavior.md).