# Handoff: build `segasys1-python`, Sega's System 1 and System 2 boards ## 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 [start-here.md](start-here.md). Boards to model this one on: **sms-python** (a Z80 + SN76489 board in lockstep with MAME, the SG-1000 being this board's console cousin), **system16-python** (Phase 0: a Sega arcade board, MAME romsets by environment variable, the same MC-8123 and 8751 questions). Cores it embeds, read and not touched by this phase: **z80-python** (twice), **sn76489-python** (twice), later **mcs51-python**. Read before writing code: [boards.md](boards.md), [protection.md](protection.md), [sound.md](sound.md), [video.md](video.md), [cpu-cores.md](cpu-cores.md), [domains-and-speed.md](domains-and-speed.md), [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` (3 files, about 18 MB; `--verify` to check). ## Scope System 1 (the census's `ppi-small`, `pio-small`, `pio-big` groups) and System 2 (`ppi-big`), plain and decrypted sets first; Sega's opcode decryption (two classes), the MC-8123 and the 8751 as later milestones. ## Decided by Aubrey (standing rules that apply here) - Name `segasys1-python`, package `segasys1_python`. Local, no remote, until he says otherwise. - Romsets through `MAME_ROMPATH` (non-merged sets), never a machine path; no ROM, MCU program or key committed; game parts identified by name and SHA-1 from MAME's lists. - MIT; nothing copied or transcribed from GPL/LGPL code; MAME and the MiSTer HDL are closed boxes (MAME files' `license:` lines only). - 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. ## Package and classes (proposed) ``` segasys1_python/ machine.py # machine rows: group (ppi-small, pio-small, pio-big, ppi-big), clocks, fetch filter, MCU romset.py # load a MAME set from MAME_ROMPATH by machine name; CRC/SHA-1 checks; region layout console.py # build(machine, rompath=...) -> System1 bus.py # the main CPU's map: ROM (and System 2's bank), work RAM, video RAMs, open bus io.py # inputs, DIPs; Z80Pio (UMPIO) or Ppi8255 per row; the video-control byte video.py # timing (640 x 260 at 10 MHz), interrupts, the mixer tiles.py # tile layers, scroll, banks sprites.py # sprite list, line buffers, collision palette.py # palette RAM or the colour PROMs; the lookup PROM sound.py # SoundBoard: the Z80 (z80-python), the latch, the timer interrupt, two SN76489A, READY waits fetch.py # FetchFilter socket: identity, SegaClassA(part), SegaClassB(part), Mc8123(key) mcu.py # Mcu8751 socket (mcs51-python): bus-master window, the hold, its ports sockets.py # the socket Protocols and their test-only fakes savestate.py # whole-machine states naming the set by machine name and ROM hashes ``` ## The import boundary The **cores** own instruction semantics and bus timing: the Z80s (M1 cycles through `read_m1`, NMI/INT, IM 1, port cycles), the SN76489A (tone, noise, READY's busy time), the MCS-51 (as the 8751). The **board** owns every decode, latch, bank, wait state, the PIO and 8255, the video hardware, the fetch filters and the MCU's wiring. ## Sockets | Socket | Fills it | Empty or fake | | --- | --- | --- | | `FetchFilter` (main Z80 reads, told M1 from data) | Sega class A or B with the part's data; MC-8123 with the set's key ROM | identity (plain and decrypted sets) | | `IoChip` | `Z80Pio` or `Ppi8255` by row | none: one is always fitted | | `McuSocket` | mcs51-python running the set's 8751 program | empty: the board runs as the unprotected siblings do | | `PsgSocket` x2 | sn76489-python `SN76489AN` at the row's clocks | a silent fake that still reports READY's busy time | ## The gate (defined at milestone 1, before any socket is filled) Unit tests of the documented chip facts; the main Z80 lockstep against `scripts/mame_trace.py --ports` on a plain set, `0 unexplained` over a fixed number of frames; later the sound Z80 lockstep and the pictures ([validation.md](validation.md)). With every socket empty or fake, filling a socket must not move these. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **Main CPU on `wboyu`** (plain, PIO): the map, ROM, work RAM, video RAMs as plain memory, the PIO, inputs and DIPs, the vblank interrupt (IM 1) | UMPIO (2); MAME (5) as detector | `wboyu`'s main-CPU stream and port accesses equal MAME's for 60 frames (390,796 instructions in MAME's run) or each difference has a named reason | | M2 | **Sound**: the sound Z80, the latch and its NMI, the timer interrupt, two SN76489A, READY waits (needs decision 6's wait path) | sn76489-python's ladder; MAME (5) | sound Z80 stream equals MAME's for 60 frames (376,097 instructions, 237 timer interrupts, 1 NMI) | | M3 | **Pictures on `wboyu`**: tiles, sprites, palette RAM, the lookup PROM, collision, the video-control byte | MAME snapshots (5); facts per decision 2 | attract frames against `--snap`, every pixel difference named | | M4 | **The PPI boards**: `upndownu`, `regulusu`, `starjack` (2.5 MHz sound, 1.25 + 2.5 MHz PSGs) | as M1-M3 | lockstep and pictures | | M5 | **System 2**: `wbmld`, `chopliftu`, `wboysys2`'s plain relatives; 96 KB program with its bank, 96 KB tiles, colour PROMs | as M1-M3 | lockstep and pictures; U9 answered for `chopliftu` | | M6 | **Class A decryption** (decision 3; `read_m1` on z80-python `main`) | MAME's two views (5) | each class-A set's trace equals its plain sibling's where one exists (`flicky`/`flickys2`, `pitfall2`/`pitfall2u`, `regulus`/`regulusu`, `seganinj`/`seganinju`, `myherobl`/`myhero`); the class-A-only titles boot | | M7 | **Class B and the MC-8123** | MAME (5) | `wboy`/`wboyu`, `wbml`/`wbmld`, `ufosensi`/`ufosensib`; `gardia`, `4dwarrio`, `dakkochn` boot | | M8 | **The 8751** (mcs51-python pinned) | MCS51 (2); MAME traces (5) | `choplift`, `nob`, `shtngmst` boot; the 8751's stream against MAME's for 3 frames (19,503 instructions on `choplift`) | | M9 | **Smoke list and states** | the gate | a state saved mid-game loads into a fresh machine and continues bit-identically | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | which games are System 1 and which System 2; whether MAME's machine configurations match Sega's board revisions (Brain; `gardia` vs `gardiaj`) | MAME's grouping (5); board documents set aside | the census's groups as rows, HYPOTHESIS | | U2 | the PIO's and 8255's wiring: register order by address, which port carries the sound command and the video control | MAME trace (5): ports 0x18-0x1B, 0x18 once, 0x19 each frame | MAME's observed use, HYPOTHESIS | | U3 | the crystal and a measured frame rate | MAME: 4 MHz CPU, 10 MHz dots, a 20 MHz PIO clock | 20 MHz master, HYPOTHESIS | | U4 | where the sound Z80 reads the latch; READY's wiring to /WAIT | MAME's `sn_ready` merger (5) | READY to /WAIT on both PSGs, HYPOTHESIS | | U5 | the rest of the main map: System 2's bank register, 0xF000-0xFFFF, open bus | MAME trace (5) | log unknown accesses | | U6 | the decryption on real parts: M1 only, or other pins; interrupt-acknowledge cycles; where MAME's tables come from | MAME (5) | decision 3 | | U7 | the 8751's wiring and duties after the ROM check (BUSREQ/WAIT/RESET, port 1) | MAME trace (5) | M8 | | U8 | the sound timer interrupt's source and period (16,384 cycles, some intervals about 1,025 longer) | MAME (5) | MAME's period, HYPOTHESIS | | U9 | why `choplift` and `chopliftu` stay on "IC CHECK" for 60 s under this recipe | not investigated | before M5 | | U10 | 256 or 512 pixels a line | MAME draws 512 | 256 doubled, HYPOTHESIS | | U11 | tile layers, scroll registers, System 2's extra layers and banks | none | decision 2 | | U12 | CPU waits on video RAM accesses (contention) | none | zero waits provisional, FITTED later against cycle counts | | U13 | sprite format, per-line limits, collision detection | none | decision 2 | | U14 | priority (the lookup PROM), palette format, DAC levels | none | decision 2 | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | the PSG part | the task brief: SN76496; MAME's device: SN76489A | MAME's name provisionally (both are sn76489-python's discrete row); a board photo would settle it | | C2 | System 2 membership | MAME names only `wboysys2` "system 2"; the census puts all 8751 and MC-8123 System-2-looking sets in `ppi-big` | the census's groups as rows; names follow a document when one is in hand | | C3 | part numbers | MAME's descriptions say 315-5115 (`teddybb`), 315-5113 (`seganinja`), 315-5030 (`upndown`); MAME's devices are 315-5155, 315-5133, 315-5098 | the device (it is what MAME runs); note both | ## Domains and speed - **Domains**: M (main Z80 or its protected form, ROM and bank, RAM, PIO or 8255, the 8751 as a second bus master), V (tiles, sprites, palette, collision; may be stepped inside M), S (sound Z80, two SN76489A with READY, the timer). **Channels**: the sound latch and its NMI, the video RAMs and collision reads, the video-control byte, the vblank interrupt, audio out. Detail: [domains-and-speed.md](domains-and-speed.md). - **Instructions a second**: main Z80 4 MHz / 10.2 cycles (OBSERVED) = 0.39 M; sound Z80 4 MHz / 10.6 = 0.38 M; 8751 0.667 M machine cycles / 1.71 = 0.39 M on three sets. **0.77 M CPU instructions a second** (1.16 M with the 8751), plus 0.375 M PSG ticks and 10 M dots (640 x 260 x 60.096). - **The no-JIT path**: the Python specification is near or below real time on PyPy with per-dot video (HYPOTHESIS until measured). **segasys1-rust reaches real time on desktop, phone and browser without -fast**: z80-rust's 50 M instructions a second is over 60 times the need, and z80-wasm already runs a Z80 arcade board in browsers. `-fast` buys run-ahead headroom; no cached interpreter, no threads; `recompile: none` everywhere, the iPhone included. ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | Z80 (and the Sega parts and MC-8123 as a Z80 with a filter) | many | z80-python; the filters in the board | | SN76489A | sms-python (in VDP form), many | sn76489-python | | 8751 (MCS-51) | system16-python (16A, 16B, 18), this board's System 2, CPS-2's `gigaman2` | new `mcs51-python` (a CPU; already proposed) | | Z80 PIO | this board; Model 2's sound board (model2-python's notes) | in the board | | 8255 PPI | this board, system16-python (16A), ibmpc-python, msx-python | in the board; extract when a second board builds one (decision 9) | | tile, sprite, collision hardware | this board only | in the board | ## Open decisions for Aubrey (each with a recommendation) 1. **Charles MacDonald's notes and their host's robots.txt.** dreamjam.co.uk's robots.txt disallows `/emuviews/` (his notes; techno-junk.org frames it) to every crawler. I did not fetch them. *Recommend*: treat it as "set aside (site's wishes)" for agents, as the brief's rule reads; you fetch what you want yourself (for this board, any System 1 notes if they exist; for the sibling, s24tech.txt and s32tech.txt) and the repositories pin them by SHA-256 with "fetched by Aubrey"; and decide whether the earlier agent fetches in system16-python, saturn-python and megadrive-python stand. 2. **MAME's BSD-3 System 1 files for facts.** No tier 1-3 source exists for the video or the maps. *Recommend*: allow reading `system1.cpp`, `system1_v.cpp` and `system1.h` for facts (never transcribed), notice in NOTICE.md, as pcenginecd-python did with MAME's BSD-3 `msm5205.cpp`; otherwise the board is built against MAME's pictures alone, slowly. 3. **The decryption.** *Recommend*: plain sets first (M1-M5); then class A, class B and the MC-8123 from MAME's BSD-3 `segacrpt_device.cpp`, `segacrp2_device.cpp` and `mc8123.cpp` read for their tables (data), credited, files holding them `MIT AND BSD-3-Clause`. The alternative, measuring MAME's two views closed-box (the method is in `mame_trace.py --views`), keeps MAME's source unopened but yields the same tables with the same provenance; I recommend the honest citation over the detour. 4. **One board, machine rows.** *Recommend* one `segasys1-python` with rows for the four groups, not separate System 1 and System 2 repositories: the CPUs, sound and (by MAME's one driver file) most of the video are shared. 5. **First machine.** *Recommend* `wboyu` (plain, PIO, MAME trace in hand), then `upndownu` (PPI), then `wbmld` (System 2). 6. **z80-python asks.** *Recommend* raising two items on z80-python: merge `read_m1` to `main` (59 of 92 machines need it; system16-python and the ColecoVision track wait too), and a way for a board to add wait states to an access (the PSGs' READY; the SC-3000 has the same wiring). Neither changes z80-python's behaviour for existing boards. 7. **mcs51-python.** *Recommend* its Phase 0 as system16-python proposed, with this board's three System 2 sets added to its users; this board runs with the MCU socket empty until then (every 8751 title but Shooting Master has an unprotected sibling). 8. **Speed claim.** *Recommend* no -fast fork until a phone measurement asks for one; the plain Rust transcription has 60 times the CPU headroom. 9. **8255 extraction.** *Recommend* deciding when the second family board builds an 8255 (system16-python's 16A or ibmpc-python); keep it pin-shaped in the board here. 10. **sources.md rows**: the six proposed in [sources.md](sources.md) (MacDonald's host and system16.com set aside for the site's wishes; MAME's System 1 files with their `license:` lines; `ym2151.txt` without one; Zilog's peripherals manual; the MiSTer core).