# Timing: the ARM9E-S pipeline, the memory system, and the DS's bus Three layers decide how long an ARM9 instruction takes on a DS, and they come from three different sources: 1. **The core** (ARM9E-S Rev 1): cycles per instruction at zero wait states, with interlocks. DDI 0165B chapter 8, cycle by cycle, both buses. DOC. 2. **The macrocell's memory system** (ARM946E-S): TCM and cache hits are single-cycle; misses, uncached accesses and unbuffered writes go through the BIU to the AHB, which on the DS is clocked at half the core clock. DDI 0201D chapters 3, 5, 6. DOC. 3. **The DS's bus and memories**: the wait states of main RAM, WRAM, VRAM, I/O and the GBA slot, and a nonsequential penalty nobody has explained. GBATEK "DS Memory Timings", which Martin Korth measured on his DS (DOC, from measurement; no capture published). Layers 1 and 2 belong to this core; layer 3 to nds-python, which answers each AHB transaction with its cost. ## Clocks (GBATEK "DS Memory Timings") Bus clock 33.513982 MHz (1FF61FEh Hz; Korth measured nearer 1FF6231h against the RTC); the ARM7 runs at the bus clock; the ARM9 core at twice it, 67.027964 MHz. GBATEK counts everything in 33 MHz units, so ARM9 core cycles appear as "half cycles". This specification counts in **ARM9 core cycles** (67 MHz) inside the core and converts at the bus interface; the board's clock of record is the 33 MHz bus (nds-python domains.md). ## Layer 1: the core (DDI 0165B table 8-2, selected) | Instruction | Cycles | Note | | --- | ---: | --- | | data processing | 1 | +1 register-specified shift; PC destination 3 (4 with shift) | | LDR | 1 | +1 if the next instruction uses the word; +2 for a byte, halfword or unaligned word used next; loading PC 5 | | STR | 1 | | | LDRD / STRD | 2 | LDRD +1 if the last word is used next | | LDM n regs | n (n > 1) | 2 for one register; +1 if the last word is used next; with PC n + 4 | | STM n regs | n (n > 1) | 2 for one | | SWP | 2 | +1 if used next | | B, BL, BX, BLX, SWI, undefined | 3 | | | MUL, MLA | 2 | +1 if the result is used next (not as an accumulator); MULS/MLAS 4 | | SMULL etc. | 3 | +1 RdHi used next; flag-setting 5 | | SMULxy, SMLAxy, SMULWy, SMLAWy | 1 | +1 if used next | | SMLALxy | 2 | +1 | | QADD family | 1 | +1 if used next | | CLZ | 1 | | | MRS | 2 | | | MSR | 1 flags only, 3 otherwise | | | MCR / MRC | b+1 / b+1 (+1 if used next; to PC b+4) | b = coprocessor busy cycles | | coprocessor absent | b+4 | | "Used next" means "in its first Execute or Memory cycle" (the table's wording); the interlock is charged to the producing instruction (DDI 0165B 8.2). The tables also give, cycle by cycle, the instruction-bus and data-bus activity (N, S, I, C) and addresses; those rows are the core's **bus schedule**, transcribed into data (the JIT proposal's "timing as data"), the way arm7tdmi-python's docs/timing.md does for the ARM7. ## Layer 2: the memory system - **TCM**: single cycle, both sides; an instruction fetch and a data read of ITCM in the same cycle stall one cycle (DDI 0201D 5.5.3). - **Cache hit**: single cycle. **Miss** in a cachable region: an 8-word AHB burst (INCR8) from word 0 of the line, the core streaming (advancing) during it (6.2.4-6.2.5); crossing a line boundary inserts idle AHB cycles for the look-up (figure 6-2). - **Uncached**: single AHB transfers; sequential instruction fetches become nonsequential (6.2.6); Thumb fetches are 32-bit reads whose second halfword is latched (6.3). - **Write buffer**: a bufferable store costs the core one cycle while the 16-entry FIFO has room; NCNB stores and all uncached reads wait for the AHB (6.5). - **AHB synchronisation**: an AHB transfer starts on the next HCLKEN, up to one core cycle late on the DS (ratio 2), and the core waits for HREADY (6.4). ## Layer 3: what GBATEK measured on a DS (NDS9 columns, 33 MHz units) | Region | Code N32 / S32 | Data N32 / S32 / N16 / S16 | Bus | | --- | --- | --- | --- | | Main RAM (cache off) | 9 / 9 | 10 / 2 / 9 / 1 | 16-bit | | WRAM, BIOS, I/O, OAM | 4 / 4 | 4 / 1 / 4 / 1 | 32-bit | | VRAM, palette | 5 / 5 | 5 / 2 / 4 / 1 | 16-bit | | GBA ROM (10,6 setting) | 19 / 19 | 19 / 12 / 13 / 6 | 16-bit | | TCM, cache hit | 0.5 | 0.5 | | | cache miss, BIOS / main RAM | | 11 / 23 (a line) | | And Korth's notes, each a measured statement without a capture: - every ARM9 opcode fetch outside TCM and cache is "N32 + 3 waits", Thumb included (two opcodes per fetch), agreeing with DDI 0201D 6.2.6 and 6.3 on the shape; - "an infamous PENALTY of 3 cycles is added to all nonsequential accesses (except cache, tcm, and main ram)" on the ARM9, and main RAM "is ALWAYS having the nonsequential 3 wait PENALTY (even on ARM7)"; the cause is unknown (cp15-tcm-pu-caches.md U3); - data accesses may overlap opcode fetches (Harvard), except LDM, data in the same region as the code, and data in ITCM; with code in uncached main RAM and data elsewhere the cost is typically "codetime + datatime - 2"; - internal cycles are half cycles and are sometimes skipped "depending on whether or not the next opcode is using the result" (that is the interlock model of layer 1); - VRAM accesses from the ARM9 gain a wait when the video controller reads VRAM at the same time (removable by forced blank); capture adds more. ## The modelling level this proposes **Instruction level with bus transactions and the TRM's cycle counts** (charter, axis B, "transaction" for "32-bit buses, caches"): each instruction is charged its DDI 0165B cycles and interlocks; every access that leaves the core is a transaction through the TCM / cache / write-buffer model; every AHB transaction goes to the board with its kind (fetch, line fill, uncached read, buffered or unbuffered write, burst position) and the board returns its cost in bus cycles, which the core converts to core cycles with the HCLKEN alignment. The per-cycle overlap of the two buses (layer 3's "MAX instead of SUM") is modelled only where a source states the rule (Korth's three exceptions); elsewhere the sum is charged and the choice is marked HYPOTHESIS. A full two-bus, per-cycle pipeline model would claim more than any judge can check: there is no hardware-tier timing oracle for the ARM9 (validation.md). ## Unknowns (U) and contradictions (C) - U1 The nonsequential penalty's cause and exact rule (BIU, arbiter, or memory controller). - U2 Whether code and data accesses really overlap on the AHB (a single AHB master port in DDI 0201D: how could they?). Korth's "codetime + datatime - 2" suggests the BIU overlaps the next fetch's address phase; needs a probe. - U3 Write buffer drain timing against later reads of the same address (does a read wait for the buffer to drain, or bypass it as DDI 0201D says for NCNB reads?). - U4 Cache line fill cost on each region (GBATEK gives only BIOS and main RAM). - U5 How DMA (the board's) and the ARM7's main-RAM accesses stall the ARM9's AHB transfers (EXMEMCNT bit 15 priority): board question, but it decides the core's interface (the core must accept a cost that includes arbitration). - C1 GBATEK's "0.5 cycles" for TCM/cache hits is one core cycle; DDI 0201D's "single cycle" agrees. Not a contradiction, a unit: recorded because the half-cycle notation is easy to misread. - C2 DDI 0165B (Rev 1) and DDI 0240B (r2p1) chapter 8: to be diffed in the build session; any row that differs is a C-item, and Rev 1 applies.