#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Check the tables rung 0 will build against their documents and against ymfm. python3 -I validation/check_tables.py 1. FM detune: ymfm's 32 x 4 table (reference/src/ymfm/src/ymfm_fm.ipp, BSD-3, the s_detune_adjustment rows below) against the YM2608 manual's Table 2-6 (YM2608J_Translated.pdf p. 26, values in Hz at phi M = 8 MHz, "same as above" arrows expanded). One unit of phase step is phi M / 144 / 2**20 Hz. 2. FM log-sine and exponent: ymfm's two 256-entry tables (ymfm_fm.ipp, read from the pinned file) against the closed forms round(-log2(sin((i + 1/2) * pi / 512)) * 256) and round(2 ** ((255 - i) / 256) * 1024) - 1024 (ymfm stores this one reversed). 3. ADPCM-A steps: floor(16 * 1.1**n), n = 0..48, in exact rationals, against Dialogic's printed list (00-1366-001, 1988, as cited in msm6295-python's docs/decoder.md) and ymfm's s_steps (ymfm_adpcm.cpp). Exit status 0 when every comparison holds. """ from __future__ import annotations import math import re import sys from fractions import Fraction from pathlib import Path ROOT = Path(__file__).resolve().parent.parent YMFM = ROOT / "reference" / "src" / "ymfm" / "src" # YM2608 manual Table 2-6 (p. 26): (block, note) -> Hz for FD = 1, 2, 3; None = "same as above" MANUAL_DETUNE = { (0, 0): (0.000, 0.053, 0.106), (0, 1): None, (0, 2): None, (0, 3): None, (1, 0): (0.053, 0.106, 0.106), (1, 1): (0.053, 0.106, 0.159), (1, 2): None, (1, 3): None, (2, 0): (0.053, 0.106, 0.212), (2, 1): (0.053, 0.159, 0.212), (2, 2): None, (2, 3): (0.053, 0.159, 0.264), (3, 0): (0.106, 0.212, 0.264), (3, 1): (0.106, 0.212, 0.317), (3, 2): None, (3, 3): (0.106, 0.264, 0.370), (4, 0): (0.106, 0.264, 0.423), (4, 1): (0.159, 0.317, 0.423), (4, 2): (0.159, 0.317, 0.476), (4, 3): (0.159, 0.370, 0.529), (5, 0): (0.212, 0.423, 0.582), (5, 1): (0.212, 0.423, 0.635), (5, 2): (0.212, 0.476, 0.688), (5, 3): (0.264, 0.529, 0.741), (6, 0): (0.264, 0.582, 0.846), (6, 1): (0.317, 0.635, 0.899), (6, 2): (0.317, 0.688, 1.005), (6, 3): (0.370, 0.741, 1.058), (7, 0): (0.423, 0.846, 1.164), (7, 1): None, (7, 2): None, (7, 3): None, } DIALOGIC_STEPS = [ 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, ] def c_table(source: str, name: str) -> list[int]: body = re.search(name + r"(?:\[\d+\])+\s*=\s*\{(.*?)\};", source, re.S).group(1) body = re.sub(r"//[^\n]*", "", body).replace("X(", "(") return [int(x, 0) for x in re.findall(r"0x[0-9a-fA-F]+|\d+", body)] def main() -> int: failures = 0 fm = (YMFM / "ymfm_fm.ipp").read_text() adpcm = (YMFM / "ymfm_adpcm.cpp").read_text() rows = c_table(fm, "s_detune_adjustment") detune = [rows[i:i + 4] for i in range(0, 128, 4)] unit = 8e6 / 144 / 2**20 last = None bad = 0 for (block, note), hz in sorted(MANUAL_DETUNE.items()): hz = hz or last last = hz for fd in (1, 2, 3): if round(hz[fd - 1] / unit) != detune[block * 4 + note][fd]: bad += 1 print(f"detune: ymfm vs YM2608 manual Table 2-6: {96 - bad} of 96 agree (unit {unit:.5f} Hz)") failures += bad != 0 sin_t = c_table(fm, "s_sin_table") pow_t = c_table(fm, "s_power_table") sin_f = [round(-math.log2(math.sin((i + 0.5) * math.pi / 512)) * 256) for i in range(256)] pow_f = [round(2 ** ((255 - i) / 256) * 1024) - 1024 for i in range(256)] s_ok = sum(a == b for a, b in zip(sin_t, sin_f)) p_ok = sum(a == b for a, b in zip(pow_t, pow_f)) print(f"log-sine: ymfm vs closed form: {s_ok} of 256 agree") print(f"exponent: ymfm vs closed form: {p_ok} of 256 agree") failures += (s_ok != 256) + (p_ok != 256) steps = [math.floor(16 * Fraction(11, 10) ** n) for n in range(49)] ymfm_steps = c_table(adpcm, "s_steps") print(f"ADPCM-A steps: floor(16 * 1.1**n) == Dialogic: {steps == DIALOGIC_STEPS}; " f"== ymfm: {steps == ymfm_steps}") failures += (steps != DIALOGIC_STEPS) + (steps != ymfm_steps) return 1 if failures else 0 if __name__ == "__main__": sys.exit(main())