# Timing: every instruction class, cycle by cycle Source: TRM chapter 6 (ARM DDI 0029G, tables 6-1 to 6-22; the DS's chapter 10 has the same tables), manufacturer documentation. Every table below has been **converted**: the TRM prints nMREQ and SEQ one row early (they are pipelined and describe the *next* cycle, TRM 6.1), so a TRM row's type is read from the row above it. Here each row carries its own type. Measured agreement is in the last section: NanoBoyAdvance's I-cycle counts over the 2.25 M corpus cases agree with these tables except C5 (the undefined trap). ## How to read the tables - `pc` is the address of the instruction; `L` is 4 in ARM state, 2 in Thumb; `w` is the fetch width (32 in ARM, 16 in Thumb). - Cycle 1 of every instruction is the prefetch of `pc+2L` (the instruction two ahead), which overlaps the instruction's first execute cycle. Its type, **N or S, is decided by the previous instruction** (column "next" of the previous table): this is the corpus's and the core's `next_fetch_seq`. - `alu` is the computed branch target or data address; `pc'` is a value loaded into R15; `Xn` is an exception vector. - Type `I` rows show the address the core drives during the internal cycle; it is passed to `idle(address)`. - "next" is the type of the next instruction's cycle 1. **S after an I cycle is a merged IS cycle** (TRM 3.3.4): the I cycle already drove that address. The GBA times it as N (C1); the core reports S. - A `step()` is cycles 1..k of one table. The number of callbacks equals the number of rows. ## Branch and branch with link (B, BL) — TRM table 6-1 | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | | 1 | prev | pc+2L | w | prefetch; target computed | | 2 | N | alu | w | refill; R14 written for BL | | 3 | S | alu+L | w | refill | next: S (at alu+2L). Total 2S+N. ARM only for BL; Thumb B (format 18) and a taken conditional branch (format 16) have the same three cycles. ## Thumb BL, both halves — TRM table 6-2 | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | | 1 | prev | pc+4 | 16 | first half: LR ← PC + (offset_hi << 12); a data operation | | 2 | S | pc+6 | 16 | second half begins | | 3 | N | alu | 16 | | | 4 | S | alu+2 | 16 | | next: S. The halves are separate `step()`s: the first is one cycle (next S), the second three (2S+N in all, with cycle 2 above as its cycle 1). Total 3S+N. ## Branch and exchange (BX) — TRM table 6-3 | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | | 1 | prev | pc+2W | old width | TBIT still old | | 2 | N | alu | new width | TBIT new | | 3 | S | alu+w | new width | | next: S. `W`/`w` are the old/new instruction lengths. The target's low bit selects the state and is cleared; for an ARM target the fetch address has its bit 1 cleared too (C3). ## Data processing, PSR transfer — TRM table 6-4 | Kind | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | --- | | normal | 1 | prev | pc+2L | w | | | | | | | | next S at pc+3L | | Rd = PC | 1 | prev | pc+2L | w | | | | 2 | N | alu | w | | | | 3 | S | alu+L | w | next S | | shift by Rs | 1 | prev | pc+2L | w | | | | 2 | I | pc+3L | – | the shift; R15 operand reads pc+12 (U9) | | | | | | | next S at pc+3L (merged IS) | | shift by Rs, Rd = PC | 1 | prev | pc+8 | 32 | | | | 2 | I | pc+12 | – | | | | 3 | N | alu | 32 | | | | 4 | S | alu+4 | 32 | next S | MRS and MSR are "normal" (they never use the PC; TRM 6.5). Thumb's hi-register MOV/ADD to PC is "Rd = PC" in Thumb widths. A test instruction (TST, TEQ, CMP, CMN) never writes R15. ## Multiply — TRM tables 6-5 to 6-8 | Instruction | Cycles | I cycles | | --- | --- | --- | | MUL | prev fetch pc+2L, then m × I at pc+3L | m | | MLA | prev fetch pc+8, then (m+1) × I (the first at pc+8, the rest at pc+12) | m+1 | | UMULL, SMULL | prev fetch pc+8, then (m+1) × I at pc+12 | m+1 | | UMLAL, SMLAL | prev fetch pc+8, then (m+2) × I at pc+12 | m+2 | next: S (merged IS). `m` is set by the multiplier operand Rs (TRM 6.20): | m | Condition on Rs | | --- | --- | | 1 | bits [31:8] all zero or all one | | 2 | bits [31:16] all zero or all one | | 3 | bits [31:24] all zero or all one | | 4 | otherwise | For UMULL/UMLAL only "all zero" terminates early (the TRM's wording is for signed operands; the DS's 4.8.3 gives the unsigned rule; zayd's model agrees, U11). Thumb MUL (format 4) is MUL with Thumb widths. ## Load register (LDR, LDRB, LDRH, LDRSB, LDRSH) — TRM table 6-9 | Kind | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | --- | | normal | 1 | prev | pc+2L | w | address computed | | | 2 | N | alu | 8/16/32 | data; base written back | | | 3 | I | pc+3L | – | data to Rd | | | | | | | next S at pc+3L (merged IS) | | Rd = PC | 1 | prev | pc+8 | 32 | | | | 2 | N | alu | 32 | pc' | | | 3 | I | pc+12 | – | | | | 4 | N | pc' | 32 | | | | 5 | S | pc'+4 | 32 | next S | Total S+N+I (+S+N when PC is loaded). nTRANS is low for LDRT/LDRBT and in User mode. The address is the computed one, unaligned bits included (U1–U3). Thumb PC-relative and SP-relative loads are "normal". ## Store register (STR, STRB, STRH) — TRM table 6-11 | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | | 1 | prev | pc+2L | w | address computed | | 2 | N | alu | 8/16/32 | write Rd; base written back | next: **N** at pc+3L (an STR is two N cycles back to back, TRM 3.3.1). ## Load multiple (LDM, POP) — TRM table 6-12 | Kind | # | Type | Address | Width | | --- | --- | --- | --- | --- | | n registers, no PC | 1 | prev | pc+2L | w | | | 2 | N | alu | 32 | | | 3 … n+1 | S | alu+4 … | 32 | | | n+2 | I | pc+3L | – | | | | | | next S (merged IS) | | n registers with PC | 1 … n+1 | as above | | | | | n+2 | I | pc+3L | – | | | n+3 | N | pc' | w' | | | n+4 | S | pc'+L' | w' | | | | | | next S | n = 1 is the same table with no S cycles. Total nS+N+I (+S+N with PC). Addresses always ascend: a decrementing LDM computes its lowest address first (DS 4.11). With PC loaded, `w'` and `L'` follow the new state if the S bit restored a Thumb CPSR; Thumb POP {PC} stays in Thumb (ARMv4T: bit 0 of the loaded value is ignored, not an interworking branch). The empty list is U4. ## Store multiple (STM, PUSH) — TRM table 6-13 | # | Type | Address | Width | | --- | --- | --- | --- | | 1 | prev | pc+2L | w | | 2 | N | alu | 32 | | 3 … n+1 | S | alu+4 … | 32 | next: **N**. Total (n-1)S+2N. ## Swap (SWP, SWPB) — TRM table 6-14 | # | Type | Address | Width | LOCK | Note | | --- | --- | --- | --- | --- | --- | | 1 | prev | pc+8 | 32 | 0 | | | 2 | N | Rn | 8/32 | 1 | read | | 3 | N | Rn | 8/32 | 1 | write Rm | | 4 | I | pc+12 | – | 0 | read data to Rd | next: S (merged IS). Total S+2N+I. The TRM sets LOCK on both the read and the write; the corpus and NBA mark only the write (C10). ## SWI and exception entry — TRM table 6-15 | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | | 1 | prev | pc+2L | w | mode change, R14 and SPSR written | | 2 | N | Xn | 32 | ARM state from here (TBIT 0), nTRANS high | | 3 | S | Xn+4 | 32 | | next: S. Total 2S+N. An IRQ or FIQ entry is the same three cycles, with `pc` the address of the instruction that was not executed: the core makes the cycle-1 fetch (whose data it discards) and refills (TRM table 2-3, the link value). ## Undefined instruction and absent coprocessor — TRM table 6-21 | # | Type | Address | Width | Note | | --- | --- | --- | --- | --- | | 1 | prev | pc+2L | w | coprocessor handshake (nCPI low) | | 2 | I | pc+2L | – | CPA, CPB high: absent | | 3 | N | Xn (0x04) | 32 | | | 4 | S | Xn+4 | 32 | | next: S. Total 2S+N+I. NBA and the corpus have no I cycle here (C5). On the GBA this covers CDP, LDC, STC, and MCR/MRC to any coprocessor except CP14. ## CP14 register transfer (MRC, MCR) — TRM tables 6-19, 6-20 (ready, b = 0) | Instruction | # | Type | Address | Note | | --- | --- | --- | --- | --- | | MRC | 1 | prev | pc+8 | | | | 2 | C | pc+12 | data from the bus (U17) | | | 3 | I | pc+12 | data to Rd | | | | | | next S (merged IS) | | MCR | 1 | prev | pc+8 | | | | 2 | C | pc+12 | Rd driven on the bus | | | | | | next N | A C cycle is reported through `idle(address)` with `cpu.coprocessor_cycle` true for the duration of the call (handoff-brief.md). This table is the TRM's for a coprocessor that answers; that the GBA's CP14 answers this way is what alyosha-tas's `Coprocessor_14` timing check implies, not yet measured here (validation.md, rung 5). ## Unexecuted instruction — TRM table 6-22 | # | Type | Address | Width | | --- | --- | --- | --- | | 1 | prev | pc+2L | w | next: S. Any ARM instruction whose condition fails, and a Thumb conditional branch not taken. ## Reset — TRM 2.10, 3.10 While nRESET is low the core makes I cycles with an incrementing address; after it goes high: N at 0, S at 4, then execution in Supervisor mode, ARM state, I and F set. The core's `reset()` models only the two refill fetches. ## Measured: the referee's I cycles against these tables `scripts/referee_sst.py` runs NanoBoyAdvance's core as a closed box (the referee's bus records each I cycle) over all 2.25 M corpus cases. Histograms of I cycles per instruction, by file (executed and condition-failed cases together): | Corpus file | I cycles: cases | Table above | | --- | --- | --- | | `arm_data_proc_register_shift` | 0: 24,807, 1: 25,193 | 1 if executed | | `arm_mul_mla` | 0: 24,774, 3: 86, 4: 12,582, 5: 12,558 | MUL m, MLA m+1 | | `arm_mull_mlal` | 0: 24,936, 4: 82, 5: 12,612, 6: 12,370 | m+1, m+2 | | `arm_ldr_str_*`, `arm_ldrh_strh` | 0: ~37,600, 1: ~12,400 | loads 1, stores 0 | | `arm_ldrsb_ldrsh` | 0: 24,724, 1: 25,276 | loads only | | `arm_ldm_stm` | 0: 37,430, 1: 12,570 | LDM 1, STM 0 | | `arm_swp` | 0: 25,090, 1: 24,910 | 1 | | `thumb_ldr_pc_rel` | 1: 50,000 | 1 (always executed) | | `thumb_data_proc` | 0: 34,326, 1: 12,518, 3: 29, 4: 3,127 | shifts by register 1, MUL m | | `thumb_push_pop`, `thumb_ldm_stm` | 0/1 about half each | POP/LDMIA 1 | | `arm_cdp`, `arm_stc_ldc`, `arm_mcr_mrc` | 0: 50,000 | **1 when trapped (C5)** | | every branch, SWI, MSR/MRS, Thumb ALU | 0 | 0 | With random operands most multipliers need m = 4, which is why m = 1 or 2 never appear and m = 3 is rare. Everything matches the converted tables except the undefined trap.