# ym2612-python The Yamaha YM2612 and YM3438 (OPN2) FM sound chip as a readable pure-Python core for megadrive-python. No runtime dependencies. PyPy 3.11 is the supported interpreter. ```python from ym2612_python import YM2612, YM2612_DISCRETE fm = YM2612(YM2612_DISCRETE) # the model 1's discrete chip; YM3438_ASIC for later boards fm.write(0, 0x22) # address, part I fm.write(1, 0x00) # data fm.run(7_670_453) # one second of the chip's clock pin (NTSC) frames = fm.take_samples() # one (left, right) pair per 144 clocks ``` [docs/api.md](docs/api.md) is the whole surface. ## Status (2026-11-13) **Built from documents.** The code is written from [docs/spec.md](docs/spec.md), which is written from the sources in [docs/phase0.md](docs/phase0.md) under the rules in [docs/restart.md](docs/restart.md). Each statement was checked against its source's prose before any code; [docs/provenance.md](docs/provenance.md) gives each statement's chain, the decisions taken where the sources stop, and what was deliberately not read. Every rule in the code names its spec section, or says CHOICE or PROVISIONAL. What it supports, and no more: *the core does what Yamaha's and Sega's manuals say and what Nemesis and others report reading out of real chips; it steps once per sample and takes writes at sample boundaries.* **No measurement was made for it, and nothing is claimed exact to a console or to any other model.** The discrete chip's output stage (the "ladder") uses provisional constants. [docs/claims.md](docs/claims.md) lists each claim with its evidence; [docs/validation.md](docs/validation.md) says what was run and what it showed. - 186 tests pass on PyPy 3.11: the contract (12), published read-outs of real chips (64), the manuals (71), community statements checked on hardware (18), and the provisional decisions (21). - `validation/faults.py` breaks the core in 28 ways; every fault is noticed. - Nuked-OPN2 was run as a closed box and **its source was not read** (`validation/closed_box.py`). 9 of 55 streams agree on every channel with the same writes; the differences are written down as open, chiefly when a write is heard, and nothing was tuned to it. - About 9 times real time on PyPy with six channels sounding. - Used by megadrive-python. Not on PyPI. ym2612-rust is a Rust port proven equivalent to a pinned revision of this repository. Yamaha's YM2612 application manual (S1, Japanese), from a Sega of Japan developer binder that circulates online, is cited a sentence at a time; nothing from it is copied here. Nearly everything it says is also in Yamaha's YM3438 manual (S2). ## What is known, and how *documented*: a manual says so (tier 2). *measured*: someone read it out of a real chip or saw it at a pin, and said so in prose (tier 1). *community*: an author who tested on hardware (tier 3). *provisional*: no source above an emulator settles it; the value is a decision, labelled in the code and pinned by a test. Spec section numbers in brackets. | Behaviour | Standing | |---|---| | Register map, addressing, the address latch [2, 3] | documented; the shared latch measured | | Phase generator: detune, multiple, 17- and 20-bit wraps, key-on reset [5] | measured (Nemesis) and documented | | Operator: log-sine and exponent tables, output range, total level [6] | measured (the OPL read-outs, Nemesis) | | Envelope: rates, the every-third-sample clock, attack curve, SSG-EG [7] | measured; the step table and attack formula **provisional**, ymfm's (U21, U22) | | LFO: rates and depths [8] | documented; divider length and vibrato precision **provisional** (C14, C16) | | Algorithms, feedback, modulation paths [9] | documented; the delayed paths from a die reading, **provisional** (T4) | | Channel 3 special mode, CSM [10] | measured | | Timers, status, BUSY [11, 12] | measured; status addresses **provisional** (T3) | | DAC channel [13] | documented and measured | | Accumulator and panning [14] | measured; the upper limit **provisional** (T5) | | Output stage of the discrete chip [15] | the shape measured; the constants **provisional**, ymfm's (T10, U12) | | State after reset, test registers [16, 17] | **provisional** (T14, T8) | | When inside a sample a write lands [2.8, U1] | not modelled: writes are heard from the next sample (T9) | ## Layout - `src/ym2612_python/`: `chip.py`, `operator.py`, `tables.py`, `variant.py`. - `tests/`: `test_contract.py` (rung 0), `test_measured.py` (rung 1), `test_manuals.py` (rung 2), `test_community.py` (rung 4), `test_provisional.py`. - `validation/`: `faults.py` and `closed_box.py` (rung 6). The rung's harness, `nuked_harness.c`, is linked with Nuked-OPN2, so it is a GPL tool kept outside this repository: since 2026-11-24 it is nuked-harness's `opn2-stream` (built in emu-tools-gpl, run at `$EMU_TOOLS_GPL/tools/nuked/build/opn2-stream`); set `EMU_TOOLS_GPL`. - `scripts/fetch_references.py`: the pinned references, into the git-ignored `reference/`. Nuked-OPN2 is not fetched (Aubrey, 2026-11-24): it is compiled only in emu-tools-gpl, its `.c` file never opened; its header is cited as nukeykt/Nuked-OPN2 at `335747d` (docs/provenance.md). - `docs/`: `api.md` (the surface), `spec.md` (the behaviour), `phase0.md` (the sources), `provenance.md` (chains, decisions, what was not read), `validation.md` (the ladder and its results), `claims.md`, `restart.md` (the rules). ```sh python -m pytest # all rungs python validation/faults.py # can each rung fail? python validation/closed_box.py # the closed-box comparison (needs EMU_TOOLS_GPL) ``` ## History This core was rebuilt in November 2026. Its predecessor followed an LGPL-licensed model's source code (Nuked-OPN2) too closely to be published under the MIT licence, and was withdrawn. This version is written from manufacturer documents and hardware measurements under the rules in [docs/restart.md](docs/restart.md); the model is only run, as a closed box, to find differences. ## Licence MIT.