# Handoff: build `system16-python`, the Sega System 16 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 [start-here.md](start-here.md). Boards to model this one on: **megadrive-python** (a 68000 + Z80 board with per-dot video and an FM chip; its frame rates are this board's speed precedent), **cps1-python** (Phase 0: a 68000 arcade board, MAME romsets by environment variable, the YM2151's first consumer) and **sms-python**/**pcengine-python** for board structure. Cores it embeds, read and not touched by this phase: **m68000-python**, **z80-python**, and later **ym2151-python**, **mcs48-python** and a new **mcs51-python**. Read before writing code: [boards.md](boards.md), [memory-map.md](memory-map.md), [video.md](video.md), [sound.md](sound.md), [protection.md](protection.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), [system18-outrun.md](system18-outrun.md). Then `python -I scripts/fetch_references.py` (17 files, about 50 MB; `--verify` to check); text versions of every PDF land in `reference/text/`. ## Scope System 16A (with the Pre-System 16 sets MAME drives from the same file) and System 16B, plain 68000 sets and MAME's decrypted sets first; the 8751 MCU, the FD1089/FD1094 and MC-8123 models, System 18 and the bootleg boards as later milestones; Out Run as a separate board. ## Decided by Aubrey (standing rules that apply here) - Name `system16-python`, package `system16_python` (the x-python naming rule). 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; firmware identified by name and SHA-1 from MAME's lists. - MIT; nothing copied or transcribed from GPL/LGPL code; MAME and jotego's 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) ``` system16_python/ machine.py # machine rows: board (pre16, 16a, 16b, later 18), ROM board, MCU, CPU filter, sample chip romset.py # load a MAME set from MAME_ROMPATH by machine name; CRC/SHA-1 checks; region layout console.py # build(machine, rompath=...) -> System16 bus16a.py # the fixed decode (CMP16), lockups, open bus mapper.py # Mapper5195: 32 registers, regions, sizes, mirroring, halt/reset/sound-latch strobes romboard.py # RomBoard5358 / 5704 / 5797: program regions, sprite/tile/sample banking, PLS153, math chips math5248.py # Multiplier5248 (and Comparator5250 when U5 is known) io.py # 16B I/O area and misc control; Ppi8255 for 16A; inputs and DIPs video.py # timing (262 lines, line-223 interrupt), the colour RAM bus, the mixer tiles.py # Tilemap5197: pages, scroll, row/column scroll, alternate layers, text sprites.py # Sprites5196 (16B, zoom, write-back) and SpritesPre16 (16A format) sound.py # SoundBoard: the Z80 (z80-python), latches, decode (315-5214), the FM socket upd7759.py # Upd7759: slave mode, /DRQ, /BUSY, the decoder (decision 3) sample7751.py # the 16A sample path: mcs48-python as uPD7751, the 8243, the DAC, CT wiring mcu.py # Mcu8751 socket: bus-master access, /HALT, /IPL drive (mcs51-python) fd1094.py # later: the opcode-fetch filter socket (decision 4) 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 68000 (prefetch, open-bus word, function codes, interrupt acknowledge), the Z80 (NMI/INT, port cycles, `read_m1` when merged), the YM2151, the MCS-48 (as the uPD7751) and the MCS-51 (as the 8751). The **board** owns every decode, latch, mirror, lockup, bank and wait state, the video chips, the uPD7759, the 8255, the 8243, the math chips, and the protection filters. ## Sockets | Socket | Fills it | Empty or fake | | --- | --- | --- | | `RomBoard` | 171-5358, 171-5704 (171-5521), 171-5797; the 16A board's fixed ROMs | the machine row picks one | | `FmSocket` | ym2151-python once its rungs land | a silent fake that keeps the register file and the timer flags (16B polls them) | | `SampleSocket` | `Upd7759` (16B) or `Sample7751` (16A) | silence with /BUSY and /DRQ idle | | `McuSocket` | mcs51-python running the set's 8751 program | empty: the board runs as with the MCU removed (CM16B §9: no inputs or sound on Tough Turf) | | `FetchFilter` (68000 program reads) | FD1089A/FD1089B/FD1094 with the set's key | identity (plain and decrypted sets) | | `Z80FetchFilter` | MC-8123 with its key (needs `read_m1`) | identity | ## The gate (defined at milestone 1, before any socket is filled) Unit tests of the documented facts; the 68000 lockstep against `scripts/mame_trace.py` on a plain set, `0 unexplained` over a fixed number of frames; later the 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 | **16A bus and CPU**: the fixed decode with lockups and 0xFFFF banks, work RAM, I/O, 8255, DIPs and inputs, the video timing skeleton (262 x 400, level-4 interrupt), video RAMs as plain memory, the sound socket fake | CMP16, CM16B §4 (3); MAME (5) as detector | `shinobi`'s 68000 stream equals MAME's for 3 frames (42,574 instructions in MAME's run) or each difference has a named reason; the map facts as unit tests | | M2 | **16A pictures**: tiles, text, Pre-16/16A sprites, priority, palette, display enable, flip | CMP16, CM16B §5-§8 (3); MAME snapshots (5) | `shinobi` attract frames against `--snap` with every pixel difference triaged | | M3 | **16B**: the 315-5195, ROM boards and banking, the 315-5196 with zoom and write-back, the 315-5248, prefetch open bus, the 16B I/O area | CM16B §1-§3, CM5797 (3) | `altbeast`-family plain sets, `goldnaxe1d`, `eswatd`, `tetris` (171-5704) lockstep and pictures; Riot City's word writes to the mapper | | M4 | **16B sound**: the Z80, the 315-5214 decode, latches, the uPD7759 in slave mode (decoder per decision 3), the FM socket | CMSND, DS7759 (2-3); MAME audio (5) | Z80 lockstep with the sound command times; samples play; audio differences against MAME named | | M5 | **YM2151**: pin ym2151-python | its ladder | music on 16B sets; /IRQ unconnected; timer polling works | | M6 | **16A sound**: mcs48-python as the uPD7751, the 8243, the DAC, the CT wiring | CMP16 (3), D7751 (2) | Shinobi's voices; the CT bit 7 -> /INT wiring as a unit test | | M7 | **The 8751**: mcs51-python pinned; bus mastering, /HALT, /IPL drive, the sound latch | CMP16, CM16B §9 (3), MCS51 (2); MAME 8751 traces (5) | `goldnaxe`, `altbeast`, `wrestwar` and `bodyslam` boot; the 8751 stream against MAME's for 3 frames | | M8 | **Smoke list and states**: the smoke list grown from failures; save states that continue identically | the gate | a state saved mid-game loads into a fresh machine and continues bit-identically | | M9 | **Protection models** (by decision 4): FD1089A/B, FD1094 behind `FetchFilter`; MC-8123 once `read_m1` is on z80-python's main | AHFD, CM16B §9 (3); MAME (5) | each encrypted set's trace equals its decrypted sibling's (the MAME observation turned into a gate); the five titles with no plain set boot | | M10 | **System 18 row** (by decision 6) | CM18 (3) | `shdancer` boots | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | the 16B Z80's clock | MAME 5 MHz; CMSND: not traced | MAME's value, tagged HYPOTHESIS | | U2 | the 315-5195's DTACK/wait states per region | only JT16B, citing an unattributed gate trace (set aside) | zero waits provisional, FITTED later against lockstep cycle counts | | U3 | 16A wait states (2 for object RAM, palette, I/O) | JT16, "derived from the schematics", unchecked on a PCB (tier 4) | adopt as HYPOTHESIS; check against MAME cycle counts | | U4 | whether 16A has a zoom ROM | CMP16 asks | none modelled | | U5 | the 315-5250 comparator | CM16B: 24 word registers, untested | stub; log accesses | | U6 | on 8751 sets, who programs the mapper | MAME: the 68000 never writes it in 40 frames (OBSERVED) | settle at M7 | | U7 | sprite word 5 bits 15-10 written back | CM16B: depends on zoom, start, pitch, lines; meaning unknown | detectors | | U8 | sprites a line and overflow behaviour | CMP16: ~800 render ticks a line | FITTED | | U9 | shadow and highlight levels | none (JT16 assumes 25%) | provisional, one constant | | U10 | whether the Z80's latch read clears /INT | CMSND: "likely" | clear on read, HYPOTHESIS | | U11 | the uPD7759 decoder and data stream | NEC gives none; MAME `upd7759.cpp` (BSD-3, unopened) | blocks M4's samples: decision 3 | | U12 | FD1089/FD1094 algorithm and state rules | MAME `fd1094.cpp`/`fd1089.cpp` (BSD-3, unopened) | decrypted sets meanwhile (decision 4) | | U13 | the interrupt's dot within line 223; whether it ever drops unacknowledged | CM16B §4 | line start; held until IACK | | U14 | power-on contents of RAMs and the PLS153 (7 by CM16B), the mapper's registers | partial | zeros except where documented | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | the Pre-16 8751's clock | CMP16: 8 MHz with a divider, runs at 4 MHz; MAME: 8 MHz | follow CMP16 for Pre-16 rows (a board observation), MAME's 8 MHz for 16B/18 until evidence; tag both | | C2 | frame rate | 60.054 Hz from 6.2937 MHz / (400 x 262) (MAME's figure); 60.042 Hz MEASURED by jotego | keep the clock arithmetic as the model (the crystal's tolerance can explain 0.02%); record the measurement; revisit if a second measurement agrees | | C3 | the sound command's interrupt | CM16B §2 (2001-2003): NMI; CMSND (2003, the sound-specific note): /INT, read at port 0xC0; 16A uses /NMI via the PPI (JT16) | CMSND for 16B | | C4 | which YM2151 register bit drives pin 8 | Yamaha's manual: D6 -> CT1 (pin 8); IKAOPM's die trace and MacDonald's Body Slam board: bit 7 -> pin 8 | die and board (ym2151-python already records the split) | | C5 | the vblank interrupt's acknowledge | CM16B: "automatically acknowledged by the video hardware", yet taken late if masked; MAME: held until the CPU acknowledges (m68000-python timing.md) | held until IACK (consistent with both readings); U13 | ## Domains and speed - **Domains**: C (68000 or FD1094, 315-5195 or the 16A decode, ROM, work RAM, I/O, the 8751 as a second bus master), V (315-5196/5197, line buffers, video RAMs, mixer), S (Z80, YM2151, uPD7759 or uPD7751 + 8243 + DAC). **Channels**: the sound command latch (16B mapper register 0x07 -> Z80 /INT; 16A 8255 port A with /OBF -> /NMI and /ACK back), video RAM accesses, colour RAM during display, the control bits, the vblank interrupt, the 16A mute bit, audio out. Detail: [domains-and-speed.md](domains-and-speed.md). - **Instructions a second**: 68000 10 MHz / 12 cycles (11.7-12.2 OBSERVED in MAME) = 0.83 M; Z80 5 MHz / 12.0 = 0.42 M; 8751 8 MHz / 12 / 1.29 = 0.52 M; uPD7751 up to 0.4 M. A 16B board with an MCU is **1.8 M CPU instructions a second**, plus 2.0 M YM2151 operator steps (62.5 kHz x 32) and 6.29 M dots (400 x 262 x 60.05). A 10 MHz 68000 is easy: one million instructions a second, which m68000-python on PyPy exceeds 22-40 times. - **The no-JIT path**: the Python specification is CPU-light but per-dot video and per-clock FM make it likely below real time with FM (megadrive-python's 16-21 fps precedent: HYPOTHESIS until measured). **system16-rust reaches real time on desktop, phone and browser without -fast**: m68000-rust runs 134 M instructions a second natively and about 112 M as WebAssembly, over 60 times the CPU need; a phone at a tenth of that still has six times. `-fast` buys run-ahead headroom only; no cached interpreter, no threads, `recompile: none` everywhere, the iPhone included. ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | 68000 (and FD1089/FD1094 as a 68000 with a filter) | many | m68000-python; the filter in the board | | Z80 (and MC-8123) | many | z80-python; the filter in the board | | YM2151 | cps1-python, this board | ym2151-python (being built) | | uPD7751 (MCS-48) | this board | mcs48-python (a CPU) | | 8751 (MCS-51) | this board (16A, 16B, 18), CPS-2's `gigaman2` | new `mcs51-python` (a CPU) | | uPD7759 | this board | in the board; extract on a second family consumer | | 315-5195, 315-5196, 315-5197, 315-5248/5250, 8255, 8243 | this board (Out Run, X, Y, System 18 use relatives) | in the board, pin-shaped; Out Run would be the second consumer of the 315-5195 and the tilemap family | | System 18's YM3438, RF5C68, 315-5313, PSG | ym2612-python, segacd-python (in board), megadrive-python (in board), sn76489-python | decision 6 | ## Open decisions for Aubrey (each with a recommendation) 1. **First machine.** Recommend System 16A plain sets for M1-M2 (`shinobi`: the fixed decode is the simplest bus, and MAME's trace is in hand), then 16B (143 of the 297 machines, and MacDonald's fullest notes); 16B's sound (uPD7759, in the board) before 16A's (which waits on mcs48-python). 2. **One board, machine rows.** Recommend one `system16-python` with rows `pre16`, `16a`, `16b` and later `18`, not separate repositories: the 68000 side, the tile/text/priority model and the sound core are shared, and System 18 is "quite similar" (CM18). 3. **MAME's BSD-3 System 16 files.** The brief keeps MAME closed; these files' `license:` lines say BSD-3-Clause (and `nl_segas16b.cpp` CC0-1.0). For the uPD7759 decoder (U11) no other description exists. Recommend: allow reading `upd7759.cpp` for facts, with the notice kept in NOTICE.md, as pcenginecd-python did for MAME's BSD-3 `msm5205.cpp`; allow `nl_segas16b.cpp` (CC0) for the output stage; keep `fd1094.cpp`/`fd1089.cpp`/`mc8123.cpp` closed until decision 4's milestone and decide then. 4. **Decrypted sets or a decryption model.** Recommend MAME's decrypted sets first (58 of 63 titles run on plain-68000 sets; MAME's FD1094 and decrypted Golden Axe traces are identical), the FD1089/FD1094 and MC-8123 filters as M9, so the original sets and the five titles with no plain set (`aceattac`, `dunkshot`, `pontoon`, `sjryuko`, `suprleag`) come later. 5. **mcs51-python.** Recommend a Phase 0 for a new `mcs51-python` core (Intel's MCS-51 manual pinned here as its tier-2 judge), started before M7; the board runs with the MCU socket empty until then (every MCU group but Body Slam has a set that runs without the MCU). 6. **System 18's shared chips.** Recommend deciding at M10: System 18 is the second family user of the 315-5313 VDP (megadrive-python, in-board) and of the RF5C68 (segacd-python's RF5C164, in-board), so the chip rule would extract both into cores with history; the YM3438s pin ym2612-python. 7. **Out Run.** Recommend a separate `outrun-python` board, later, that pins `system16-python` for the 315-5195, tiles and the sound core (the add-on shape) and adds the second 68000, the road generator and the Sega PCM. 8. **ym2151-python dependency.** Recommend building M1-M4 against a silent `FmSocket` and pinning ym2151-python when its rungs land; raise now on ym2151-python: 16B leaves /IRQ unconnected (timers polled), and MacDonald's Body Slam board observation supports IKAOPM's bit 7 -> pin 8 against the manual (C4). 9. **The unattributed 315-5195 gate trace** in jtcores. Recommend leaving it set aside; if you want it, the lineage question goes to jotego (an outside post: your call). Nothing here depends on it. 10. **z80-python's `read_m1`** (on branch `m1-fetch-hook-rebased`): needed only for MC-8123 sets (6 machines, each with a plain sibling). Recommend no action now; it lands with the ColecoVision track. 11. **sources.md rows**: the seven proposed in [sources.md](sources.md) (MacDonald's System 16/18 notes; NEC and Intel documents; ArcadeHacker; MAME's System 16 files with their `license:` lines; the unattributed trace; the jotego README refinement, including that jtcores' `s16b.txt` re-hosts MacDonald's notes).