# Handoff: build `arm946es-python`, the Nintendo DS's ARM9 ## Context you are inheriting Chips are cores, boards compose cores, and CPUs always get their own core (charter). This core's first user is `nds-python` (`../nds-python`, Phase 0 done the same night; do not edit it from here). A board that finds a corner of this core raises it **on the core**. The first decision Phase 0 had to answer is in [core-choice.md](core-choice.md): this is a **new core**, not an `arch` axis on `../arm7tdmi-python`, because the ARM946E-S differs from the ARM7TDMI in its pipeline, its two buses, its timings and its whole memory system, not only in a dozen instructions. arm7tdmi-python stays the DS's ARM7 (and the DS's GBA mode) unchanged. Era: the DS sits between era 2 and 3 (two CPUs at 67 and 33 MHz with caches; charter table). Model the ARM9 at **instruction level with bus transactions** (timing.md: the TRM's per-instruction counts and interlocks, the memory system's transactions, the board's wait states); Python will be too slow for long runs, so cases are short. Models to read before writing code: - `../arm7tdmi-python`: the shape (mixins per instruction family, generated decode tables with `--check`, the tools, the readability test, docs layout). Copy its **shape**, never its code; re-check every ARM7 corner it records against the ARM9's sources. - `../sh2-python` and `../r3000a-python` (brief): 32-bit cores with caches and a bus contract with `kind` bits; the JIT proposal's restricted handler style. - `../emulator-lineage/docs/proposals/2026-11-jit.md` (accepted 2026-11-25): one pure-looking handler per decode row, named state, the bus through one interface, timing as data, sync points and memory kinds declared. Do not bend readability for it. Rules: Python >= 3.11, CPython and PyPy; modern forms; no dangerous-looking shell one-liners; local git only; MIT; nothing read from GPL/MPL emulators (melonDS, DeSmuME, NooDS, DualSOUP), MAME's BSD-3 ARM files kept closed (Open decision 4); no BIOS, firmware or game bytes committed; run Python that reads `reference/` with `-I`. ### Read in this order README.md; docs/start-here.md; docs/core-choice.md; docs/isa-v5te.md; docs/cp15-tcm-pu-caches.md; docs/timing.md; docs/validation.md; docs/mame-oracle.md; docs/provenance.md; then DDI 0165B chapters 2 and 8, DDI 0201D chapters 2-6, the ARM ARM (DDI 0100E) parts A3-A5 and A10 (the v5E instructions), and GBATEK "ARM CPU Reference" and "DS Memory Timings". ## Scope In: the ARMv5TE integer core (ARM and Thumb), its banks, PSRs with Q, exceptions with the base-restored abort model, the coprocessor interface with CP15 inside and every other coprocessor absent; the ITCM and DTCM (sizes, virtual size, load modes, DTCM base), the protection unit, the I- and D-caches (4-way, lines, lockdown, clean/invalidate, round-robin and pseudo-random replacement), the write buffer, the BIU turning core accesses into AHB transactions at a configurable core:bus clock ratio, the CP15 wait-for-interrupt halt. Out (nds-python): everything at 02000000h and up that the AHB reaches (main RAM, shared WRAM, I/O, VRAM, palette, OAM, the GBA slot, the BIOS ROM at FFFF0000h), the DS's wait states and nonsequential penalty (timing.md U1, default board), bus arbitration with the ARM7 and DMA, interrupt sources (the core takes IRQ and FIQ lines). ## Proposed names - Repository `arm946es-python`, package **`arm946es_python`** (the 2026-11-12 naming rule). - `arm946es_python.ARM946ES`: the core as Nintendo configured it: I-cache 8 KB, D-cache 4 KB, ITCM 32 KB at 0, DTCM 16 KB, Main ID 41059461h, cache type 0F0D2112h, TCM size 00140180h. Constructor argument `clock_ratio` (2 for the DS; the DSi switches between 2 and 4, GBATEK SCFG_CLK bit 0, so it is also settable at run time). No configuration object for other cache sizes until a second user exists (YAGNI). - Modules in arm7tdmi-python's style: `_alu.py`, `_multiply.py` (with the E multiplies), `_saturate.py` (Q ops, CLZ), `_transfer.py`, `_block.py`, `_branch.py`, `_psr.py`, `_exception.py`, `_coprocessor.py`, `cp15.py`, `tcm.py`, `cache.py`, `protection.py`, `write_buffer.py`, `biu.py`, `_tables.py` (generated), `bus.py` (the contract's kinds), and the tools `state.py`, `disasm.py`, `debug.py`, `console.py`, `trace.py`. ## The bus contract (proposal, to settle in the first commit) The board implements, at the **AHB** (behind the core's TCMs, caches and write buffer): - `read(address, size, kind) -> (value, bus_cycles)` and `write(address, size, value, kind) -> bus_cycles`, with `kind` bits: `FETCH` (instruction side), `LINEFILL` (one word of an 8-word burst; `FIRST` on its first word), `UNCACHED`, `BUFFERED` (a write leaving the write buffer), `SEQ` (the AHB says sequential), and `observe`-only bits for hits (`TCM`, `HIT`) so a debugger can see every access the core makes even when the board is not asked. - The board returns the transaction's cost in **bus cycles** (33.51 MHz), its wait states and any arbitration stall included; the core converts with `clock_ratio` and the HCLKEN alignment (DDI 0201D 6.4). - `run(until)` runs to a time stamp (core cycles) and returns at an instruction boundary; `set_irq(level)`, `set_fiq(level)`; `halted` is true after a CP15 wait-for-interrupt until nIRQ or nFIQ is asserted, "regardless of whether the interrupts are enabled or disabled" in the CPSR; the write buffer drains meanwhile (DDI 0201D 2.3.10). - Invalidation hooks for the board: the core's caches do not snoop (DDI 0201D 3.2.3), so the board never calls into the caches; DMA into memory the ARM9 has cached is stale until software cleans and invalidates, exactly as on the hardware. ## Unknowns (U) and contradictions (C) The detailed lists live with their subjects: - isa-v5te.md U1-U7, C1-C2 (unpredictable LDRD/STRD forms, empty-list transfers, the external abort, MCR2 to CP15, Thumb hi-register forms, Q on SMLA, the reset control value; GBATEK vs TRM on control bits 1 and 15, LDRD alignment); - cp15-tcm-pu-caches.md U1-U5, C1 (the pseudo-random replacement, dirty granularity, the nonsequential penalty's home, BIST, small PU regions; drain write buffer on the ARM9); - timing.md U1-U5, C1-C2 (penalty rule, code/data overlap on one AHB port, write-buffer drain against reads, line-fill costs, arbitration; half-cycle notation; Rev 1 vs r2p1). The two that shape the code: **timing.md U1** (does the 3-cycle nonsequential penalty belong to the core's BIU or the board's bus? default the board) and **U2** (code and data overlap: modelled only where GBATEK states the rule). ## Milestones (rungs; exit tests in validation.md) 0. Tooling and decode (`gen_tables.py` from the ARM ARM; disassembler vs MAME's text on the 372,403-line boot trace). 1. Instruction semantics: synthetic cases per new instruction and per changed rule; ARMv4T subset in lockstep with arm7tdmi-python (results only). 2. MAME lockstep over the BIOS boot. 3. On nds-python: rockwrestler's ARMv5 and TCM groups all PASS. 4. CP15, caches, PU, write buffer cases from DDI 0201D. 5. Timing: DDI 0165B chapter 8 as data; GBATEK's table reproduced through nds-python. 6. Seeded faults, each caught. ## Domains and speed **Domains.** The ARM9 with its TCMs, caches and write buffer is one domain: everything in it runs on the core clock and nothing outside can see inside (TCMs are not on the AHB; the caches do not snoop). It meets the rest of the DS only at the AHB: every transaction that leaves the BIU is a potential sync point, and of those only the ones to memory or registers another domain can touch need one (shared WRAM, main RAM, the IPC registers, I/O). nds-python's docs/domains.md places this domain beside the ARM7's and the video and sound domains and names the channels. **Instructions per second the ARM9 needs.** Peak one instruction per core cycle at 67.03 MHz: **67 M/s**, reached only from TCM and cache with no interlocks (DOC). MAME's model runs 22.3 M/s (3.00 core cycles per instruction, OBSERVED, mame-oracle.md) and is a flat model. Real game code runs mostly from the I-cache and DTCM, with uncached main-RAM data costing 10+ bus cycles per access (timing.md): planning figure **40 M/s**, ceiling 67 M/s (HYPOTHESIS until nds-python measures game loops). **Is the no-JIT path enough?** Measured family figures (this machine, an i9-13900K core, under load 58-65): arm7tdmi-rust 33.9-39.6 M instructions/s native, 41.5 M as wasm32 under Node (arm7tdmi-rust docs/status.md, 2026-11-25); the family's `-fast` forks gain x1.4-1.9 over their transcriptions. An ARM946E-S transcription does more per instruction (TCM and cache look-ups on both sides, the write buffer, interlock bookkeeping): estimate **25-40 M/s native** for `arm946es-rust`, 35-75 M/s for `-fast` (HYPOTHESIS). | Target | `-fast` estimate | Need (plan / ceiling) | Verdict | | --- | ---: | ---: | --- | | Desktop, native | 35-75 M | 40 / 67 M | real time for typical code; the ceiling needs the turbo tier's cached interpreter | | iPhone app, native (no JIT) | 28-60 M (x0.8) | 40 / 67 M | cached interpreter needed | | Browser (wasm), desktop | 21-45 M (x0.6) | 40 / 67 M | cached interpreter needed, marginal | | Browser, iPhone Safari | 17-36 M | 40 / 67 M | not real time without recompiling to wasm | | ARM handheld (A55 class) | 7-15 M (x0.2) | 40 / 67 M | needs native recompilation (allowed there) | (Device ratios as in psx-python docs/domains.md, all HYPOTHESIS until measured.) **Why a cached interpreter can be exact here.** For code fetched through the I-cache, what executes is the cache line's content, which changes only on a line fill or an invalidate (the I-cache never sees stores, DDI 0201D 3.2.3): a decoded-instruction cache keyed to modelled I-cache lines is invalidated exactly when they are. Code in ITCM changes on stores to ITCM (the core sees all of them: ITCM is not on the AHB) and code run uncached from main RAM changes on any store or DMA there (the board must tell the turbo tier). That is the memory-kinds table the JIT proposal asks for, in three rows. ## Open decisions for Aubrey 1. **A new core, not an architecture axis on arm7tdmi-python** (core-choice.md option B). Recommended: the two chips differ in pipeline, buses, timing and memory system; an axis would rewrite arm7tdmi-python's heart and risk its gates. Alternative C (import arm7tdmi-python's pure helpers) is cheaper but couples two chips through private modules: not recommended. 2. **Modelling level: instruction level with bus transactions, the TRM's cycle counts and interlocks, no per-stage two-bus pipeline model.** Recommended: it is what DDI 0165B and GBATEK can judge; no timing oracle can check more. 3. **The caches, TCMs, PU and write buffer in the specification, not as optimisations.** Recommended: they decide results (stale data after DMA, self-modifying code, aborts) as well as timing, and software relies on DTCM and ITCM for speed. 4. **MAME's BSD-3 ARM and DS files stay closed** (their licence lines would allow reading with notice). Recommended: closed, as for psx-python; the ARM documents are complete for the ISA, and MAME's ARM9 is a flat-timing model. 5. **The DS's nonsequential penalty in the board, not the core** (timing.md U1). Recommended provisional default: GBATEK says the ARM7 sees it on main RAM too, which points at the bus or memory controller. 6. **Jakly's work set aside pending your word** (provenance.md): DualSOUP's CONTRIBUTING says "I really dont like AI ... any AI usage will be frowned upon and rejected". Its scope is contributions to DualSOUP; the brief says stop, record and ask. Recommended: treat it as an objection and leave Jakly's works out (DualSOUP, polyrastertest, ndsframereplay, the Jaklyy/ndsdoc fork); none is needed for this core. 7. **Default provisional choices** (Aubrey's 2026-11-12 rule): pseudo-random replacement as round-robin until a probe shows otherwise (HYPOTHESIS); LDRD/STRD 4-aligned per GBATEK; MCR2/MRC2 to CP15 undefined; empty-list LDM/STM transfer nothing, base +/- 40h, two cycles. Each tagged HYPOTHESIS in code until a judge settles it. 8. **Probe queue for a real DS** (probe batching, if Aubrey ever has one): asiekierka's cached-memory-performance, a pseudo-random replacement probe, a CP15 reset read-back, LDRD/STRD alignment. Recommended: queue them; nothing blocks on them.