# Handoff: build `vr4300-python`, the Nintendo 64's CPU core ## Context you are inheriting The family's shape (charter): CPUs always get their own core; boards compose pinned cores; `x-python` is the specification. This core's first user is `n64-python` (Phase 0 beside it, `../n64-python`); the RSP is a separate core proposed in `../rsp-python` (Phase 0 docs only). The N64 is an **era 3** machine (charter): transaction level appears, Python is too slow for long runs, cases are short and targeted. Read n64-python's `docs/domains-and-speed.md` before planning any rung that runs a game. Models: `sh2-python` (an era-3 RISC core: generated decode from the manual's table, one handler per row, a pipeline module timing from a manual's tables, the cache as a module, a `ChipState` that round-trips) is the closest; `z80-python` for the readability contract; `m68000-python` for the claims boundary (`docs/claims.md`). Rules that bind this work: oracle tiers (validation.md); Python floor 3.11, PyPy green; reference cores are correct, not fast; modern over compat; no dangerous-looking one-liners; never copy a chip file between repositories; no hardware probes; run anything that reads `reference/` with `python -I`; GPL emulators (MAME, mupen64plus, gopher64, simple64, MiSTer) are closed boxes, never read; Nintendo's PIF ROM and IPL3 are never committed, copied, disassembled or read. ### Read, in this order 1. `README.md`, `docs/start-here.md` (what the chip is, the boundary, the N64 deviations). 2. `docs/isa.md`, `docs/cp0-tlb.md`, `docs/fpu.md`, `docs/timing.md`. 3. `docs/validation.md` (oracles, rungs), `docs/mame-oracle.md`. 4. The manual: UM chapters 3, 4, 5, 6, 11, 16, 17, 19 (`reference/docs/vr4300.txt` is `pdftotext -layout` of it: regenerate it, it is not committed). 5. `reference/src/nemu64-test/src/tests/testlist.rs` and the modules it names (MIT). Then `python -I scripts/fetch_references.py --verify` and `python -I scripts/fetch_wiki.py --verify`. ## Scope The whole VR4300 as the N64 uses it: big-endian, 32-bit kernel mode first but the 64-bit modes complete (n64-systemtest's `tlb64`), CP0 and the TLB, CP1, both caches, the write buffer, the interlocks as stall rules, the SysAD transaction interface, reset/NMI/Int0-4. Not in scope: the VR4305/VR4310 variants' clock ratios beyond a parameter, JTAG, the low-power mode beyond Status.RP's clock division (U15), parity/cache-error registers (unused on the part, UM Table 1-2). ## Proposed names - Repository `vr4300-python`, package `vr4300_python` (the family's naming rule). - `VR4300` (the chip), `CPUState`/`ChipState` (snapshots), `Cop0`, `Fpu`, `ICache`, `DCache`, `WriteBuffer`, `Tlb`, `SysAD` (the bus protocol: `read(kind, paddr, size) -> (data, time)`, `write(...) -> time`, `block_read(paddr, words) -> (data, time)`). - Modules: `_decode.py` (generated from `scripts/gen_decode.py` over a transcription of UM Figure 16-1 and chapter 17's encoding figure, page cited per row), `_arith.py`, `_logic.py`, `_shift.py`, `_muldiv.py`, `_loadstore.py`, `_branch.py`, `_trap.py`, `cop0.py`, `tlb.py`, `fpu.py`, `cache.py`, `pipeline.py` (the stall rules), `bus.py`. ## The import boundary and the sockets - The core imports nothing from the board. The board gives it one `SysAD` object and pin callables; the core gives the board `run(until_pclock)` and `step()`, and pin setters (`set_int(n, level)`, `nmi()`, `reset(cold)`). - Time: the core counts **half-PClocks** as integers (timing.md: one SClock = 3 half-PClocks at the N64's 2:3 ratio; Count advances every 4). Bus responses are returned as a duration in SClocks, which the core converts. The board decides what an access costs. - Sync points (JIT proposal): every SysAD transaction to a non-RDRAM physical address is a sync point (another domain can see it); RDRAM accesses are sync points only when the board says another domain is writing RDRAM in that window (DMA); interrupts are sampled at instruction boundaries. The core reports each transaction with its PClock so the board can order it against the RCP. - Snapshots: every field, including cache lines, tags, the write buffer, the micro-TLB and the pipeline's stall state, so a state loads into a fresh chip and continues identically (the sh2-rust lesson, 2026-11-25). ## Unknowns (U) and contradictions (C) Unknowns, each with the judge that would settle it: - U1. The exact hardware HI/LO after division by zero and on overflowing DIV (nemu64-test `arithmetic`, krom DIV). - U2. Whether MULT/DIV inputs that are not sign-extended behave as n64brew says on PRId 0x0B10 parts too (only 2.2 reported). - U3. Integer divide-by-zero results (as U1; listed apart because DDIV differs). - U4. Whether a MFHI/MFLO hazard exists at all on this part, given the whole-pipeline stall (nemu64-test `timing` says MFLO right after MULT costs 6 = 5 + 1). - U5. COP2 and LWC2/SWC2/LDC2/SDC2 behaviour with CU2 set (nemu64-test `cop_unusable`). - U6. Random: per instruction or per PCycle, and during stalls (nemu64-test `cop0`). - U7. TLB shutdown (Status.TS) on the VR4300: whether multiple matches set it (`tlb`). - U8. The cycle at which an interrupt pin change is taken (n64-python's interrupt timing tests; nemu64-test `timing`). - U9. The multiplication bug's exact conditions (no judge we hold). - U10. Python's single-precision and directed rounding: correctness of a hand rounding implementation (nemu64-test `cop1`; a generated test set against a C reference built locally is a possible extra detector). - U11. The cost of FPU operations that raise exceptions (nemu64-test says unknown). - U12. CACHE on uncached addresses and unlisted ops (no judge). - U13. Whether the RCP ever issues SysAD external requests (none known). - U14. Whether MAME models the caches (run N64-Cache-Emulation-Tests in MAME). - U15. Status.RP (reduced power) on the N64: whether any software sets it (none known). - U16. The reserved-CP0-register latch: which writes feed it (all MTC0/DMTC0? CP0 writes by exceptions?) (nemu64-test `UnusedRegistersExtra*`). Contradictions: - C1. Write buffer: "eight words (32 bytes), equal to the data cache line size" vs a 16-byte D-cache line and four doubleword entries (UM p.120 vs p.275). - C2. CP0 hazard numbers up to stage 8 on a five-stage pipeline (UM chapter 19): a compatibility table, not the part's behaviour. - C3. Random is "read-only" yet bit 5 is "readable and writable by software" (UM p.147). - C4. R4000 HI/LO interlock model vs the VR4300's whole-pipeline stall (UM p.76; nemu64-test). - C5. The SRA/SRAV and 32-bit multiply/divide descriptions in UM chapter 16 vs n64brew's hardware observations (start-here.md): the hardware wins (charter: directness). ## Milestones (rungs) As validation.md: rung 0 decode, 1 integer ISA, 2 CP0/TLB/exceptions, 3 CP1, 4 caches and write buffer, 5 timing, 6 CP0 hazards. Each exits on the named judges; rungs 1-6 need n64-python's M1 board (RDRAM, PI serving a ROM, MI interrupt, a result reader), built alongside. A rung's report says which judges ran, with counts, and which tier. ## Open decisions for Aubrey (each with my recommendation) 1. **Names.** `vr4300-python` / `vr4300_python`, class `VR4300`. *Recommend* yes. 2. **Modelling level.** Instruction-level semantics, transaction-level bus timing, the UM's interlocks as stall rules, caches and write buffer modelled (timing.md). *Recommend* this over a stage-by-stage pipeline: no judge we can run sees inside a stage, and the stage-level model costs several times the speed of an already slow core. 3. **Build the test ROMs.** nemu64-test (MIT) is the main judge and must be built with Rust + `nust64` (a cargo install from crates.io, and a MIPS target). *Recommend* building it once locally in M1 (a few minutes on one core, outside tonight's limits), pinning the built ROM's SHA-256, never committing it; it boots with libdragon's open IPL3, so no Nintendo code is involved. 4. **The deviations.** Model SRA/SRAV, the MULT/DIV input leakage and the imprecise load interlock as hardware does (tier 3, OBSERVED, with the manual's text cited as the reason the line looks wrong); model the multiplication bug as an off-by-default switch of an early-revision machine row, or not at all. *Recommend* the former three now, the bug not at all until a judge exists. 5. **Semantics in a restricted form** (JIT proposal, decisions 2 and 3). The decode is generated from the manual's figure from the start; the handlers keep to a declared subset (integer arithmetic on named state, one bus interface, no hidden state) with a lint. *Recommend* adopting it for this core from day one: the N64 is where a cached interpreter or a generated Rust interpreter matters most (n64-python domains-and-speed). 6. **Local until rung N.** *Recommend* local through rung 3, then public with the family's minimal CI (PyPy only), after a provenance check that nothing came from GPL source or Nintendo code. 7. **sources.md rows.** The new sources (n64brew, nemu64-test, krom N64, Dillon's tests, cen64, the cache tests, bigbass tests, the NEC and MIPS manuals) are proposed in n64-python's `docs/sources-n64.md` for whoever owns `emulator-lineage` to add. *Recommend* adding them as written there. ## What was not verified in this phase (candour) - No test ROM was built or run except krom's CPUADD (and RSPCPUADD) under MAME. - The UM was read for chapters 1, 3 (multiply/divide), 4, 5.4.2, 6.4.2, 7.6, 11.2, 16 (SYNC, CACHE, the opcode figure) and 19; chapters 6 (per-exception register updates), 9, 10, 12 and 17 were not read in detail. - The R4400 manual was fetched and not read beyond its title. - n64brew claims are the wiki's; nemu64-test's expectations are its author's.