# Handoff: build `gekko-python`, the GameCube'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 `gamecube-python` (Phase 0 beside it, `../gamecube-python`); the Wii's Broadway is planned as a variant of this core, decided by the Wii Phase 0. The GameCube is an era-4 machine; read gamecube-python's domains-and-speed.md and execution-architecture.md before planning any rung that runs more than a test program. Models: `sh2-python` (an era-3 RISC core with a generated decode from the manual's tables, one handler per row, timing from the manual's tables, the cache as a module, a state that round-trips) is the closest; `vr4300-python`'s Phase 0 for a 64-bit RISC core with caches and a TLB; `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; never copy a chip file between repositories; no hardware probes; run anything that reads `reference/` with `python -I`; Dolphin, QEMU, MAME and every GPL/LGPL/unlicensed emulator are closed boxes (never fetched here; built and run from emu-tools-gpl); the Gekko manual is IBM's confidential document: facts are cited by section and page, nothing is copied; Nintendo's IPL is never traced or read (the MAME recipe starts tracing only at our own program). ### Read, in this order 1. `README.md`, `docs/start-here.md`. 2. `docs/isa.md`, `docs/paired-singles.md`, `docs/caches-dma-wgp.md`, `docs/mmu-exceptions.md`, `docs/timing.md`. 3. `docs/validation.md`, `docs/mame-oracle.md`. 4. The Gekko manual: chapters 2, 4, 5, 6, 9, 12 and appendix A (`reference/docs/gekko_um_2000-05.pdf`; regenerate its text with `pdftotext -layout`). The PEM for every architectural definition; the 750CL manual as a cross-check. Then `python -I scripts/fetch_references.py --verify`. ## Scope The Gekko as the GameCube uses it: big-endian, 32-bit; UISA, VEA and OEA as the 750 implements them; the FPU with paired singles and quantized loads and stores; HID0/1/2 and every implemented SPR (full SPR decode); the MMU (BATs, segments, page-table walk, both TLBs); the exception model; L1 instruction and data caches with tags, MEI states, pseudo-LRU and data; the L2; the locked cache, `dcbz_l` and the DMA engine; the write-gather pipe; the 60x transaction interface; the time base and decrementer; the performance monitor's registers (counting what the timing model can count). Not in scope: little-endian mode beyond the exceptions the manual defines; the thermal assist unit beyond its registers; power-saving modes beyond doze/nap/sleep entry and wake as state; JTAG. ## Proposed names - Repository `gekko-python`, package `gekko_python`; class `Gekko`; `CPUState` for snapshots; `Fpu`, `ICache`, `DCache`, `L2Cache`, `LockedCacheDma`, `WriteGatherPipe`, `Mmu`, `Tlb`, `Bus60x` (the protocol the board implements). - Modules: `_decode.py` (generated by `scripts/gen_decode.py` from our transcription of the PEM's and the UM's appendix A tables, page cited per row), `_integer.py`, `_branch.py`, `_system.py`, `_loadstore.py`, `_fpu.py`, `_paired.py`, `_quantize.py`, `cache.py`, `l2.py`, `dma.py`, `wgp.py`, `mmu.py`, `exceptions.py`, `timing.py` (the table-driven model), `bus.py`. ## The import boundary and the sockets - The core imports nothing from the board. The board gives it one bus object and pins (INT, MCP, SRESET, HRESET); the core gives `run(until_tick)`, `step()`, pin setters and its state. - **Time**: integer ticks of the 486 MHz core clock; the bus at 3 ticks a cycle (the board's ratio, a parameter), the time base and decrementer every 12 ticks (bus/4). - **The bus protocol**: each 60x transaction as kind (single-beat read/write by size, burst read, burst write with flush, the DMA's two burst-read transaction types), physical address, data, source (fetch, data miss, cast-out, uncached access, DMA, write-gather), issue tick; the board returns the completion tick. - **Sync points** (JIT proposal): every uncached transaction; every transaction while another master's DMA could touch the same lines (the board says when); `mtspr` to HID0, HID2, DMAL, WPAR, L2CR; interrupts sampled at instruction completion. - **Snapshots**: every field, including cache lines with data, tags, states and PLRU bits, the TLBs, the locked cache, the DMA queue, the write-gather buffer and its pointer, the timing model's scoreboard, so that a state loads into a fresh core and continues identically, in the board's shared format (late-era method L3). ## Unknowns (U) and contradictions (C) Unknowns, each with what would settle it (a hardware test with captured results unless noted; none is in hand): - U1. `divw`/`divwu` quotient on division by zero and on 0x80000000 / -1 (the PEM: undefined). - U2. `mulhw`/`mullw` and the other operand-dependent instructions: only timing depends on the operands (UM table 6-6); results are defined. Listed so rung 1 tests both. - U3. What a program sees if it uses a Gekko extension after enabling it in HID2 without invalidating the I-cache (UM 2.1.2.4 requires the invalidate; UM 2.2.1 even says PSE may change "only when the i-cache is invalidated and disabled"). - U4. Reset values of registers UM table 4-8 leaves "undefined" (to list at rung 2). - U5. The bit patterns of `fres`, `frsqrte`, `ps_res`, `ps_rsqrte` (the manual gives only an error bound and allows variation between executions). - U6. The internal representation of a paired single against a double ("unspecified", UM 2.2.1); what a double-precision instruction does to ps1. - U7. Which half's floating-point exception is reported, and whether ps1 is written when an enabled exception is taken on ps0. - U8. `frsp`'s ps1 result ("undefined"). - U9. Rounding inside `fmadds`/`ps_madd` and friends (once or twice), and single-precision operations on inputs that are not single-representable. - U10. The quantized load/store alignment rule (UM 4.5.6, two readings). - U11. GQR reserved types 1-3. - U12. A write-gather store with a non-zero low address (UM: "incorrect data in the buffer"). - U13. Bus timing of the DMA engine against the CPU's own misses beyond UM 9.3.3's description (the BIU's arbitration in cycles). - U14. The exact instruction at which an external interrupt is taken on an out-of-order core. Contradictions: - C1. The bus clock: YAGCD 2.1 says 200 MHz; the OS globals (YAGCD 4.2) say 162 MHz. - C2. UM table 2-45 gives 65525 for a u16 store at scale 31; the clamping rule gives 65535; the table's scale column is headed LD_SCALE in a store table. - C3. UM table 6-7 lists `ps_cmpu0` with primary opcode 0; appendix A gives 4. - C4. `fres` precision: "Error < 1/4000" (UM 2.2's feature list) against "one part in 4096" (chapter 12). - C5. `psq_l`'s pseudo-code sets the element size to "10" and "20" for 8- and 16-bit types; as byte counts they must be 1 and 2. - C6. YAGCD 4.1's "0xE0000000 16k L2 Cache" is the locked L1 half at a BAT-chosen address. - C7. UM 2.1.2.4 (invalidate the I-cache after setting PSE) against UM 2.2.1 (PSE may change only with the I-cache invalidated and disabled): which is required. - C8. The PVR: the UM says the processor version is 0x7000 ("early releases ... 0x0008"); what a retail Gekko returns is not in any source in hand (the commonly quoted 0x00083214 is HYPOTHESIS here; a hardware read would settle it). ## Rungs As validation.md: 0 decode, 1 integer UISA, 2 OEA, 3 FPU/paired/quantized, 4 caches/locked cache/DMA/WGP, 5 timing model, 6 on the board. Rungs 0-5 run on the core's own harness (a flat memory bus answering transactions); rung 6 needs gamecube-python's M1. ## Domains and speed The Gekko is the lead of the board's CPU domain and its order of record (gamecube-python domains-and-speed.md). Its channels to other domains are its bus transactions: uncached register accesses, write-gather bursts into the GX FIFO, misses and cast-outs that other masters' DMA can race, and the locked-cache DMA's bursts. **The need, with the arithmetic:** 486 MHz, up to 2 instructions retired a clock (972 M/s peak); typical game code at IPC 0.4-0.8 (HYPOTHESIS: unmeasured) is 194-389 M/s, 300 M/s as the working figure. Budgets at 300 M/s, in desktop-equivalent host cycles per instruction: desktop 5.0e9 / 300e6 = **16.7**, phone app 4.0e9 / 300e6 = **13.3**, phone browser 2.4e9 / 300e6 = **8.0**. An exact interpreter with cheap time-keeping costs 25-60 (HYPOTHESIS); with an SH-2-style stage scheduler 89-145 (MEASURED, sh2-rust-fast); a cached interpreter gains x1.15-1.4 at best (n64-python's refinement of the SH-2 evidence). **Whether the no-JIT path reaches real time:** desktop 0.33-0.84x, phone app 0.27-0.67x, phone browser 0.16-0.40x for typical games (20-50 cycles against the budgets above). **No target reaches real time for typical games without recompilation**; light titles might on desktop. Threads do not help the CPU itself (one Gekko); they help by moving the DSP and the pixel pipeline off its thread. Real time comes from turbo's block recompilation (native on desktop and Android, wasm in browsers) and exact idle-loop fast-forward; on iOS native apps (no JIT) only named concessions (`cpu_cache_off`, `inexact_idle_skip`, `cpu_timing_cpi`) could close part of the gap. **What Python can be:** the definition and a runner of test programs and slices, at 0.3-1.5 M instructions a second on PyPy (HYPOTHESIS), 1/200 to 1/1,000 of real time. ## Open decisions for Aubrey (each with my recommendation) 1. **Names**: `gekko-python` / `gekko_python`, class `Gekko`. Broadway as a model parameter of this core (HID4, eight BATs, its clock), not a separate repository, unless the Wii Phase 0 finds differences beyond the 750CL manual's. *Recommend* yes. 2. **Modelling level**: instruction-level semantics, transaction-level bus timing, and a table-driven core timing model marked FITTED/HYPOTHESIS as a whole (timing.md), with a constant-CPI comparison switch. *Recommend* this over a stage-accurate pipeline: no judge sees inside a stage. 3. **Paired-single representation** (U6): ps0 held as a 64-bit double, ps1 separately; double-precision instructions leave ps1 unchanged (HYPOTHESIS, stated). *Recommend*. 4. **Generated decode and restricted semantics** (JIT proposal J2): from day one, with the lint. *Recommend* yes: this is the core where a recompiler matters most in the family. 5. **Caches with data in the specification** (write-back model, PLRU, MEI); "no cache" only as the turbo concession `cpu_cache_off`. *Recommend* yes. 6. **Estimate tables** (U5): a documented deterministic estimate meeting the manual's bound, tagged HYPOTHESIS, replaced when hardware data appears; never a table taken from a GPL emulator. *Recommend*. 7. **Test programs**: our own assembler in `tests/` (as `scripts/mame_trace.py` does), with MAME's ppc603 as a detector for integer rungs. *Recommend* yes. 8. **ogamespec/gekko_cosim** (an IBM Gekko simulation model, provenance not stated; validation.md): a die-level oracle if it were usable. *Recommend* not using it or anything derived from it unless its provenance is established as releasable; record it as excluded. 9. **libogc / devkitPro**: devkitpro.org blocks AI crawlers (set aside, site's wishes). Does that extend to devkitPro's libraries and toolchain on GitHub? *Recommend* treating it as an expressed wish: no devkitPro library in our own tests; building third-party tests that need libogc (dolphin-emu/hwtests) only in emu-tools-gpl, and only if Aubrey reads the signal as covering the website alone. 10. **Local until rung 3**, then public with the family's minimal CI after a provenance check (nothing from GPL sources, no text from the confidential manual). *Recommend*. ## What was not verified in this phase (candour) - No Gekko-specific instruction ran anywhere: MAME has none, Dolphin was not built. - The Gekko manual was read at the sections cited (chapters 2, 4, 6, 9, 12, appendix A); chapters 3, 5, 7, 8, 10 and 11 were skimmed for the facts named, not read through. - The 750CL and 740/750 manuals were fetched and grepped, not read. - Speed figures are HYPOTHESIS with arithmetic shown, except the SH-2 yardstick (MEASURED).