# The decoder, attenuation, mixing and the output What OKI printed, what four implementations do, and where they part. **The MSM6295 data sheet does not give the decoder's arithmetic, its step table, its starting state, or what happens on overflow.** Those come from OKI's general literature (the 1987 data book's introduction), from Dialogic's application note for the same algorithm, and from the emulators, which agree on almost everything and have no hardware measurement behind the parts they agree on ([validation.md](validation.md)). ## The algorithm in OKI's and Dialogic's words The 1987 data book's introduction (printed pp. 4-6, `reference/docs/db1987.txt` around line 220) gives OKI's ADPCM reproduction: qn = (1 - 2·B3) x (4Δ·B2 + 2Δ·B1 + Δ·B0 + Δ/2) X(n) = X(n-1) + qn "Xn is set to 12 bits" Δ(n+1) = Δ(n) x M(Ln) with B3 the sign bit and B2-B0 the magnitude, and no table of Δ or M. The '94 data book does not print them either (its text layer has neither 1552 nor 1411). The MSM6295 sheet says its data is "Oki straight ADPCM" (p. 53) and DS87 that it is "compatible with the data which is analyzed by MSM5218 or MSM6258". Dialogic's *Dialogic ADPCM Algorithm* (00-1366-001, 1988; Dialogic licensed OKI's method for its voice boards) prints the rest: - d(n) = ss(n)·B2 + ss(n)/2·B1 + ss(n)/4·B0 + ss(n)/8, negated if B3 = 1. With ss = 4Δ this is OKI's formula. - the step-index adjustment by the three magnitude bits: **-1, -1, -1, -1, +2, +4, +6, +8** for 0-7; - **49 step sizes**, from 16 to 1552: 16 17 19 21 23 25 28 31 34 37 41 45 50 55 60 66 73 80 88 97 107 118 130 143 157 173 190 209 230 253 279 307 337 371 408 449 494 544 598 658 724 796 876 963 1060 1166 1282 1411 1552 These are exactly **floor(16 x 1.1^n)** for n = 0..48: checked in exact rational arithmetic and with MAME's and FBNeo's floating-point expressions, all 49 equal; jt6295's `lut` holds the same 49 numbers. - "When the ADPCM algorithm is reset, the step size ss(n) is set to the minimum value (16) and the estimated waveform value X is set to zero (half scale)." - 12-bit linear output. **Nothing about overflow.** ## The decoder as all four implementations run it Per nibble *b* (4 bits), with signal *x* (signed) and step index *i* (0-48), step = STEP[i]: magnitude = step*B2 + (step >> 1)*B1 + (step >> 2)*B0 + (step >> 3) x = x - magnitude if B3 else x + magnitude x = clamp(x, -2048, 2047) i = clamp(i + ADJUST[b & 7], 0, 48) Points that a reimplementation gets wrong easily: - **Each term is truncated on its own** (`step/2`, `step/4`, `step/8` in integer arithmetic, shifts in jt6295) and then summed. That is not floor of the exact sum: at step 19 (index 2) the magnitude for `0111` is 19 + 9 + 4 + 2 = 34, where the exact sum is 35.625. - **Sign and magnitude**: a negative nibble subtracts the same magnitude, so the code is symmetric (`1000` subtracts step/8, it does not add a two's-complement value). - The signal uses the **old** step; the index moves afterwards. - MAME precomputes all 49 x 16 differences (`okiadpcm.cpp`, `compute_tables`); the same table can be built from the formula above. > **Provenance note (2026-11-19).** The FinalBurn Neo and jt6295 columns were written by > reading those programs' source under the superseded rule "read, cite, never copy". > FBNeo (non-commercial licence) and jt6295 (GPL-3.0) are **closed boxes** from now on: > their source is not opened again. The columns stay as a record of what those detectors > do; none of them is a basis for any default (docs/provenance.md). | Question | MAME 0.285 | FBNeo c3e8c4d | libvgm c5c03a2 | jt6295 7d76b0b | | --- | --- | --- | --- | --- | | magnitude | as above, `/` on ints | as above | as MAME | as above, over pipeline stages | | signal overflow | clamp -2048..2047 | clamp | clamp | clamp (a 14-bit sum, saturated) | | step index | clamp 0..48 | clamp | clamp | clamp (an index past 48 brought back to 0 or 48) | | signal at a phrase start | **0** | **-1** | **-2** | 0 (forced to 0 while the channel is idle) | | step index at a phrase start | 0 | 0 | 0 | 0 | | state carried from the previous phrase | none | none | none | none | **Overflow, clamp or wrap (U1).** All four clamp. OKI's '94 data book says of the MSM5205 that it "is not equipped with an overflow protection unit" and that an overflowed waveform "is looped" (it wraps), while the MSM5218 and MSM6585 have overflow protection (the MSM6585/MSM5205 comparison table: "Overflow preventing circuit: Included / Not included"). **Nothing OKI printed says which the MSM6295 does.** MAME's own MSM5205 device clamps too, against OKI's statement for that chip (C12): so MAME's clamp is not evidence about the MSM6295. **Starting value (U2, C5).** MAME used -2 until commit `a557983` (cam900, 2020-06-11, "okiadpcm.cpp: Add looped sample support, Allow reading current output without decoding"), which made it 0 for the 4-bit decoder and kept -2 for the MSM9810's ADPCM2; the message gives no reason. libvgm kept -2, FBNeo has -1, Dialogic's reset value is 0 "(half scale)". A different start shifts every sample of a phrase by a constant until the clamp absorbs it, so the decoder rung can tell the conventions apart exactly ([handoff-brief.md](handoff-brief.md)). **Nibble order**: high nibble first (DS p. 63); every implementation agrees (MAME, BSD-3, and jt6295 both take the high nibble of each byte first). ## How it differs from the MSM5205 The MSM5205 (one voice, no ROM interface: the host feeds it a nibble per sample on four pins and clocks it from a 384 kHz resonator through the S1/S2 pins at 4, 6 or 8 kHz) has the same step table and the same index adjustments in MAME, plus a 3-bit mode. Differences that matter for a core: | | MSM6295 | MSM5205 | | --- | --- | --- | | voices | 4, from ROM, by phrase | 1, fed by the host each sample | | sample clock | input clock / 132 or / 165 | 384 kHz / 48, 64 or 96 (S1, S2), or an external clock | | data | 4-bit only | 3- or 4-bit | | overflow (OKI) | not stated | **wraps** ("looped"), no protection | | D/A | 12-bit | **10-bit** (MAME drops the low two bits: `m_signal & ~dac_mask`) | | reset | /RESET pin | RESET pin holds the decoder at 0, used by hosts to stop it | ## Attenuation (DS p. 61, p. 65) The second byte's low four bits. The data sheet's table: | Code | DS (dB) | m/32 | 20·log10(m/32) | jt6295 measured on the Magic Sword board (step from the previous) | | --- | --- | --- | --- | --- | | 0 | 0 | 32 | 0 | (reference) | | 1 | -3.2 | 22 | -3.25 | -3.3 | | 2 | -6.0 | 16 | -6.02 | -2.7 | | 3 | -9.2 | 11 | -9.28 | -3.3 | | 4 | -12.0 | 8 | -12.04 | -2.8 | | 5 | -14.5 | 6 | -14.54 | -2.5 | | 6 | -18.0 | 4 | -18.06 | -3.4 | | 7 | -20.5 | 3 | -20.56 | -2.4 | | 8 | -24.0 | 2 | -24.08 | not measured | | 9-15 | **not in the data sheet** | | | not measured | The data sheet's figures are those of a multiplier m/32 with m = 32, 22, 16, 11, 8, 6, 4, 3, 2, each **truncated toward zero to a tenth of a dB** (-9.28 printed -9.2, -18.06 printed -18.0, -20.56 printed -20.5). Checked 2026-11-12: among m/32 for m = 1..32, each of the nine printed figures is matched by exactly one m under that rule (and among m/64 only by 2m), so the table pins a 5-bit multiplier down uniquely. DS p. 65's figure says the -24 dB output is "MAX. 1/16 VDD p-p" against "MAX. VDD p-p" at 0 dB: 2/32 = 1/16. MAME's comment guesses the same ("seems to map to a 5-bit volume control"). The measurements are jotego's, reported in a comment of `jt6295_adpcm.v` (commit `0207ec1`, 2024-03-02; read under the old rule): codes 0-7 on two CPS-1 boards (Magic Sword 88617A and SF2 89626A), 192 kHz recordings, power at 637 Hz. They agree with the 32-based steps to about 0.1-0.2 dB, through each board's analogue path; they do not decide the rounding of the multiply. Under the owner's ruling of 2026-11-19 a measurement reported only inside a GPL program is not by itself a hardware fact: the column is a **provisional cross-check**. The multipliers rest on the data sheet's own figures (above), and Artemio Urbina's recordings of the same boards (jtcores #559, rung 5) are the hardware source to check them against. | Implementation | codes 0-8 | 9-15 | rounding | | --- | --- | --- | --- | | MAME | m/32 as a float | **silent** | none (float) | | libvgm | m | silent | `signal * m / 2` in C, truncating toward zero, into a 16-bit range | | jt6295 | m | silent | floor of signal x m / 32, a 12-bit result | | FBNeo | 256 / 1.412537545^n, rounded (exact 3 dB steps: 256, 181, 128, 91, 64, 46, 32, 23, 16) | **keeps falling 3 dB a step** (11, 8, 6, 4, 3, 2, 1) | the product divided by 16 | Codes 9-15 are U5 (they are part of C7 with FBNeo's exact 3 dB steps); the rounding of the multiply inside the chip, if it rounds at all before the D/A, is U7. ## Mixing, output width, filtering - **Block diagram (DS p. 54)**: "4-channel voice synthesis circuit" -> 12 bits -> "D/A converter" -> DAO. One 12-bit D/A for four channels. - **The '94 book's product table** lists the MSM6295 with a 12-bit D/A and **"Low-pass filter: None"** (against -24 dB/oct and -40 dB/oct for the parts that have one). DS p. 56: D/A output impedance typ. 15 kΩ, relative error max 20 mV. The application circuit (DS p. 68) puts the low-pass filter outside. **The chip does no interpolation and no filtering**; the data sheet describes none and the product table says so. - **How four 12-bit channels reach one 12-bit D/A is not stated (U6).** One channel at 0 dB already swings "MAX. VDD p-p". Possibilities: a sum clipped to 12 bits, a sum that wraps, each channel scaled down, or a D/A shared in time (the four channels output in turn within each sample and averaged by the board's filter). The emulators all sum without a 12-bit limit: MAME as floats (each channel `signal * m/32 / 2048`, so up to 4.0 for four), libvgm in ints, FBNeo in ints limited only at the host's 16 bits, jt6295 in 14 bits (four 12-bit values cannot overflow 14). - **The board's filter is the board's.** jtcores issue 559 found it decisive for CPS-1's sound against recordings: jotego settled on a single pole at 770 Hz for the 88617A board (from the *Forgotten Worlds* schematics) and a second-order Butterworth at 4 kHz for the 89626A, after an IIR and a 4x FIR interpolator of his own each matched the recordings worse ("the interpolator I was using was removing aliasing too well in comparison with the real PCB"). jt6295's own interpolator has been **off by default since 2024-02-23** (`INTERPOL=0`, commit `9db6dc2`); it was never claimed to be the chip's. FBNeo interpolates linearly only when the frontend's interpolation setting is 3 or more; cubic has been disabled since 2020-08-24, and its comment calls no interpolation the setting whose "recording sounds like PCB re: sf2". So a core outputs, per sample, the four channels' attenuated values and their sum, and the board filters, scales and mixes with its other sound chips.