# Handoff brief: the MSM6295 core For the session that builds the core. It stands on its own; the evidence for each statement is in the subject documents: [bus.md](bus.md) (commands, status, timing, pins, clock), [rom.md](rom.md) (phrase table, data, banking), [decoder.md](decoder.md) (ADPCM, attenuation, mixing, output), [validation.md](validation.md) (oracle tiers), [census.md](census.md) (users and ROMs), [mame-oracle.md](mame-oracle.md) (the MAME tap). ## Scope The OKI MSM6295: four-voice 4-bit ADPCM playback from an external ROM of up to 256 KB through a phrase table, as a stand-alone pure-Python chip core. One chip, both packages (the DIP42 is the same chip with A17 and /RD not bonded out: a board concern). Python is the readable reference; correctness over speed; PyPy 3.11 must work. Not in scope: the board's filter, amplifier and mixing with other chips; ROM banking (NMK112 and every other scheme); the related OKI chips (below). Users: **2,743 MAME 0.285 machines (1,064 parents) in 316 driver files**; in the family, `cps1-python` first (one chip at 1 MHz, pin 7 from the Z80 at run time, no banking), later many arcade boards (NMK16, Cave, Kaneko, Data East, Seibu, Toaplan, Metro, Gaelco, Atari JSA, ...). ## Names - Distribution `msm6295-python`, repository `msm6295-python`. - Import package: proposed **`msm6295`** (as the task named it). The family's chip cores all use `_python` (`ay8910_python`, `sn76489_python`, `ym2413_python`, `gb_apu_python`): see Open decisions. - Class **`MSM6295`**; conventions as a frozen dataclass **`OKIVariant`** (a value, not a subclass, as `PSGVariant` and `OPLLVariant` in the siblings), with named presets: **`MAME_0285`** (MAME 0.285's conventions, the default for comparison), **`DATASHEET`** (MAME's conventions where the data sheet is silent, the data sheet's timing where it speaks), `LIBVGM`, `JT6295`, `FBNEO_STYLE`. **The `JT6295` and `FBNEO_STYLE` presets, and every alternative below attributed only to jt6295 or FBNeo, were learned by reading those programs' source under the superseded rule (2026-11-19):** build them only after re-establishing each value by running the program as a closed box (jt6295 under Verilator through its top-level ports; FBNeo as a built emulator), and cite the run; otherwise leave them out. None is ever a default. ## The import boundary | The core owns | The board owns | | --- | --- | | the command latch (pending first byte), the four channels' address counters, end addresses, attenuation codes, ADPCM signal and step index, BUSY | the address decoding of the chip's port, the /CS, /RD, /WR strobes and their timing | | counting input clocks into samples (33 x n, n from SS), the 15n/48n delays when the variant models them | the input clock: it simply ticks at whatever rate it has, and may change it at any time | | reading the table and the data through `rom_read` at the moments the chip reads | the ROM, its size, mirroring, open-bus value, and **all banking** | | the four channels' attenuated values and their sum, per sample | the D/A's scale, DC offset, the low-pass filter, the amplifier, mixing with FM, resampling to the host | | the low four bits of a status read | bits 7-4 of the status read (the bus), and whether /RD is wired at all (DIP42) | | /RESET's effect on the chip | when /RESET is pulsed (Atari JSA does it from a latch) | ## The board contract ```python from msm6295 import MSM6295, MAME_0285 chip = MSM6295(rom_read, pin7=True, variant=MAME_0285) chip.write(value) # one byte on /WR, an instant between two input clocks chip.read_status() # -> 0..15, bit 0 = channel 1 busy chip.reset() # the /RESET pin chip.pin7 = False # SS: True = clock/132, False = clock/165; any time chip.tick() # one input clock chip.run(clocks) # that many input clocks chip.clock # input clocks since reset chip.samples # completed samples since the last drain (see below) ``` - **Time is input clocks** (the XT pin), as every sibling core: the host never knows the divider, and a board that changes the chip's clock just ticks at the new rate. `tick()` exists beside `run()` because PyPy runs `run(1)` from a hot loop several times slower (measured on ay8910-python: 335 ns against 41 ns). - **Pin 7 is read when a sample closes**: a change applies from the next sample boundary; the counter inside the current sample keeps its count (the variant documents this choice: U10). - **`rom_read(address) -> int`**, address 0..0x3FFFF, a byte back. Called at the moment the chip reads, never cached: six table bytes (8p .. 8p+5) when a play command is taken in, and one data byte for each nibble decoded (MAME and jt6295 read the byte for each of its two nibbles; whether the chip latches it once is U13). A board that banks changes its own mapping between reads. - **Output per sample**: for each channel the decoded 12-bit signal and its attenuation code, and the **unrounded attenuated value signal x m** (m from 32, 22, 16, 11, 8, 6, 4, 3, 2, 0: units of 1/32 LSB), and the sum of the four. Exact integers, no float and no rounding choice (U7 stays the board's or the variant's). A board scales `sum / (32 x 2048)` to its own range. With `record=False` the core keeps only the latest sample for a polling board, as ay8910-python does. - **No filtering, no interpolation, no resampling** in the core: the data sheet and OKI's product table describe none. - **Power-on is /RESET's**: all channels idle, no pending first byte, signal and step index 0, the sample divider at the start of a sample. ## The variant fields (the conventions nothing in hand decides) | Field | `MAME_0285` | Alternatives seen | Unknown | | --- | --- | --- | --- | | `start_signal` | 0 | -1 (FBNeo), -2 (libvgm, MAME before 2020) | U2 | | `overflow` | `"clamp"` | `"wrap"` (OKI's MSM5205) | U1 | | `phrase_nibbles` | `2*(end-start+1)` | `2*(end-start)` (FBNeo), `2*(end-start)+1` (jt6295) | C6 | | `start_ge_end` | `"ignore"` | `"one_nibble"` (jt6295), `"wrap_to_end"` (an equality comparator) | U3 | | `address_high_bits` | `"mask"` | `"keep"` (FBNeo) | C11 | | `play_on_busy` | `"ignore"` | `"restart"` (jt6295's HDL) | U5, C9 | | `multi_channel` | `"all"` | `"lowest"`, `"none"` | U5 | | `attenuation_9_15` | silent | further 3 dB steps (FBNeo) | U5, C7 | | `timing` | `"immediate"`: BUSY at the second byte, sound from the next sample, stop at once | `"datasheet"`: BUSY after 15n clocks, sound after 48n, stop at the next sample | U8 | ## Unknowns - **U1** Accumulator overflow: clamp (all four implementations) or wrap (OKI says its MSM5205 wraps; MAME clamps the MSM5205 too, Mesen2 and Geargrafx wrap it, per pcenginecd-python's notes). Nothing for the MSM6295. - **U2** The signal at a phrase start: 0, -1 or -2, and whether a phrase started straight after another on the same channel inherits anything. - **U3** An entry with start >= end: nothing, one nibble, or play until an equality comparator meets the end after the 18-bit counter wraps. CPS-1 ROMs hold 50 such entries. - **U4** Phrase 0 (`80`): "not valid"; every implementation plays entry 0. - **U5** A play on a busy channel; several channel bits in one second byte; attenuation codes 9-15. - **U6** How four 12-bit channels reach one 12-bit D/A: a clipped sum, a wrapping sum, scaled channels, or a D/A shared in time. - **U7** The rounding of the attenuation multiply, if the chip rounds before the D/A. - **U8** When BUSY falls at a phrase's natural end, and what Artemio Urbina found MAME gets wrong about BUSY on a real CPS-1 board (unstated). - **U9** Whether a pending first byte waits for ever, and what /RESET does to it (MAME keeps it, jt6295 clears it). - **U10** A pin 7 change inside a sample; the divider's phase after /RESET. - **U11** Status bits 7-4 on a real bus (the pins are inputs). - **U12** (closed) The DIP42 lacks A17 and /RD: confirmed on the '94 sheet's 42-pin drawing (p. 55). - **U13** Whether the chip reads a data byte once per byte or once per nibble (matters only to a board that banks between the two nibbles). - **U14** The order and slots in which the chip serves its four channels inside a sample, and so where within a sample a command takes effect. ## Contradictions between sources - **C1** Sample rate with SS low at 1.056 MHz: "6.5 kHz" ('94 pin table) against 6.4 kHz (1987) and clock/165 = 6,400 Hz ('94 p. 67). - **C2** Oscillator: 1.18 and 4.13 MHz (1987 pin text) against 1 and 4 MHz (1987 features) and 1.056 and 4.224 MHz ('94). - **C3** Gap between the two bytes: 7,500 ns (1987 figure) against >= 500 ns ('94); interval between commands 15 Tcyc (1987) against 75 tCYC ('94, = 15n at SS low); delay to sound 216 µs (1987) against 227 µs ('94). - **C4** Attenuation: "-3 dB to -24 dB, 8 steps" (1987) against "9 levels from about 0 dB to -24 dB" ('94), same table. - **C5** Start value: MAME 0, FBNeo -1, libvgm -2 (MAME's own until 2020). - **C6** Phrase length: MAME, libvgm and the data sheet's figure 2(end-start+1); FBNeo 2(end-start); jt6295 2(end-start)+1. - **C7** Attenuation 9-15 silent (MAME, libvgm, jt6295) against FBNeo's further 3 dB steps; FBNeo's exact 3 dB steps against the 32-based table for 1-8. - **C8** Status bits 7-4: 1111 (MAME, libvgm, jt6295) against 0000 (FBNeo). - **C9** jt6295's README (a play on a busy channel is ignored) against its HDL (it restarts). - **C10** SS low divides by 165 (data sheet, jt6295's counters) against 164 (jt6295's comment, and the 6,097.5 Hz derived from it in jtcps and quoted in cps1-python's notes). - **C11** High bits of the table's first address byte: dropped (MAME, libvgm, jt6295) against kept in the length (FBNeo). - **C12** OKI says the MSM5205 wraps on overflow; MAME's MSM5205 clamps. Not about this chip, but it means MAME's clamp is not evidence for U1. ## The ladder Each rung has an exit test; a rung's results go into `docs/validation.md`. **Rung 0. Phase 0** (this repository at the end of Phase 0). Exit: the documents, `scripts/fetch_references.py --verify` passes, the MAME tap has been run. **Rung 1. Data sheet to tables.** The core's constants come from formulas with their sources beside them. Exit tests: - the step table is floor(16 x 1.1^n), n = 0..48, computed in exact rationals, and equals Dialogic's 49 printed values (typed into the test from the PDF); - the 16 x 49 difference table built from the per-term formula equals a brute-force table from the formula, including the step-19 case (34, not 35); - for the nine attenuation codes, trunc(20·log10(m/32), 0.1 dB) equals the data sheet's printed figure, and no other m in 1..32 does; - 132 and 165 are 33 x 4 and 33 x 5; at 1 MHz the rates are 7,575.76 and 6,060.61 Hz; at 1.056 MHz 8,000 and 6,400. **Rung 2. The decoder against a second implementation, every local phrase.** Decode, by direct computation and not by running any game, every phrase with start < end that lies inside its region in all 1,249 distinct sample regions read by `scripts/rom_census.py` (for the 648 regions larger than 256 KB, every 256 KB window that begins with a plausible table; the windows chosen are recorded). The second implementation is a small C program built from MAME's two BSD-3-Clause files' arithmetic (`okiadpcm.cpp`) driven by a 30-line harness, fed the same nibbles: it prints each phrase's decoded 12-bit sequence, or its SHA-256. Exit: **zero differing values** over every phrase with the `MAME_0285` variant; with `start_signal = -2` zero differences against the same harness set to -2 (libvgm's convention); the count of phrases whose decoded values ever reach the clamp is recorded (the phrases where U1 matters), and so is the count where start value -2 and 0 still differ at the last sample (where U2 matters). **Rung 3. Commands, status and timing.** Unit tests written from the data sheet and the variant table: the play and stop bit layouts; a second byte after any first byte; 0, 1 and several channel bits; play on busy; phrase 0; start = end, end = start - 1 (CPS-1's 47 entries), start > end; attenuation 0-15; stop of several channels; `reset()` with a first byte pending. With `timing="datasheet"`: BUSY reads 0 for 15n - 1 clocks after the second byte and 1 at 15n; the first nibble's value appears in the sample that closes after 48n clocks; a stop clears BUSY at the next sample boundary; at SS high and low. With `timing="immediate"`: MAME's timing. Exit: all pass on CPython and PyPy. **Rung 4. Whole-chip streams against MAME.** Replay MAME tap logs (`scripts/mame_oki_tap.py`; Final Fight and at least four other CPS-1 games, each a few emulated seconds, one MAME at a time) into the core and into MAME's `okim6295_device` built into a small harness (or a second Lua tap that logs the device's per-sample voice states), aligned by the logged clock. Exit: **zero differing per-channel values** against MAME 0.285 with `MAME_0285` over every logged stream; each difference with the other presets named by its variant field (C5-C8, C11). **Rung 5. The board recordings.** From Artemio Urbina's FLAC recordings of the Magic Sword and SF2 boards and the MDFourier profile (`mdfblocksCPS1ADPCM.mfn`): measure the sample rate of the pin 7 high and low blocks from the spectral images (expected 7,575.76 and 6,060.61 Hz at the boards' 16 MHz / 16, within the crystal's tolerance), and the nine attenuation steps of each block. Exit: both rates and steps 0-8 agree with the data sheet within the measurement's stated error; any disagreement becomes a C-item. This is the only hardware-tier rung. **Rung 6. Fault injection.** Seed faults (one per line of the decoder and the command handling: step table entry, index adjustment, clamp bound, nibble order, end inclusive, channel bit order, stop bit order, attenuation entry, pin 7 divisor, BUSY timing), as the siblings' `scripts/mutation.py`. Exit: every seeded fault is caught by rungs 1-4's tests. **Rung 7. Speed.** Measure input clocks per second on CPython and PyPy with four channels playing. Exit: the number is recorded with the machine's load; no target (reference cores are not real-time). A fast path that changes an output value is not allowed. **Rung 8. Adoption by cps1-python** (that session's). The board pins the core by commit. ## Related OKI chips, under the chip rule | Chip | MAME machines | Family users | What it shares with the MSM6295 | Recommendation | | --- | --- | --- | --- | --- | | MSM5205 (and MSM6585, its successor) | 533 (+19) | **pcenginecd-python** decodes it inside the board (`adpcm.py`); later arcade boards (Double Dragon and others) | the step table and the per-term arithmetic only. No ROM, no phrase table, one voice fed by the host each sample, 3-bit mode, its own clock, wraps per OKI, 10-bit D/A | **a separate core, not a variant here**, extracted from pcenginecd-python when a second family board needs it (the extract-on-second-consumer rule) | | MSM6258 | 5 (X68000 and few) | none | the arithmetic; host-fed like the MSM5205 | leave alone | | MSM9810 | 58 | none | ROM-based multi-voice, but 8 voices, its own ADPCM2 tables, PCM modes, different command set | leave alone; a separate core if ever needed | | MSM6376, MSM6650, MSM6588 | 5,808 / 36 / 2 | none (fruit machines and others) | | leave alone | The common table (49 steps, eight adjustments) is about ten lines; sharing it across repositories is not worth a dependency. Each core writes it from the formula and tests it against Dialogic's numbers. ## Licences The core is MIT, like the family. MAME's two files are BSD-3-Clause and may be built into a local, ignored referee; jt6295 is GPL-3.0 and FBNeo non-commercial: **closed box only** (run, never read; wave-2 rule of 2026-11-19), no code of theirs in this repository, and no default may rest on them alone. The recordings carry no stated licence: cite them, never commit or redistribute. ## Open decisions for Aubrey **Decided by Aubrey on 2026-11-12** (recorded after a walk through all fourteen Phase 0 briefs): - The Python package is `msm6295_python` (family rule set the same day: a repository `x-python` always imports as `x_python`); read any other package name in these documents as that. - Decision 5 (asking Artemio for a test recording): deferred with the other outside contacts. - Referees built from die-derived material are built just before the rung that needs them, not up front. - Outside contacts proposed below are **deferred until the core or board exists**; Aubrey sends them himself, from a draft. - Remote: local through Phase 0; after the first passing rung or milestone, public like the other chip cores, after the usual history audit. Where a recommendation below names a different point, this replaces it. - Every other recommendation below is **accepted as written** unless a line here says otherwise. 1. **Package name.** `msm6295` (as proposed for this task) or `msm6295_python` (every sibling chip core's form). *Recommendation: `msm6295_python`, for one convention across the family.* 2. **Default variant.** `MAME_0285` everywhere, or `DATASHEET` (MAME's conventions where the data sheet is silent, its 15n/48n timing where it speaks). *Recommendation: `DATASHEET` as the default for boards, since tier 2 beats tier 5 where they differ, with `MAME_0285` used for the rung 4 comparison; every other choice follows MAME 0.285 and is named.* 3. **Rung 5, the recordings.** Analyse the CPS-1 board recordings in this repo (rates, attenuation steps), the only hardware-tier evidence there is. *Recommendation: yes; it is a few hundred lines of Python on 192 kHz audio (`ffmpeg` is installed at `/usr/bin/ffmpeg` to decode the FLAC to raw PCM; `flac` and `sox` are not).* 4. **Referees built locally.** Only MAME's BSD files as a small C harness, or also jt6295 under a Verilog simulator (none is installed; Icarus would have to be built in user space, as ay8910-python did). *Recommendation: MAME's harness only; jt6295 is debugged against MAME and adds no independent evidence on the values. Revisit if a hardware question (U14) needs its pipeline.* 5. **Asking for hardware evidence.** Artemio Urbina has CPS-1 boards and an MDFourier CPS-1 test; a recording of a test ROM built for U1 (a phrase that overflows), U3 (start > end), U5 (play on busy) and U6 (four channels at full scale) would settle them at tier 1. *Recommendation: write the test ROM in the core's rung 3 and ask only when the core exists; mention the model when posting (Claude Opus 5.5).* 6. **sf2ud.zip** (Capcom ROMs inside) is already in `reference/` from the first session. *Recommendation: leave it there, ignored and opt-in in the fetch script; it is only needed if the MDFourier test is to be run in MAME.* 7. **Remote.** None created. *Recommendation: a private GitHub remote when the core has code, per the remotes-as-needed rule.*