# Handoff: build `r3000a-python`, the PlayStation's CPU core ## Context you are inheriting The family's shape: chips are cores, boards compose cores, CPUs always get their own core (charter). This core's first user is `psx-python` (`../psx-python`, Phase 0 done in parallel tonight; do not edit it from here). A board that finds a corner of this core raises it **on the core**. Era 3 (charter): a 32-bit CPU with caches, modelled at **instruction level with bus transactions**; Python is too slow for long runs, so cases are short and targeted; the Rust tiers carry the playing. Models to read before writing code: - `sh2-python` (`../sh2-python`): the nearest relative. A 32-bit RISC with delay slots, a cache, a bus contract with `kind` bits (`src/sh2_python/bus.py`), a decode table generated from the manual (`scripts/gen_decode.py`), one small handler per instruction (the JIT proposal's "restricted semantics" are already its style), a pipeline timing model. Copy its **shape**, never its code. - `z80-python`: the readability contract (one instruction family per module, one mnemonic per handler docstring, the hardware reason as a comment on the line that encodes it), tooling bar, docs layout. - `../emulator-lineage/docs/proposals/2026-11-jit.md`: write the handlers so that a cached interpreter (and perhaps a generator) can be built from them later: one pure- looking handler per decode row, named state, the bus through one interface, timing as data. Do not bend readability for it. Aubrey accepted the proposal on 2026-11-25 (decisions J1, J2, J4-J6): a specification declares sync points, memory kinds, timing as data and, for CPUs, a named handler subset checked by a lint (sh2-python's branch `handler-subset` is the pilot: follow its subset once it lands). psx-python docs/domains.md notes why a cached interpreter for this CPU can be exact for cached code: the i-cache never sees stores, so decoded lines change only on refill or flush. Rules: Python >= 3.11, CPython and PyPy; modern forms; no dangerous-looking shell one-liners; local git only; MIT; nothing read from GPL emulators; no BIOS or game bytes committed; run Python that reads `reference/` with `-I`. ### Read in this order README.md; docs/start-here.md; docs/cpu.md; docs/cop0-and-caches.md; docs/gte.md; docs/validation.md; docs/mame-oracle.md; then `reference/src/psx-spx/docs/ cpuspecifications.md`, `memorymap.md`, `memorycontrol.md` (BCC), `geometrytransformation enginegte.md` in full, and the LR33300 ch.3-4 scans. ## Scope In: the CW33300 integer unit, HI/LO and the multiply/divide unit with its busy time, COP0 (all registers including the debug ones and the garbage window), exceptions and interrupts (one level input), the 4 KB i-cache with tags and per-word valid bits and the isolate/tag modes, the 1 KB scratchpad, BCC (FFFE0130h), the 4-word write queue, load scheduling, and the GTE as `r3000a_python.gte`. Out (psx-python): RAM, BIOS ROM, memory control 1F801000h-1F801020h and 1F801060h, DMA, I_STAT/I_MASK, timers, MDEC, GPU, SPU, CD, SIO, expansion. ## Proposed names - Repository `r3000a-python`, package **`r3000a_python`** (the 2026-11-12 naming rule). - `r3000a_python.CW33300`: the core as Sony configured it (1 KB scratchpad, 4 KB i-cache, PRId 2, the LR33300-family debug registers). A plain `R3000A` base class only if a second configuration appears (YAGNI until then). - `r3000a_python.gte.GTE`: COP2; `r3000a_python.coprocessor.Coprocessor`: the protocol the core calls for COP1-3 (`mfc`, `cfc`, `mtc`, `ctc`, `command`, `busy_until`); an absent coprocessor raises CpU. - `bus.py` with `kind` bits in sh2-python's style: `FETCH`, `FILL` (an i-cache refill word), `FIRST`, `UNCACHED` (KSEG1), `QUEUED` (a write leaving the write queue), `WRITE`, and `observe`-only `HIT`. ## The bus contract (proposal, to settle in the first commit) `read(physical, size, kind) -> (value, cycles)` and `write(physical, size, value, kind) -> cycles`. The core strips the segment, answers the scratchpad and FFFE0130h itself, turns i-cache misses into `FILL` reads (from the missed word to the line's end, per IBLKSZ), and passes stores through the write queue (a store returns 1 cycle while the queue has room; queued writes leave in order when the bus is free, a KSEG1 access or a read of a queued address drains them first). The full 32-bit GPR value goes with every store, with `size` saying 1, 2 or 4 (cpu.md: 32-bit I/O registers see it). The board returns the cycles each transaction costs (its memory-control delays, DMA holding the bus); the core charges them. One method `run(cycles)` runs until a cycle budget is used and returns at an instruction boundary; `set_interrupt(level: bool)` drives CAUSE bit 10. Time is the CPU clock (33.8688 MHz). The board's other domains (GPU, SPU, CD) meet the CPU at the channels psx-python declares; the core only needs to stop at the boundary the board asks for (`run` until a stamp). ## Unknowns (U) and contradictions (C) - U1 ALU write to a register in the delay slot of a load to the same register: which wins. - U2 The exact interrupt sampling point relative to a COP2 command (EPC = the command). - U3 REGIMM rt values other than 00000, 00001, 10000, 10001 (decoded by bit 0 and bits 4-1?). - U4 The "do not touch HI/LO for 2 cycles" rule (psx-spx, not understood). - U5 SR.RE (reverse endian) on this part. - U6 The 2025-26 i-cache refill and tag details in psx-spx: which test program shows them. - U7 DCIC bits 12-13 "jump redirection" and bits 14-15. - U8 PRId on CXD8530 chips (only CXD8606CQ = 2 is documented). - U9 cop0r16-31 "garbage" values. - U10 How many cycles the CU2 enable takes to act. - U11 Other KSEG2 addresses than FFFE0130h. - U12 KUSEG above 512 MB: address error from the core or bus error from the board. - U13 Whether ps1-tests' `psx.log` files are console outputs (presumed). - U14 Whether the PS2 IOP's PS1 mode has this GTE (affects only the chip-rule table). - U15 MTC2/CTC2 landing delays (2 vs 3 cycles). - U16 LWC2/SWC2 delays ("2 instructions? That's wrong!" in psx-spx). - U17 Whether amidog's suites were verified on hardware (no statement in the readmes). - U18 Why SST's LWL/LWR/SWL/SWR cases make more than one bus access in some cases. - C1 MULT timing: psx-spx 6/9/13 cycles by rs magnitude (thresholds 800h, 100000h) vs the LR33300 manual's 4/7/12 by "any operand" < 512 / < 256K. Judge: amidog / a timing probe. - C2 D-cache size: LR33300 2 KB vs the PlayStation's 1 KB scratchpad (the PlayStation value is what software sees; the configuration is Sony's). - C3 SingleStepTests r3000's README says exceptions are disabled; 5,462 of its cases change CAUSE (alignment, overflow, BREAK, SYSCALL). - C4 MAME charges ~2 cycles per instruction in every window measured; the hardware's cost varies with cache and bus (cpu.md, mame-oracle.md). Not a contradiction about behaviour, a reason not to take MAME's frame timing as a judge. ## Milestones (rungs; exit tests in validation.md) 0. Tooling and decode: `gen_decode.py` from psx-spx's encoding tables (facts) into a committed `_decode.py`; disassembler matching MAME's text on the recipe's 566,120 lines (mnemonics and operands); state/trace/debug tools in z80-python's shape. 1. SingleStepTests: 55,000 cases, delay records included. 2. MAME lockstep: BIOS boot (psu 2.2a) and PeterLemon CPUTest EXEs, register writes. 3. On psx-python: PeterLemon CPUTest all PASS, amidog psxtest_cpu/cpx all PASS. 4. GTE: ps1-tests test-all 1150/1150, amidog psxtest_gte all PASS. 5. Timing: access-time table, MULT/DIV interlocks, GTE stalls, i-cache refills. 6. Seeded faults, each caught. Speed target for the Python core: correctness only. For orientation, sh2-python runs about 0.1-0.2 M instructions/s on its harness loop (sh2-rust docs/status.md); expect the same order here, i.e. **roughly 1/200 of real time** (the PlayStation needs up to 33.9 M instructions/s, psx-python docs/domains-and-speed.md). Rung cases must stay short. ## Open decisions for Aubrey 1. **The GTE in this repository, as module `r3000a_python.gte`, not its own repository.** Recommended: every known GTE sits on the same die as this core and is reached only through COP2; a separate repository would have one consumer and would put the busy interlock across a pin. Extract only if a consumer needs a GTE without this CPU. 2. **One class, `CW33300`, no generic `R3000A` base yet.** Recommended: no second configuration exists in the family's plans; split when one appears. 3. **MAME's BSD-3 psx device files stay closed.** Their `license:` lines say BSD-3-Clause (mame-oracle.md), which the family's rules would allow with notice; the brief tonight says closed box. Recommended: keep them closed (MAME's psx code was written alongside GPL code in one tree, and the tier-3 sources are enough); revisit only if a specific question has no other source. 4. **Timing model: instruction level with bus transactions, load scheduling and interlocks, no per-stage pipeline model.** Recommended: it is what the evidence supports (LR33300 ch.3, psx-spx, ps1-tests access-time); a stage model would claim more than we can judge. 5. **The i-cache in the specification, not as an optimisation.** Recommended: its contents change timing and, with self-modifying code, results (Formula One 2001). 6. **Rungs 3-5 run on psx-python.** Recommended: the tier-3 suites print through the GPU; building a GPU stub here would duplicate the board. 7. **Default provisional choices** (Aubrey's 2026-11-12 rule: take a default, mark it provisional): U1 ALU write wins over the pending load; U3 decode by rt bit 0 with link when rt bits 4-1 = 1000b; U12 address error from the core. Each marked HYPOTHESIS in code until a judge settles it.