# Handoff: build `saturn-python`, the Sega Saturn board ## Context you are inheriting The family's shape: chips are cores, boards compose cores; CPUs always get their own core; a non-CPU chip used by one machine stays in its board and is extracted only when a second family project needs it; never copy a file between repositories, pin the sibling by commit. Read the charter first (emulator-lineage `docs/charter.md`), then this repository's [start-here.md](start-here.md). The Saturn is era 3 in the charter's table: tightly coupled 32-bit CPUs, transaction level appears in the specification, Python too slow for long runs, threaded `-fast` needed, turbo likely. Boards to model this one on: **sega32x-python** (two sh2-python cores on one bus, boot ROMs by SHA-1, save states naming ROMs by hash), **pcengine-python** and **pcenginecd-python** (a CD add-on, a CD BIOS as a program to run, CHD/bin-cue), **segacd-python** (a CD drive model). Cores it embeds, read and not touched by this phase: **sh2-python** (pin its current main; sh2-rust exists, sh2-rust-fast is being built tonight by another agent) and **m68000-python**. Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md), [memory-map.md](memory-map.md). 2. The chips: [scu.md](scu.md), [vdp2.md](vdp2.md), [vdp1.md](vdp1.md), [smpc.md](smpc.md), [scsp.md](scsp.md), [cdblock.md](cdblock.md), [cartridge-bios-region.md](cartridge-bios-region.md). 3. [cpu-cores.md](cpu-cores.md), [domains-and-speed.md](domains-and-speed.md). 4. [sources.md](sources.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md), [census.md](census.md). Then `python -I scripts/fetch_references.py` (25 files, about 60 MB, and one repository; `--verify` to check). Text versions of every PDF land in `reference/text/` for grepping. ## Decided by Aubrey (standing rules that apply here) - Name `saturn-python`, package `saturn_python` (the x-python naming rule). - Local, no remote, until he says otherwise. Bring your own ROMs and discs; nothing of Sega's in the repository. - Python floor = PyPy's (3.11), PyPy must work; modern over compat; reference boards are correct, not fast; sound may be less exact than pictures; smoke lists before sweeps. - Technical choices are made, marked provisional and reported; policy waits for him. ## Proposed by this Phase 0 - **NTSC US machine first** (`saturn-us`, Overseas v1.01a), with `saturn-jp` and `saturn-eu` as machine-table rows from milestone 1 (area code, PAL bit, PLL figures). - **One timeline in a common unit** with three unrelated clocks (PLL, SCSP, CD block) converted at the channels; the domains and channels of [domains-and-speed.md](domains-and-speed.md) are first-class structure from milestone 1. - **The two SH-2s interleaved at bus-transaction level** in one order of record (earlier bus time first, master first on a tie), one domain. - **The CD block high-level first** (open decision 3), behind a `CdBlockPort` socket at the A-bus CS2 register level. - **Boot ROMs and CD block ROMs identified by SHA-1** (census.md lists them), never by file name (a BIOS pack's `saturn_bios.bin` turned out to be Japan v1.00). ## Package and classes (proposed) ``` saturn_python/ machine.py # machine rows: area code, video standard, PLL figures, boot ROM SHA-1s console.py # build(machine, disc=None, cartridge=None, bios_dir=...) -> Saturn bus.py # the SCU's three buses, the memory map, open-bus values, master/slave order scu.py # Scu: registers, DMA levels 0-2 and indirect mode, interrupts, timers, # A-bus control; the DSP's ports (the DSP itself: open decision 4) smpc.py # Smpc: commands, INTBACK, RTC, SMEM, ports, reset and clock lines vdp1.py # Vdp1: command list, rasteriser, frame buffers, erase/change vdp2.py # Vdp2: timing, cycle patterns, NBG/RBG, priority, colour calc, output scsp.py # Scsp: slots, EG, LFO, mixer, DSP, timers, MIDI stub, sound RAM port sound.py # SoundDomain: the 68EC000 (m68000-python) + Scsp + sound RAM cdblock_hle.py # CdBlockHle: CDCI commands, periodic response, selectors, data transfer disc.py # bin/cue (and CHD) sector source, IP.BIN reader cartridge.py # CartridgeSlot devices: backup RAM, extended RAM, ROM carts sockets.py # the socket Protocols and their test-only fakes savestate.py # whole-machine states naming ROMs and discs by hash ``` ## What the board needs from its cores (the embedding contracts) From **sh2-python** (two instances, master and slave mode, as sega32x-python already uses them): 1. Bus callbacks with address, width, kind (fetch, data, DMA) and the board's wait states (it exists). 2. IRL inputs driven by the board and external vector fetch for IRL interrupts (ICR VECMD, its intc module: exists); the SCU supplies the vector. 3. **FRT input capture on FTI**: the Saturn uses it for MINIT/SINIT (DUAL 1.2), and sh2-python's `frt.py` says input capture on FTI "is not modelled yet (rung 5)". This is the first request to raise upstream, with a repro (a write to SINIT must set the slave's ICF and capture FRC). 4. Master/slave bus arbitration between two chips (its BSC module lists it; the Saturn's slave shares every device, DUAL 1.1). 5. A reset input the board can hold (SSHOFF/SSHON, and the slave reset on a clock change: DUAL 3.0) and NMI (NMIREQ). 6. A clock it can change at run time (CKCHG352/320): the core counts states; the board converts at the new rate. From **m68000-python**: bus callbacks per cycle with the SCSP's waits, autovectored interrupts on levels 1-7 driven by the SCSP, and a reset line (SNDON/SNDOFF). Nothing new is expected; confirm against its README when wiring. From a future **scudsp-python** (if decision 4 says so): a step function, its program and data RAM ports, a DMA request callback, flags readable by the SCU's program control port, the end interrupt. ## The gate (defined at milestone 1, before any add-on) The board's lockstep cases (the boot ROM's instruction stream against MAME's master SH-2 trace, `0 unexplained` over a fixed number of frames), its unit tests of the documented facts, and later its picture rows against the detectors. With nothing in the cartridge slot and the CD block port empty, the base board must not move these when an add-on lands. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **Buses, both SH-2s, SCU registers and interrupts (no DMA), SMPC commands, a VDP2 timing skeleton (TVSTAT, blanking, line counts, the interrupts), VDP1 and SCSP register files, the sockets and their fakes** | SCU, SMPC, CMH (2-3); MAME trace (5) as detector | the boot ROM runs from reset through its SMPC and SCU setup with the master's instruction stream equal to MAME's for 3 frames or a named reason for each difference; CMH's map facts (mirrors, open bus, CRAM write mirroring) as unit tests | | M2 | **VDP2 pictures**: NBG cell and bitmap, CRAM modes, cycle patterns as documented, priority, colour calculation, back and line screens | VDP2 (2), CMH (3); MAME and Mednafen pictures (5) | the boot ROM's language and system screens drawn; every pixel difference with the detectors triaged; forbidden cycle patterns logged | | M3 | **VDP1 and SCU DMA**: command list, colour paths, frame-buffer change modes, erase; DMA levels and indirect mode | VDP1, SCU, SCUF (2); StrikerX3 scu-dma hardware rows (3) | the boot ROM's logo animation; the four scu-dma hardware rows reproduced exactly; VDP1 timing constants in one table tagged FITTED | | M4 | **Sound**: the 68EC000, SCSP slots, EG, LFO, mixer, timers, interrupts; the DSP per decision 5 and U13 | SCSP (2); detectors' audio (5) | the boot ROM's sounds play; audio compared with detectors, differences named | | M5 | **The CD block (high-level)**: CDCI commands, HIRQ, periodic response, selectors, data transfer by CPU, SH-2 DMA and SCU DMA; bin/cue and CHD; the boot ROM's disc check | CDCI (2); detectors (5) | 5 games of a smoke list (problem games and well-behaved ones: smoke-gate rule) boot to their title screens; every CD timing constant tagged | | M6 | **SCU DSP** (pinned core or in-board) and the **smoke list** grown from failures | SCU ch. 4 (2) | the smoke list's games reach gameplay; DSP programs captured from them replay against MAME's scudsp | | M7 | **Cartridges and regions**: backup RAM, extended RAM 1/4 MB, the two ROM carts; JP and EU rows (PAL); save states | TB#47 (2) | KOF '95 with its cart boots; a save state loads into a fresh machine and continues identically | | M8 | **Low-level CD block** (only by decision 3): sh1-python pinned, gate array and drive modelled from observation | the firmware itself | the CD block ROM boots and serves the boot ROM's disc check in place of the high-level block | Full Redump sweeps only when the board might be ready (Aubrey's smoke-sweep rule), and the 16 incomplete zips (census.md) are refetched first. ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | SH7604 x 2 | 32X, Saturn, ST-V (CPS3 is an SH-2 too) | the existing core, sh2-python | | MC68EC000 | many | the existing core, m68000-python | | SH-1 (CD block) | Saturn only (ST-V has no CD block) | new core `sh1-python`, deferred (decision 3) | | SCU DSP | Saturn, ST-V | its own core `scudsp-python` (decision 4) | | SCU, VDP1, VDP2, SMPC | Saturn, ST-V (MAME's `stvbios` device list; the MiSTer core's ST-V build) | stay in the board | | SCSP (with its DSP) | Saturn, ST-V, Model 2A-2C (one), Model 3 (two) | stays in the board behind a pin-shaped class; extract on the second family consumer (decision 5) | | CD block HLE | Saturn | board (socket) | ## Unknowns (U) and conflicts (C) **Unknowns** (the best source available for each; none is settleable without hardware unless noted): | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | SH-2 bus waits per region and the master/slave arbitration cost on the Saturn | none (MAME approximates) | provisional, tag HYPOTHESIS | | U2 | Power-on contents of RAMs, VDP registers, frame buffers | CMH (VDP1 registers often 55AA, frame buffer 55555AAAAA) | follow CMH, option for random | | U3 | What the CPUs see during a PLL change (100 ms + a) | SMPC table 2.1 only | provisional | | U4 | The lockup regions (05900000, 05D80000): mechanism | CMH (observed lockups) | stall and log | | U5 | VDP1 drawing time per command and pixel | VDP1 p. 20-21 (ceilings only) | FITTED table | | U6 | VDP1's distorted-quad rasterisation walk | none open | detectors | | U7 | VDP2 behaviour under forbidden cycle patterns | none open | detectors; log every case | | U8 | VDP2 rotation arithmetic precision | register formats only | detectors | | U9 | SCU behaviour on forbidden uses (SCUF Nos. 01, 02, 04, 08) | none | detectors | | U10 | SCU DMA cycle cost per transfer, bus by bus | none | FITTED | | U11 | Which SCU interrupts reach the slave SH-2, and how | none in SCU or DUAL | detectors (MAME trace of the slave) | | U12 | SCU DSP overflow and carry corners | SCU ch. 4 | detectors; Celeriyacon's tests are private | | U13 | **SCSP DSP instruction encoding** | no permissive document | blocks the DSP; see scsp.md | | U14 | SCSP EG rates, attenuation and interpolation arithmetic | SCSP manual (meanings, curves) | good-enough rule | | U15 | Sound RAM wait states for the 68EC000 and the SCU | SCSP ch. 3 (priorities), TB#51 | provisional | | U16 | Port timing (the 3D pad's ~10 NOPs) | CMH | model if a game needs it | | U17 | CD block command, seek and transfer timing | CDCI gives only the periodic response | FITTED, one table | | U18 | The empty cartridge ID value; the backup RAM cartridge's layout | none read yet | read the backup library (SYSLIB) first | | U19 | The ROM cartridges' mapping (KOF '95, Ultraman) | none read yet | detectors | | U20 | Saturn-workload states per SH-2 instruction, the slave's duty, DSP duty cycles | none (domains-and-speed.md hypotheses) | measure when games run | **Conflicts** (sources disagree): | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | 320-mode PLL, NTSC | SMPC p. 3: 26.8741 MHz; CMH: 26.8465875 MHz (master x 2 minus 3.579545/2) | follow Sega's figure as a machine-table constant, keep CMH's in a comment; decide when a line-timing measurement appears | | C2 | the SH-2 clock at reset | SMPC: the PLL starts in 320 mode; MAME runs its SH-2s at 28.63 MHz | follow the SMPC manual | | C3 | the CD block's SH-1 clock | OVW tables 2.2, 3.1: 20 MHz; MAME: 16 MHz | 20 MHz (Sega) for the record; matters only for a low-level block | | C4 | disc area symbols | DISC 4.3 (1995): J, T, U, E; 36 discs in the Redump set also carry B, K, A or L | accept all eight; find the later standard revision | | C5 | which SH-1 | OVW draws a separate 64 KB ROM (an SH7032 with external ROM); MAME names the SH7032; a 64 KB on-chip ROM fits the SH7034 | settle before any low-level work; irrelevant to the high-level block | | C6 | A-bus registers in TB#47 | TB#47: 25FE0080h, 25FE0033h; SCU 3.6: ASR0 25FE00B0, AREF 25FE00B8 | the SCU manual; the bulletin's are typos (25FE0080 is the DSP port) | | C7 | sh2-python's speed on PyPy | sh2-python validation.md: 1.33-2.38 M instructions a second; sh2-rust status: the reference at "about 0.1 M" on PyPy | rerun on a quiet machine before planning test lengths | ## Open decisions for Aubrey (each with a recommendation) 1. **Scope and first machine.** Recommend the NTSC US console first (Overseas v1.01a), with JP and EU as machine rows from milestone 1; the Victor and Hitachi variants and Korea later as rows. 2. **ST-V.** It shares every chip but the CD block (MAME's device list) and adds ROM boards and arcade I/O. Recommend a separate board `stv-python` that pins `saturn-python` (the add-on shape: as sega32x-python pins megadrive-python), not a machine row, because its cartridge and I/O side is large and arcade-specific; not started now. 3. **CD block: high-level or an SH-1 core.** Recommend the high-level block from Sega's CDCI first, with every timing tagged and kept in one table, behind a socket at the CS2 register level; defer `sh1-python` (an SH-2-derived core pinning sh2-python's instruction core) until the board shows timing failures the high-level block cannot fix, because the low-level path's real cost is the undocumented gate array and drive, which would have to be reconstructed by observation (cdblock.md). 4. **The SCU DSP: own core or in the board.** Recommend its own core `scudsp-python`: the rule says CPUs get their own core, it has its own instruction stream and wants its own single-step gate, and the JIT proposal's generated-interpreter ideas apply to it. It is small; it would be built just before milestone 6. 5. **The SCSP.** Recommend it stays in `saturn-python` (with its DSP) behind a pin-shaped class until a second family board (ST-V, Model 2A, Model 3) needs it, then extract as `scsp-python` with history. 6. **The SMPC's MCU.** Recommend no core (firmware not dumped); a functional SMPC from its manual. Revisit if a dump appears. 7. **No high-level BIOS.** Recommend running only the player's own boot ROM, identified by SHA-1; a free replacement BIOS would be its own project if ever wanted. 8. **The SCSP DSP encoding gap (U13).** Recommend asking whether Aubrey knows of, or is willing to seek, a permissively licensed description (or the author of one) before the sound milestone; otherwise milestone 4 ships the slots without the DSP, marked. 9. **Upstream request to sh2-python**: FRT input capture on FTI, needed for MINIT/SINIT. Recommend raising it now so it lands before milestone 1 (a fix starts upstream). 10. **Pins of GPL source trees in sh2-python** (Kronos, Yabause, Ymir in its `pinned_repos.json`): recommend removing them, matching the 2026-11-24 removals elsewhere; this Phase 0 did not open them. 11. **sources.md row** (emulator-lineage, Aubrey's file for shape but facts may be added): recommend adding "Sega Saturn developer manuals: Sega confidential, facts only, cite document and page" and "Charles MacDonald, Sega Saturn hardware notes: facts only, credit required, no re-hosting", and "StrikerX3 saturn-tests: MIT".