# SPDX-License-Identifier: MIT """Chip variants as data rows (docs/behaviour.md, rung 3: the output curve). No logic difference between makers or date codes of C012294 is known (U8), so there is one engine; a row carries what may differ: the analogue output curve. """ from __future__ import annotations from dataclasses import dataclass from math import exp @dataclass(frozen=True) class POKEYVariant: """One row: a name and the output transfer curve used by ``POKEY.mix()``. ``dac`` maps a volume code 0-15 to one channel's current, in units of the least significant step; ``mix_curve`` maps the summed current to the output, normalised so that four channels at volume 15 give 1.0. """ name: str dac: tuple[float, ...] mix_curve: tuple[float, ...] # indexed by round(sum * 4), sum of dac values def _linear() -> POKEYVariant: dac = tuple(float(v) for v in range(16)) top = 4 * dac[15] curve = tuple((i / 4) / top for i in range(int(top * 4) + 1)) return POKEYVariant("linear", dac, curve) # B3.9 FITTED to the Altirra manual's words, not to any table (decision 5; # AHRM 5.3 "Volume control"): the two high volume bits are slightly mismatched # against the two low ones, so the steps 3-4, 7-8 and 11-12 are wider; the # summed output is linear up to one channel's range (15) and compresses above # it, two channels at 15 giving about 1.5 times one and the full 60 about twice # one. HIGH_BIT_RATIO and the knee are chosen to meet those three words, nothing # else; another fit meeting them is as good. HIGH_BIT_RATIO = 1.05 # FITTED: weight of bits 2-3 relative to binary; steps 3-4 etc. 1.2x wider _KNEE = 15.0 # FITTED: "nearly linear ... where the volume sum ... is 15 or less" _TAU = 31.17 # FITTED: with _GAIN, f(30) = 1.5 f(15) and f(60) = 2.0 f(15) _GAIN = 19.63 # FITTED def _fitted() -> POKEYVariant: dac = tuple( (v & 3) + HIGH_BIT_RATIO * (v & 12) for v in range(16) ) def curve(s: float) -> float: if s <= _KNEE: return s return _KNEE + _GAIN * (1.0 - exp(-(s - _KNEE) / _TAU)) top = curve(4 * dac[15]) steps = int(4 * dac[15] * 4) + 1 return POKEYVariant("C012294", dac, tuple(curve(i / 4) / top for i in range(steps))) #: The chip, with the fitted output curve. C012294 = _fitted() #: The same engine with a linear output: four codes summed and scaled to 1.0. LINEAR = _linear()