# Handoff: build `v30mz-python`, the WonderSwan's CPU core ## Context you are inheriting Chips are cores, boards compose cores, CPUs always get their own core (charter). This core's first and only user is [wonderswan-python](../wonderswan-python) (Phase 0 done the same day); a board that finds a corner of this core raises it **on the core**. Era 1-2 in the charter's table: one CPU, cycle level, Python runs the tests, Rust plays. Read first: [README.md](../README.md), [start-here.md](start-here.md), [timing.md](timing.md), [flags-and-undocumented.md](flags-and-undocumented.md), [sources.md](sources.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md). Then `python -I scripts/fetch_references.py` and read the PUM (`reference/text/`) chapters 3, 5 and Appendix A in full, the WSdev CPU pages (`reference/wiki/`), and WSCpuTest's README. Shape to copy (never code): **z80-python** (readability contract, docs layout, tooling), **huc6280-python** (a CPU with a SoC around it), and **i8086-python**'s documents for the 8086 execution model, ModR/M and prefixes. Rules: Python >= 3.11, CPython and PyPy (classes under ~80 attributes); modern forms; local git only; MIT with SPDX headers; nothing read from GPL emulators (Mednafen) or from unlicensed emulator source (ARMV30MZ); WSCpuTest and WSTimingTest are unlicensed test programs: read for facts, run, never copy; no boot ROM or game bytes committed; Python that reads `reference/` runs with `-I`. ## Scope In: the register file, FLAGS with its fixed bits, segmentation and 20-bit wrap, ModR/M and every 8086/80186 instruction the PUM lists, the seven-prefix rule, string instructions and their interruption, the interrupt model (INT 0-5, NMI, INT input with an acknowledge callback returning the vector, the 5.3 blocking cases), HALT/standby, reset, the clock table with the PUM's corrections, the undefined-flag rules of WSCpuTest, the ASWAN/SPHINX row, NOP behaviour of undefined opcodes, a disassembler with both Intel and NEC names. Out (the board): the SoC's interrupt controller (ports B0-B7), DMA and its CPU stalls, the cartridge bus width and wait states (the core is told the cost of each access by the host), the 12.288 MHz quadrant arbitration. ## Proposed names - Repository `v30mz-python`, package **`v30mz_python`**; Rust tiers `v30mz-rust`, `v30mz-rust-fast` when the board needs them. - `v30mz_python.V30MZ(bus, soc="aswan" | "sphinx")`; `step() -> clocks` runs one instruction or one interrupt entry; `Registers`, `Flags`; `disassemble(bus, cs, ip, names="intel" | "nec")`. - `v30mz_python.bus.Bus` Protocol: `read8(addr, kind)`, `write8(addr, value, kind)`, `read16`/`write16` with the odd-address split done by the core, `in8/in16/out8/out16`, each returning or accepting the **wait clocks** the host charges; `kind` is fetch, data or stack, so the board can model the cartridge bus. - `int_line(level: bool)`, `nmi()`, `acknowledge: Callable[[], int]` (the vector). ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | Registers, ModR/M, MOV/ALU/stack/branch/call/string groups, segment wrap, prefixes, INT/IRET, results only | PUM (2), WSDEV (3) | unit tests from PUM chapters 3 and 5 pass; the MAME boot-ROM trace's instruction stream and registers match for 1 frame (7,148 instructions), flags masked | | M2 | Every flag rule of WSCpuTest, MUL/DIV with exceptions, BCD, shifts with masked counts, the ASWAN/SPHINX row, undefined opcodes as NOPs | WSCPU (3) | WSCpuTest v0.7.1 prints Ok on both rows inside the core's test host; seeded flag faults each caught | | M3 | Interrupts in full: 5.3 blocking, NMI pending, HALT release, string instructions interrupted and resumed with up to 7 prefixes | PUM 5 (2) | unit tests per case; the board's boot ROM reaches its splash with interrupts on | | M4 | The clock table: PUM Appendix A with its five corrections, per form, as data | PUM A (2) | every row of Table A-1 is a test; the MAME boot trace's clocks compared and every difference explained or listed under C1 | | M5 | A prefetch-queue model (16 bytes, refill after branches, target prefetch on conditional branches) only if the board needs it to pass its timing tests | WSTIME on hardware (3) when numbers exist; PUM 3.3 | the board's timing-sensitive tests (ws-test-suite timing, WSHWTest) pass with it and not without it; otherwise leave it out and record why | ## Unknowns (U) and contradictions (C) | # | Unknown | Best source now | | --- | --- | --- | | U1 | The cost of refilling the queue after a taken branch | none written; MAME charges 10-11 for a taken Bcc (HYPOTHESIS until hardware numbers) | | U2 | WSTimingTest's results on real units | its author's screen only; ask for published numbers (Aubrey's call: outside contact) | | U3 | Clocks of interrupt entry | WSDEV TODO | | U4 | Other ASWAN/SPHINX CPU differences | WSCPU knows one (MULU ZF) | | U5 | Undefined flags outside WSCpuTest's coverage | none | | U6 | FLAGS bit 15 (MD) behaviour | reset value F002h only | | # | Contradiction | Sources | Resolution for now | | --- | --- | --- | --- | | C1 | Taken conditional branch: 4 clocks or 10-11 | PUM A vs MAME 0.285 trace | follow PUM; tag the extra HYPOTHESIS | | C2 | Where the post-branch cost lives (fixed vs destination-dependent) | PUM vs WSTIME README | queue model at M5 if needed | | C3 | Removed V30 instructions: "undefined result" vs NOP | PUM 1.3 vs WSCPU/WSTIME | NOP (TEST ROM over DOC), with PUM's reading kept in a comment | | C4 | NEG 0 sets OF and AF? | MAME (yes) vs WSCPU (no) | WSCPU | ## Domains and speed **Domains.** The CPU is not a domain of its own: on the WonderSwan it shares the SoC's 12.288 MHz clock with the display and sound quadrants and meets them only through the board's I/O ports, RAM and the interrupt line, so the board's whole SoC is one domain (wonderswan-python docs/domains-and-speed.md). The core's channels are its bus callbacks, the INT and NMI inputs and the acknowledge callback; its sync points are instruction boundaries (interrupts are sampled there, PUM 5) and every I/O access. **Instructions per second.** 3.072 MHz CPU clock. MAME's boot-ROM frame: 7,148 instructions in 40,703 clocks, one of them a 24,579-clock `rep`; the other 7,147 took 16,124 clocks, **2.26 clocks per instruction**, so typical code needs about 3,072,000 / 2.26 = **1.36 M instructions/s**, and the ceiling (every instruction 1 clock) is **3.07 M/s**. Real time also needs the board: 224 x 144 x 75.47 = 2.43 M pixels/s and 24,000 audio samples/s. **The no-JIT path.** | Target | Verdict | Why | | --- | --- | --- | | Desktop, Python | slower than real time on PyPy (expected) | a readable per-instruction Python core runs well under 1.36 M instr/s; reference boards are correct, not fast (Aubrey) | | Desktop, Rust (`-rust`, then `-fast`) | real time with large margin | 1.4-3 M simple instructions/s is a light load for a compiled interpreter; to be measured | | Phone, Rust `-fast` | real time expected with margin for run-ahead 1-2 frames | same arithmetic; a cached interpreter is not expected to be needed | | Browser, wasm `-fast` | real time expected | as the phone | So `recompile: none` is the stated capability, and nothing in this CPU calls for block caching. Threads gain nothing inside the SoC domain. ## Open decisions for Aubrey 1. **Own repository, not an i8086-python variant.** Recommendation: keep `v30mz-python` separate (NEC's PUM 1.3: no V-series extensions, no 8080 mode, its own pipeline and clock table; i8086-python has no code to vary). Share conventions only; revisit a shared 80186 decode table only if both cores exist and the duplication hurts. 2. **Machine rows in the core.** Recommendation: `soc="aswan" | "sphinx"` on the core for the MULU ZF difference (and any later one), chosen by the board's machine table. 3. **Ask FluBBa for WSTimingTest numbers, or for a licence on WSCpuTest/WSTimingTest.** Both are unlicensed. Recommendation: Aubrey writes to him (outside contact is his call); until then the tests are run and their README facts restated, never copied. 4. **MAME's `v30mz.cpp` is BSD-3-Clause by its header.** This batch's brief holds MAME closed. Recommendation: keep it closed for the core (it fails WSCpuTest, so it would teach wrong flags); decide per file later if a question only it can answer comes up. 5. **The prefetch-queue model (M5).** Recommendation: build it only when a board test fails without it, and record the decision in not-kept terms if it does not pay.