# Handoff brief: building the core This is the brief for whoever builds `ym2151-python` from the restarted Phase 0. Read [restart.md](restart.md) first (why the repository started again and what may never be read), then [start-here.md](start-here.md). ## Scope A pure-Python, MIT-licensed, cycle-level reference core of Yamaha's YM2151 (OPM), with the YM2164 (OPP) as a variant: registers and bus, phase generator, operators, envelope, LFO, noise, timers, flags, /IRQ, BUSY, CT, test register, /IC, the accumulator and the serial output. Not in scope: the YM3012 D/A converter, analog filtering, any board. The specification is the set of documents in [README.md](README.md); every rule carries its evidence tag. Where the documents say DIE, the source is IKAOPM 08a5b40 (BSD-2): describe-and-reimplement, never translate its Verilog statement by statement. Where they say OBSERVED (Nuked-OPM), the number came from running the closed box; it is overruled by any document or hardware number. ## Names (decision 1, family rule) Package `ym2151_python`; engine class `OPM`; variants `OPMVariant.YM2151` and `OPMVariant.YM2164`, passed as `OPM(OPMVariant.YM2151)` (as ym2413-python has `OPLL(YM2413)`). CT is a 2-bit `ct` by register bit (decision 8), no CT1/CT2 names; the docstring states the pin question (C1). ## Cycle level (decision 2) One internal cycle per step (two input clocks); 32 cycles per sample, one slot each. Time is counted in input clocks and no rate is assumed. A constructor flag `enabled=False` gives a quiet path that keeps the bus, registers, timers, BUSY, /IRQ and CT exact but computes no sound (for boards that only need the timers, such as the CPS-1 sound program's polling). ## The board contract (decision 3) - In: `write(a0, data)` and `read(a0)` at a given input-clock time, `/IC`, and clock advance by input clocks. - Out, per cycle: SO, SH1, SH2, /IRQ (a live line, not a polled value: boards wire it to a CPU interrupt, [users.md](users.md)), `ct` (boards bank-switch on it; it must be observable every time it changes). - `take_samples()`: decoded `(left, right)` per sample ([output.md](output.md)). - The YM3012, filters and mixing belong to the board. ## Import boundary The core imports only the Python standard library. Nothing under `reference/` is imported, read or packaged; tables taken from IKAOPM live in the core's own source with the BSD-2 notice (NOTICE) and are checked at rung 0 against `scripts/ikaopm_tables.py --dump` and the frequency/DT1 rules in `scripts/referee_freq.py`. Floor: the newest Python PyPy supports (3.11 now); watch PyPy's 80-attribute limit on hot objects. ## Unknowns | ID | Unknown | Where | | --- | --- | --- | | U1 | The YM3012's exact transfer (mantissa offset, half LSB, analog) | [output.md](output.md) | | U2 | Write latency at the pins (strobe to effect) and what a data write during BUSY does on silicon | [registers.md](registers.md), [timers-and-bus.md](timers-and-bus.md) | | U3 | What a read with A0 = 0 returns (the bus is not driven) | [registers.md](registers.md) | | U4 | Test register bits 0, 2-7: die reading only | [timers-and-bus.md](timers-and-bus.md) | | U5 | The YM2164 beyond timer B and the CT lines | [ym2164.md](ym2164.md) | | U6 | The absolute phase of SH1/SH2/SO and of each pipeline stage after /IC, against the referee | [output.md](output.md), rung 1 | | U7 | Noise at NFRQ 31: one or two steps per sample | [lfo-noise.md](lfo-noise.md) | ## Contradictions | ID | Between | Status | | --- | --- | --- | | C1 | CT pin numbers: Yamaha's manual + data sheet (bit 6 on pin 8) against IKAOPM's die trace (bit 7 on pin 8) | open; `ct` by bit (decision 8) | | C2 | BUSY: manual and die reading about 68 input clocks from the strobe; closed box 64 from the next cycle; MAME 64 | core follows the die; the referee offset is written down | | C3 | DT1 table: manual Fig. 2.6 last rows (5 cells) against IKAOPM, ymfm and the closed box | settled for the die (448/448 measured) | | C4 | Noise frequency: the manual's printed formula against its own range | the range form, which the die gives | | C5 | Muted noise slot: IKAOPM 0, closed box -8/0 | decision 9: follow the referee, keep open; IKAOPM's run votes before rung 4 | | C6 | LFO period for LFRQ < 0x10: IKAOPM 7/8 of the manual's, closed box = manual | core follows the manual | | C7 | LFO PM: ymfm's shift approximation against IKAOPM's adders | IKAOPM followed; ymfm is a detector | | C8 | Noise register clocking: ymfm against IKAOPM | IKAOPM followed; exact sequence owed to rung 3 | ## The ladder Each rung has a gate: a command whose passing is the proof, run on every kept commit. "The referee" is the closed box through `validation/opm_ref.c`. Differences from the referee are counted and written down; they are fixed only when a document or the die reading says the core is wrong (restart rule 6). | Rung | Content | Gate | | --- | --- | --- | | 0 | Tables and decode: log-sine, exponent, frequency ROM and interpolation, DT1, KS, register decode, slot map | core tables equal `scripts/ikaopm_tables.py --dump` (256/256 each); the core's step for all 8,192 KC x KF and DT1 table equal `reference/referee-freq.txt` (closed-box measurements), never a dump reaching inside a model | | 1 | One slot: phase generator, operator, envelope, single-carrier streams of `referee_measure.py` and `referee_freq.py` | SO bit-exact against the referee for every directed stream; U6 settled by measurement | | 2 | Bus and timers: BUSY, flags, /IRQ, CT, CSM, test bit 1, the quiet path | pins and status per cycle equal to the referee on the probe streams and the CPS-1 `ghouls` tap logs; C2's offset recorded | | 3 | Every CON with feedback, LFO (all waves, AM, PM), noise, both sides of the accumulator | SO bit-exact against the referee on a directed set covering each CON and LFRQ class; CON delay column and C8 checked | | 4 | Real streams: MAME taps (`ddragon`) and vgmrips packs (decision 10); YM2164 variant; IKAOPM under Verilator as a second die-derived opinion (decision 5) | SO bit-exact against the referee over the replayed streams; IKAOPM's votes on C5, C6 recorded | | 5 | Board integration: cps1-python pins the core | the board's own gates with the core in place | | 6 | Publication: history audit, docs freshness, public (decisions 4, 7) | audit clean; Aubrey's go | ## The owner's decisions (2026-11-12, carried by [decisions.md](decisions.md)) 1. **Names**: package `ym2151_python`, engine `OPM`, variants `YM2151`, `YM2164` as `OPMVariant` values. 2. **Cycle level**, one internal cycle per step, time in input clocks, `enabled=False` quiet path. 3. **Output contract**: pins per cycle are the contract; `take_samples()` gives decoded `(left, right)`; the YM3012 and filters belong to the board. 4. **YM2164**: the variant fields from the start; the rest after rung 4 (now resting only on closed-box observation and any document found: [ym2164.md](ym2164.md)). 5. **A Verilog simulator** so IKAOPM runs as a second die-derived opinion: yes, before rung 4; Verilator 5.053 is on this machine in the oss-cad-suite bundle (sm83-python's reference build); no install needed. 6. **Licence approach** (rewritten 2026-11-19): MIT core in this repository's words, from Yamaha's documents, IKAOPM (BSD-2) and ymfm (BSD-3) with their notices, and closed-box measurements. Nuked-OPM (LGPL-2.1), jt51 (GPL-3.0) and MAME 0.228 (GPL-2.0+) are **run, never read**. 7. **Public**, like the other chip cores, after the usual history audit (local through Phase 0; public after the first passing rung). 8. **CT naming**: a 2-bit `ct` by register bit, pin number in the docstring. 9. **The muted noise slot** (C5): follow the referee (OBSERVED) and keep the claim open; IKAOPM gets a vote when it runs. 10. **vgmrips packs**: Aubrey downloads a handful into `validation/streams/` (ignored); rung 4 can start from the MAME taps without them. Also decided: referees built from die-derived material are built just before the rung that needs them; outside contacts wait until the core exists and Aubrey sends them himself.