# Handoff: build `deco16-python`, Data East's 16-bit arcade 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). These boards are era 2 in the charter's table: several CPUs, cycle-lockstep, `-fast` for headroom only. Boards to model this one on: **system16-python** (a 68000 arcade family as machine rows, an 8751, MAME traces as the detector, romsets by `MAME_ROMPATH`), **pcengine-python** (the HuC6280 with its on-die peripherals behind module boundaries), **cps1-python** and **irem-python** (arcade boards with MCUs and custom video). Cores it embeds, read and not touched by this phase: **m68000-python** (`55d3d60`), **huc6280-python** (`684ec86`), **w65c02-python** (`fca677f`), **6502-python** (`81e0eb6`), **z80-python**; later **mcs51-python**, an OPN and an OPL core, **ym2151-python**, **msm6295-python**. Read in order before writing code: 1. [README.md](../README.md), [start-here.md](start-here.md), [boards.md](boards.md), [memory-map.md](memory-map.md). 2. [video.md](video.md), [sound.md](sound.md), [protection.md](protection.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` (33 files, 54 MB; `--verify` to check). Text versions of the PDFs land in `reference/text/`; the Data East manuals are scans, read their `*_djvu.txt` OCR beside them. ## Decided by Aubrey (standing rules that apply here) - Name `deco16-python`, package `deco16_python`. Local, no remote, until he says otherwise. - Bring your own romsets (`MAME_ROMPATH`); nothing of Data East's in the repository; every program, MCU, PROM and PAL dump read in place from the player's set. - 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. - MAME is a closed box (run, never read); no GPL source fetched or kept here. - Technical choices are made, marked provisional and reported; policy waits for him. ## Proposed by this Phase 0 - **dec0 first**, and in it **Bad Dudes (`baddudes`) and Robocop (`robocop`)** as the first two machine rows: unencrypted, the two protection kinds (i8751; HuC6280 + MB8421), the 65C02 sound, BAC06 x3 + MXC06. Midnight Resistance and Sly Spy (HuC6280 sound, PAL fuse maps and a board trace) next. - **One repository for the 68000 generations** (karnov, dec0, the bac06 rows, deco16) as machine rows over a shared chip library inside the board; the ARM boards later as a sibling (decision 2). - **One timeline in 60 MHz ticks** for dec0 (the 20 and 12 MHz crystals divide into it exactly); per-board crystals from MAME's clocks for the deco16 rows until a board source says otherwise. - The **65C02 as w65c02-python's `R65C02` row** (MAME's choice), provisional until U5. - **Decode from the fuse maps**: reduce MRPAL and MAME's `plds` to equations in a tagged table before writing `bus.py`; JTSLY's written equations are the cross-check. ## Package and classes (proposed) ``` deco16_python/ machine.py # machine rows: family, CPUs and clocks, ROM regions by name and CRC, PROMs, PALs console.py # build(machine, rompath=...) -> Deco16Board timeline.py # the common tick, domain clocks, the order of record bus_dec0.py # the dec0 68000 map (from the PALs), DTACK waits, I/O and control at 30C0xx bac06.py # Bac06: register window, map RAM port, ROM port, colour out per dot mxc06.py # Mxc06: sprite RAM, buffer copy, line buffers, colour out mixer.py # the priority PROM and the palette DAC mb8421.py # Mb8421: 2 KB, two ports, BUSY, INTL/INTR mailboxes (FSRAM91) sound_dec0.py # SoundDomain: 65C02 or HuC6280, ROM/RAM, latch, YM2203/YM3812/MSM6295 sockets protection.py # Mcu8751Socket (mcs51-python), HuC6280SubSocket (+ Mb8421), SlySpyLogic deco55.py, deco52.py, deco104.py, deco146.py # generation 2, milestone M6 on sockets.py # the socket Protocols and their test-only fakes savestate.py # whole-machine states naming ROMs by CRC/SHA-1 ``` ## What the board needs from its cores (the embedding contracts) - **m68000-python**: per-cycle bus callbacks with the board's DTACK waits; autovectored IPL levels; a reset input. As system16-python uses it. - **huc6280-python**: the CPU as it is, plus the on-die timer, IRQ controller, I/O port and PSG, today in pcengine-python's `cpu_io` and `psg` packages (decision 4). A fix found here goes upstream with a repro. - **w65c02-python** (`R65C02` row) and **6502-python**: NMI from the sound latch, IRQ from the YM timers (U9). - **mcs51-python** (when built): the 8751 with 4 KB internal ROM, ports and interrupt pins as callbacks, 12 clocks a machine cycle. - **Sound chips** behind sockets with test fakes that answer status reads, so the sound CPU's program runs before the chip cores exist. ## Sockets | Socket | Filled by | Empty (fake) behaviour | | --- | --- | --- | | `OpnSocket` (YM2203) | the future OPN core | status register: not busy, timers per the written period (enough for the sound CPU's timing loops) | | `OplSocket` (YM3812/YM3526) | the future OPL core | as above | | `AdpcmSocket` (MSM6295) | msm6295-python | status: no voice busy | | `OpmSocket` (YM2151, deco16) | ym2151-python | not busy | | `ProtectionSocket` | i8751 / HuC6280 + MB8421 / Sly Spy logic / DECO 104 / 146 | none: a machine row without its protection does not boot, which is the point of the gate | ## The gate (defined at milestone 1, before any add-on row) [validation.md](validation.md): the 68000's instruction stream against MAME's (PC, SR, D0, SP, cycles) for N frames with a named reason for every difference; the second and third CPUs the same way; the documented facts as unit tests; seeded faults each caught. Rows added later must not move the earlier rows' gate. ## Milestones | # | Milestone | Judge (tier) | Exit | | --- | --- | --- | --- | | M1 | **dec0 bus and timing**: the PAL fuse maps reduced to a decode table (C2 named), the 68000 with ROM, work RAM, the control registers as logged stubs, the frame (384 x 272 at 6 MHz) and its vblank interrupt; BAC06 and MXC06 RAM windows as plain RAM | MRPAL + MAME `plds` (1, data), JTSLY (3); MAME trace (5) as detector | `baddudes` with an empty MCU socket runs from reset to the point it waits for the MCU, the 68000's stream equal to MAME's or each difference named; decode equations as unit tests | | M2 | **The sound CPU and its latch**: the 65C02 (`R65C02` row) with its ROM and RAM; YM2203/YM3812/MSM6295 sockets with fakes | MAME 65C02 trace (5) | `baddudes` sound CPU in lockstep with MAME's for 3 frames from reset | | M3 | **The i8751** (needs mcs51-python) and **the HuC6280 sub + MB8421** (needs the on-die timer/IRQ, decision 4) | MCS51, FSRAM91 (2); MAME traces (5) | `baddudes` and `robocop` reach their title screens with all CPUs in lockstep with MAME for 10 frames (U10, U11 answered) | | M4 | **BAC06 and MXC06 pictures**, the priority PROM, the palette | JTSLY wiring (3); MAME pictures (5), JTCOP's FPGA (4) if built | the attract screens of `baddudes` and `robocop` drawn; every pixel difference with MAME triaged; every chip rule tagged OBSERVED or FITTED | | M5 | **The HuC6280-sound rows**: `midres`, `slyspy`/`secretag` (Sly Spy's custom logic), `bouldash`; then `hbarrel`, `birdtry`, `hippodrm` | as M2-M4 | each row boots and plays its attract; C1, C4 settled or named | | M6 | **Sound**: the OPN and OPL cores pinned (decisions 6, 7), msm6295-python | their own ladders; MAME audio (5) | dec0 attract audio compared with MAME's, differences named | | M7 | **deco16 first row**: one unencrypted DECO 55/52 set with no protection chip (`darkseal` or `vaportra`, whichever proves unencrypted), the HuC6280 sound with YM2151 + MSM6295 x2 | MAME (5) | title screen; lockstep 68000 and HuC6280 for 10 frames | | M8 | **DECO 104/146, encryption (decision 9), and the rest by smoke list**: Caveman Ninja, Robocop 2, Rohga, Tumble Pop; karnov and the bac06 rows; save states | MAME (5) | the smoke list (problem sets and well-behaved ones) reaches gameplay; a save state loads into a fresh machine and continues identically | Full sweeps of the 175 sets only when the board might be ready (Aubrey's smoke-sweep rule). ## The chip rule | Chip | Users in or near the family | Proposal | | --- | --- | --- | | 68000, Z80, 6502, 65C02 | many | existing cores | | HuC6280 | PC Engine line, this board (106 sets) | existing core; its on-die timer/IRQ/PSG extracted from pcengine-python (decision 4) | | i8751 | System 16, System 1/2, Irem M72, this board | new `mcs51-python` (decision 5) | | YM2203 | this board (94 sets), PC-88, X1, many arcade boards | new OPN core (decision 6) | | YM3812 / YM3526 | this board (55 sets), many arcade boards, PC sound cards | new OPL core (decision 7) | | BAC06, MXC06, DECO 55/52/99/104/146 | this family and the out-of-scope ARM boards | stay in the board; extract on the ARM sibling (decision 2) | | MB8421 | Robocop/Hippodrome here; Irem M72 (irem-python proposes keeping it in its board) | stays in the board; a 2 KB RAM with two flags is small enough to keep twice, or extract if a third user appears (decision 8) | ## Unknowns (U) and conflicts (C) | # | Unknown | Best source now | Status | | --- | --- | --- | --- | | U1 | The frame (384 x 272 at 6 MHz, 57.44 Hz) on hardware | MAME only | FITTED to MAME | | U2 | 68000 wait states (DTACK) per region | the PALs (MRPAL) once reduced | from the decode; else zero, tagged | | U3 | Power-on RAM and register contents | none | zeros, option for random | | U4 | The 12 MHz -> sound clocks divider chain | JTSLY's nets | read the sound sheet | | U5 | The 65C02's maker (Rockwell bit instructions or not) | MBD says "65C02A"; MAME says R65C02 | `R65C02` row, provisional; a chip photo or the sound programs' use of BBR/BBS would settle it | | U6 | The deco16 boards' crystals | MAME's clocks | per row, tagged | | U7 | The MXC06's sprite format, line-buffer timing, sprites per line | JTCOP's remark only | MAME detector; FITTED | | U8 | dec0 I/O and control at 30C000-30C01F (MAME leaves 30C01A, 30C01E unmapped) | JTSLY decode sheet | trace the sheet; log every access | | U9 | The sound CPUs' maps and interrupt wiring | JTSLY's sound sheet (`~IRQ1`, `~IRQ2`, `~NMI`, `~CS_OPN`, `~CS_OPL`, `~CER`, `~CEY`) | read the sheet; MAME as detector | | U10 | Robocop's HuC6280 sub: PROM mapping, clock (21.477 / 16?), MB8421 wiring of INTL/INTR | none traced | MAME detector | | U11 | The 68000 <-> i8751 handshake | MAME's log lines only | the 8751's own program, once mcs51-python runs it | | U12 | cninja's writes to 1B0002-1B000C, 1B4000 at reset (MAME: unmapped) | none | log; a deco16 PAL reduction | | U13 | BAC06 register semantics, tile modes, JTCOP's "MSFT" column | none documented | observe (traces + pictures) | | U14 | What each PROM in the sets does (MB7116, MB7122, `tb-7`, MB7114 on JTSLY) | JTSLY's colour sheet | trace its address lines | | U15 | Palette bit layout | none | observe | | U16 | DECO 99 'ACE' | MAME's name only | late (Boogie Wings) | | U17 | Whether the HuC6280's PSG output reaches the amplifier | JTSLY sound sheet | read the sheet | | U18 | MSM6295 pin 7 per board | JTSLY ("PIN37/PIN8 M6295" notes) | read the sheet | | U19 | Sly Spy's `SLY_SPY_49` logic | JTSLY wiring; MAME traces | observe | | U20 | DECO 104 / 146 logic | MAME (closed) | decision 9 | | U21 | Which sets use DECO 102 program encryption and 56/74/141 graphics scrambling; the brief's "DECO 66" | MAME file names only | decision 9 | | # | Conflict | Sides | Proposal | | --- | --- | --- | --- | | C1 | Heavy Barrel's protection | MAME: an i8751 (`hbarrel` sets carry `mcu`); JTCOP's table: "HuC6280" | MAME's sets hold an 8751 dump and no HuC6280 program: follow MAME; JTCOP's table is likely a slip (ask if a channel to jotego opens) | | C2 | The Midnight Resistance PALs `tb-3` and `tb-4` | MRPAL (2011 PG4UW reads) vs MAME's `plds`: 1,068 and 252 of 2,048 fuses differ; the other four identical | reduce both to equations; the version whose decode matches the OBSERVED writes wins provisionally; keep both | | C3 | The 6 MHz pixel clock | MAME: 6.000 MHz; JTSLY's colour sheet labels "6.01MHz" | 6 MHz (12 MHz / 2); the label is probably a measurement or a typo (U4) | | C4 | The dec0 HuC6280 sound clock | JTSLY and MAME: 3 MHz on Sly Spy; MAME: 2 MHz on Midnight Resistance (the same family of board) | per row as MAME says until a Midnight Resistance board source appears; flag it in that row | | C5 | The dec0 sound CPU | MAME: R65C02 on dec0, NMOS 6502 on karnov/stadhero/actfancr; MBD: "65C02A" on Bad Dudes | agree on CMOS for dec0; U5 for the maker | ## Domains and speed [domains-and-speed.md](domains-and-speed.md) in full; the summary: - **Domains**: M (68000 and its bus), V (BAC06 x3 + MXC06, 12 MHz), S (65C02 or HuC6280 with YM2203, YM3812, MSM6295), P (the i8751, or the HuC6280 sub with the MB8421). deco16: M (with the DECO 104/146 window), V (DECO 55/52), S (HuC6280, YM2151, YM2203, MSM6295 x2). - **Channels**: the VRAM/palette/sprite windows (M <-> V), the sprite DMA and vblank (V <-> M), the sound latch and its interrupt (M -> S), the MCU ports or the MB8421 with its two mailbox interrupts (M <-> P), audio out (a pure producer). - **Instructions a second** (rates OBSERVED in MAME): dec0 Robocop 0.72 M (68000, 13.9 cycles an instruction at 10 MHz) + 0.54 M (65C02) + 0.30 M (HuC6280 sub) = **1.56 M**; Bad Dudes 1.60 M; Caveman Ninja 1.03 M (68000 at 12 MHz, 11.7 cycles) + 0.83 M (HuC6280) = **1.86 M**; Thunder Zone about 3.2 M. Plus 1-2 M FM operator steps and 6 M dots a second. - **The no-JIT path**: the Python reference on PyPy has 10-20 times the CPU need (m68000-python 18.7-33.5 M, huc6280-python 18.5 M warmed); video and FM are its cost, so below real time with FM is expected and acceptable. **deco16-rust** (the plain transcription: m68000-rust 134 M natively, about 112 M as WebAssembly; huc6280-rust 121 M) reaches real time on desktop, phone and browser without -fast; -fast is for run-ahead headroom; threads at the channels and a cached interpreter are not needed. `recompile: none` on every target, the iPhone included. ## Open decisions for Aubrey (each with a recommendation) 1. **Scope and first rows.** Recommend dec0 first with `baddudes` and `robocop` (two protection kinds, no encryption), then the HuC6280-sound dec0 rows, then one unencrypted deco16 row; karnov and the bac06 rows (Stadium Hero, Mad Motor, Act-Fancer) as later rows of the same board. 2. **One board or several.** Recommend one repository, `deco16-python`, for every 68000 (and HuC6280-main) row: the families share CPUs, sound and a decode style, and generation 1's chips appear on generation 2 boards (Vapor Trail's MXC06). The ARM boards (`deco32`, `simpl156`, `deco156`, `backfire`, `deco_mlc`: 62 sets) later as a sibling `deco32-python` that pins this board's DECO 55/52/104/146 once extracted (the chip rule's second consumer); Battle Rangers (HuC6280 + HuC6270) as a row of the PC Engine line or a tiny sibling, not here. 3. **The 65C02 row.** Recommend w65c02-python's `R65C02` (MAME's choice; captured on a real Rockwell part), marked provisional until a board photo or the sound programs settle U5. 4. **The HuC6280's on-die peripherals.** pcengine-python built its `cpu_io` (timer, $1400 IRQ controller, I/O port) and `psg` packages to be moved out "if a second consumer appears (a Data East arcade board's HuC6280)". This board is that consumer (106 sets). Recommend extracting them with history into huc6280-python (one die, one repository), pinned by pcengine-python and this board, before milestone M3. 5. **mcs51-python.** Recommend its Phase 0 now: this is its fourth consumer (system16-python, segasys1-python, irem-python, this board), and Bad Dudes / Robocop's milestone M3 needs it. 6. **The YM2203.** Recommend one OPN core, `ym2203-python`, shared with pc88-python (which pins the same data sheet and the OPN application manual), its SSG built on ay8910-python's YM2149 row; the YM2608 as its later variant. Coordinate with ym2610-python's OPNB work. 7. **The YM3812 / YM3526.** Recommend a new `ym3812-python` (OPL2 with the YM3526 as a variant, the Y8950 later), with an early Phase 0 task to find a YM3812 manual (none on bitsavers) and a respect-clean die or hardware source; ym2413-python (OPLL) is a relative to learn from, not to share code with. 8. **The MB8421.** Recommend keeping it in the board (as irem-python proposes for its board); extract only if a third family board needs it. 9. **MAME's BSD-3-Clause Data East files.** The brief keeps MAME closed. The DECO 104/146 logic, the DECO 102 and 56/74/141 decryption tables, and the BAC06/MXC06/DECO 55/52 register semantics have no other source found. Recommend allowing these files (each `license:` line rechecked, notice kept, facts tagged with their MAME origin and tier 5) when milestone M7 arrives, the same ruling system16-python asks for its BSD-3 files; until then the board is built from observation and the generation-1 rows need none of them. 10. **jtcores material.** Recommend accepting jotego's Sly Spy KiCad trace (`sch/`, outside `doc/`) under the jotego row as a board capture, and the Midnight Resistance PAL fuse maps as data with an unnamed reader (proposed sources.md row 4); and, if a channel opens, asking jotego who read the PALs and whether Heavy Barrel's "HuC6280" entry is a slip (C1). 11. **Cross-repo findings to act on (not touched here).** (a) dreamjam.co.uk's robots.txt disallows `/emuviews/` for every crawler; system16-python pins five MacDonald files from it: recommend adding the site to the site-wishes inventory and reviewing those pins. (b) system16-python's MAME recipe logs `a7` and `acc`, which MAME 0.285 accepts as hex constants: its stored traces show `A7` and `ACC` on every line; recommend fixing the recipe (`sp`; `a,sp,psw`) and rereading its FD1094 comparison, which still holds for PC, SR, D0 and cycles. 12. **sources.md rows** (emulator-lineage): recommend the seven rows proposed in [sources.md](sources.md) (Data East manuals; the Fujitsu SRAM book; the Yamaha YM2203, Y8950 and LSI catalogue; the jotego refinement; MAME's Data East files' licences; dreamjam.co.uk; the MAME symbol trap).