#!/usr/bin/env python # SPDX-License-Identifier: MIT """Ask the Nuked-OPM referee a few clock-level questions and print the answers. python -I scripts/fetch_references.py && sh validation/build_referee.sh python -I scripts/referee_probe.py Phase 0 groundwork: it proves the harness works and records facts the brief leans on (docs/validation.md, "What the referee said"). It is not a test of any core: there is no core yet. Times are in internal cycles (one call of OPM_Clock = two input clocks; 32 cycles = one sample) counted from the end of the referee's reset. """ from __future__ import annotations import struct import os import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent #: The referee: nuked-harness's opm-events, a GPL tool outside this repository, at its #: documented path under EMU_TOOLS_GPL (docs/provenance.md, "History"). TOOL = "tools/nuked/build/opm-events" REFEREE = Path(os.environ["EMU_TOOLS_GPL"]) / TOOL if os.environ.get("EMU_TOOLS_GPL") else None ADDRESS, DATA, IC = 0, 1, 2 SO, SH1, SH2, IRQ, CT1, CT2 = 1, 2, 4, 8, 16, 32 def run(events: list[tuple[int, int, int]], cycles: int, chip: str = "ym2151"): """Return a list of (pins, status, left, right), one per internal cycle.""" with tempfile.TemporaryDirectory(dir=ROOT / "reference") as scratch: source = Path(scratch) / "events.bin" target = Path(scratch) / "out.bin" source.write_bytes(b"".join(struct.pack(" list[tuple[int, int, int]]: """An address write at `at` and its data write `gap` cycles later.""" return [(at, ADDRESS, register), (at + gap, DATA, value)] def spans(rows, mask: int, column: int = 0) -> list[tuple[int, int]]: """(first cycle, length) of every run of cycles where the bit is set.""" found, start = [], None for cycle, row in enumerate(rows): on = bool(row[column] & mask) if on and start is None: start = cycle if not on and start is not None: found.append((start, cycle - start)) start = None if start is not None: # still set when the run ends found.append((start, len(rows) - start)) return found def probe_busy() -> None: print("BUSY (status bit 7) after one write:") for phase in range(0, 32, 5): at = 320 + phase rows = run([(at, ADDRESS, 0x20)], 600) after_address = spans(rows, 0x80, 1) rows = run([(at, DATA, 0x00)], 600) after_data = spans(rows, 0x80, 1) print( f" write before cycle {at} (slot {at % 32:2d}): " f"address write -> {after_address}, data write -> {after_data}" ) def probe_frame() -> None: rows = run([], 96) print("SH1/SH2 with no writes, cycles 0-95 (first cycle, length):") print(f" SH1 high: {spans(rows, SH1)}") print(f" SH2 high: {spans(rows, SH2)}") print(f" SO, cycles 32-63: {''.join(str(row[0] & SO) for row in rows[32:64])}") print(f" decoded (left, right) at cycle 95: {rows[95][2:]}") def probe_timers() -> None: print("timers: first cycle with the status flag after LOAD (data write before cycle 400):") cases = [ ("A, NA=1023", [*write(100, 0x10, 0xFF), *write(200, 0x11, 0x03), *write(360, 0x14, 0x05)], 1), ("A, NA=1022", [*write(100, 0x10, 0xFF), *write(200, 0x11, 0x02), *write(360, 0x14, 0x05)], 1), ("A, NA=1000", [*write(100, 0x10, 0xFA), *write(200, 0x11, 0x00), *write(360, 0x14, 0x05)], 1), ("B, NB=255", [*write(100, 0x12, 0xFF), *write(360, 0x14, 0x0A)], 2), ("B, NB=254", [*write(100, 0x12, 0xFE), *write(360, 0x14, 0x0A)], 2), ] for chip in ("ym2151", "ym2164"): for name, events, bit in cases: rows = run(events, 3000 if bit == 1 else 70000, chip) flags = spans(rows, bit, 1) irq = spans(rows, IRQ) first = flags[0][0] if flags else None first_irq = irq[0][0] if irq else None delay = None if first is None else first - 400 print( f" {chip} timer {name}: flag at cycle {first} " f"({delay} cycles = {None if delay is None else delay * 2} input clocks after LOAD), " f"IRQ at {first_irq}" ) # The period, from a free-running timer A whose flag is cleared each time. events = [*write(100, 0x10, 0xFA), *write(200, 0x11, 0x00), *write(360, 0x14, 0x05)] rows = run(events, 3000) first = spans(rows, 1, 1)[0][0] events += write(first + 10, 0x14, 0x15) rows = run(events, 3000) flags = spans(rows, 1, 1) print(f" ym2151 timer A, NA=1000, flag cleared once: flag runs {flags[:3]}") if len(flags) > 1: gap = flags[1][0] - flags[0][0] print(f" distance between the two overflows: {gap} cycles = {gap * 2} input clocks") def probe_ct() -> None: print("CT pins after writing register 0x1B (as opm.h names them):") for chip in ("ym2151", "ym2164"): for value in (0x40, 0x80): rows = run(write(100, 0x1B, value), 300, chip) pins = rows[-1][0] print( f" {chip} 0x1B = 0x{value:02X}: CT1 = {int(bool(pins & CT1))}, " f"CT2 = {int(bool(pins & CT2))}" ) def probe_tone() -> None: """One sine carrier, loudest and then 6 dB steps down: what the float format keeps.""" print("one operator (channel 0, C2), CON 7, both outputs; peak of the decoded output by TL:") for total_level in (0, 8, 16, 32, 48, 64, 80, 96, 112, 127): events = [] at = 100 for register, value in ( (0x20, 0xC7), # RL both, FB 0, CON 7 (0x28, 0x4A), # KC: octave 4, note 10 (0x30, 0x00), # KF (0x58, 0x01), # DT1 0, MUL 1 (C2 of channel 0 is register slot 0x18) (0x78, total_level), (0x98, 0x1F), # KS 0, AR 31 (0xB8, 0x00), (0xD8, 0x00), (0xF8, 0x0F), (0x08, 0x40), # key on, C2 only ): events += write(at, register, value) at += 100 rows = run(events, 32 * 1200) tail = rows[32 * 400 :] lefts = {row[2] for row in tail} print( f" TL {total_level:3d}: left min {min(lefts):6d} max {max(lefts):6d}, " f"{len(lefts)} distinct values" ) def main() -> int: if REFEREE is None or not REFEREE.exists(): print(f"the referee is missing: set EMU_TOOLS_GPL so that $EMU_TOOLS_GPL/{TOOL} exists " "(sh validation/build_referee.sh checks it)", file=sys.stderr) return 3 probe_frame() probe_busy() probe_timers() probe_ct() probe_tone() return 0 if __name__ == "__main__": sys.exit(main())