#!/usr/bin/env python # SPDX-License-Identifier: MIT """Expand IKAOPM's log-sine and exponent ROMs into 256-entry tables and compare them with ymfm's die-extracted OPN tables. python -I scripts/fetch_references.py python -I scripts/ikaopm_tables.py [--dump] IKAOPM (Sehyeon Kim, BSD-2-Clause, commit 08a5b40) stores each table as 32 rows of 46 (log-sine) or 45 (exponent) bits, traced from the YM2151 die; an entry is the sum of two bit fields chosen from one row by the index's top two bits, the second field used only for odd (log-sine) or even (exponent) indices (IKAOPM_op.v:91-144 and :192-250, the ROMs at :559-664). The rows and the bit selections are read from IKAOPM's source at run time; nothing of it is copied here. The expansion is what docs/operators.md calls DIE, and rung 0 of the core compares the core's tables with this script's `--dump` output. ymfm (Aaron Giles, BSD-3-Clause, commit 81aec25) carries tables "extracted from the die" of an OPN (ymfm_fm.ipp:46-135); they are parsed at run time too. That two independent die readings of two related chips agree is evidence, not proof, for the YM2151's tables. Notices: NOTICE. """ from __future__ import annotations import argparse import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent IKAOPM_OP = ROOT / "reference" / "src" / "IKAOPM" / "src" / "IKAOPM_modules" / "IKAOPM_op.v" YMFM_FM = ROOT / "reference" / "src" / "ymfm" / "src" / "ymfm_fm.ipp" def rom_rows(text: str, module: str, width: int) -> list[int]: body = text.split(f"module {module}", 1)[1].split("endmodule", 1)[0] rows = re.findall(rf"5'd\s*(\d+)\s*:\s*o_DATA <= {width}'b([01_]+);", body) table = [0] * 32 for index, bits in rows: table[int(index)] = int(bits.replace("_", ""), 2) if len(rows) != 32: raise SystemExit(f"{module}: expected 32 rows, found {len(rows)}") return table def selections(text: str, name: str) -> list[list[str]]: """The four bit-selection lists (MSB first) assigned to `name`, by index bits 7-6.""" found = re.findall(rf"2'd(\d): {name} = \{{([^}}]*)\}}", text) lists = [None] * 4 for sel, body in found: lists[int(sel)] = [item.strip() for item in body.split(",")] if any(x is None for x in lists): raise SystemExit(f"could not read the selections for {name}") return lists def field(row: int, items: list[str], letter: str) -> int: value = 0 for item in items: value <<= 1 if item == "1'b1": value |= 1 elif item != "1'b0": bit = int(re.fullmatch(rf"{letter}\[(\d+)\]", item).group(1)) value |= (row >> bit) & 1 return value def expand() -> tuple[list[int], list[int]]: text = IKAOPM_OP.read_text() sin_rows = rom_rows(text, "op_submdl_logsinrom", 46) exp_rows = rom_rows(text, "op_submdl_exprom", 45) s0 = selections(text, "cyc43c_logsinrom_addend0") s1 = selections(text, "cyc43c_logsinrom_addend1") e0 = selections(text, "cyc47c_exprom_addend0") e1 = selections(text, "cyc47c_exprom_addend1") logsin, mantissa = [], [] for index in range(256): row, sel = (index >> 1) & 31, index >> 6 # second field: odd indices for the log-sine ROM (:129-132) extra = field(sin_rows[row], s1[sel], "ls") if index & 1 else 0 logsin.append(field(sin_rows[row], s0[sel], "ls") + extra) # second field: even indices for the exponent ROM (:202, :233-236) extra = field(exp_rows[row], e1[sel], "e") if not index & 1 else 0 mantissa.append((field(exp_rows[row], e0[sel], "e") + extra) & 0x3FF) return logsin, mantissa def ymfm() -> tuple[list[int], list[int]]: text = YMFM_FM.read_text() block = text.split("s_sin_table[256] =", 1)[1].split("};", 1)[0] sin = [int(x, 16) for x in re.findall(r"0x([0-9a-f]+)", block)] block = text.split("s_power_table[256] =", 1)[1].split("};", 1)[0] power = [int(x, 16) for x in re.findall(r"X\(0x([0-9a-f]+)\)", block)] return sin, power def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--dump", action="store_true", help="print both expanded tables") args = parser.parse_args(argv) logsin, mantissa = expand() sin, power = ymfm() print("IKAOPM 08a5b40 ROMs expanded (DIE) against ymfm 81aec25's OPN die tables") # IKAOPM addresses its log-sine ROM with the quarter phase inverted when # phase bit 8 is clear (IKAOPM_op.v:74-75), so its address a holds the # attenuation of quarter-wave position 255 - a, the order ymfm lists. same = sum(1 for i in range(256) if logsin[255 - i] == sin[i]) print(f" log-sine, 256 quarter-wave entries (IKAOPM address = 255 - position): {same} equal; " f"range {min(logsin)}..{max(logsin)} (4.8 log units), rising with the address: " f"{all(a <= b for a, b in zip(logsin, logsin[1:]))}") for i in [i for i in range(256) if logsin[255 - i] != sin[i]][:8]: print(f" position {i}: IKAOPM {logsin[255 - i]}, ymfm {sin[i]}") same = sum(1 for a, b in zip(mantissa, power) if a == b) print(f" exponent, 256 mantissa entries (10 bits, implied leading 1): {same} equal; " f"IKAOPM range {min(mantissa)}..{max(mantissa)}") for i in [i for i in range(256) if mantissa[i] != power[i]][:8]: print(f" index {i}: IKAOPM {mantissa[i]}, ymfm {power[i]}") if args.dump: print("logsin", logsin) print("mantissa", mantissa) return 0 if __name__ == "__main__": sys.exit(main())