#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run lockstep scripts (scripts/lockstep_gen.py, scripts/export_streams.py) on ay8910-python and print the trace that examples/ay8910-trace.rs prints for this crate. external/venv/bin/python scripts/lockstep_ref.py SCRIPT [SCRIPT ...] [--out-dir DIR] With one script and no --out-dir the trace goes to stdout; otherwise each script's trace is written to DIR/.trace. The script format is scripts/lockstep_gen.py's docstring. Trace lines: r V / rr V read() / read_register(R) P REGISTER VALUE a listened port's out callable called (14 A, 15 B) tick N HASH N x (tick(); levels()), the polled levels hashed take N HASH take_outputs(): how many, and every one hashed samples N HASH take_samples(): how many, and every float's bits hashed mix BITS / levels A B C mix() as the bits of its float; levels() state k=v ... every attribute (the port callables aside) constants / variant / table CLOCKS_PER_STEP and the phases; every part; the DAC tables save nothing here (the Rust side may round-trip a save state) A hash is the first 8 bytes of SHA-256, in hex, over the levels as bytes (a, b, c per clock) or over the floats as little-endian doubles. In "mode full" a tick or take line lists the levels themselves instead. """ from __future__ import annotations import hashlib import struct import sys from itertools import chain from pathlib import Path from ay8910_python import ( AY8910, AY8912, AY8913, CLOCKS_PER_STEP, COUNT_PHASE, DAC_5B, DAC_AY_LOGARITHMIC, DAC_AY_MEASURED, DAC_YM_DATASHEET, DAC_YM_MEASURED, PSG, STROBE_PHASE, SUNSOFT_5B, VARIANTS, YM2149, ) PARTS = {"AY8910": AY8910, "AY8912": AY8912, "AY8913": AY8913, "YM2149": YM2149, "SUNSOFT_5B": SUNSOFT_5B} def digest(data: bytes) -> str: return hashlib.sha256(data).digest()[:8].hex() def level_bytes(levels) -> bytes: return bytes(chain.from_iterable(levels)) def level_list(levels) -> str: return "".join(f" {a},{b},{c}" for a, b, c in levels) def bits(x: float) -> str: return f"{struct.unpack(' str: return ",".join(str(int(v)) for v in values) def state(chip: PSG) -> str: return ( f"state variant={chip.variant.name.replace(' ', '_')} record={int(chip.record)} " f"envelope_top={chip._envelope_top} driven={join(chip._driven)} " f"registers={''.join(f'{r:02X}' for r in chip.registers)} address={chip.address} " f"selected={int(chip.selected)} bus={chip.bus} clock_divider={chip.clock_divider} " f"divider_count={chip._divider_count} phase={chip.phase} " f"tone_period={join(chip.tone_period)} tone_count={join(chip.tone_count)} " f"tone_held={join(chip.tone_held)} tone_output={join(chip.tone_output)} " f"noise_period={chip.noise_period} noise_count={chip.noise_count} " f"noise_held={int(chip.noise_held)} noise_half={chip.noise_half} " f"noise_register={chip.noise_register} envelope_period={chip.envelope_period} " f"envelope_count={chip.envelope_count} envelope_held={int(chip.envelope_held)} " f"envelope_half={chip.envelope_half} envelope_step={chip.envelope_step} " f"envelope_rising={int(chip.envelope_rising)} " f"envelope_holding={int(chip.envelope_holding)} envelope_level={chip.envelope_level} " f"clock={chip.clock} queued={len(chip.outputs)} levels={join(chip._levels)}" ) def tables(out: list[str]) -> None: out.append(f"constants {CLOCKS_PER_STEP} {STROBE_PHASE} {COUNT_PHASE}") for v in VARIANTS.values(): out.append( f"variant {v.name}|{v.envelope_steps}|{v.ports}|{join(v.register_masks)}|" f"{v.clock_divider}|{int(v.has_sel_pin)}|{v.codes}|{','.join(bits(x) for x in v.dac)}" ) for name, table in ( ("DAC_AY_MEASURED", DAC_AY_MEASURED), ("DAC_AY_LOGARITHMIC", DAC_AY_LOGARITHMIC), ("DAC_YM_DATASHEET", DAC_YM_DATASHEET), ("DAC_YM_MEASURED", DAC_YM_MEASURED), ("DAC_5B", DAC_5B), ): out.append(f"table {name} {','.join(bits(x) for x in table)}") def trace(lines: list[str]) -> list[str]: out: list[str] = [] head = lines[0].split() chip = PSG(PARTS[head[1]], record=not (len(head) > 3 and head[3] == "0")) full = lines[1].split()[1] == "full" pins = [None, None] def listener(register: int): return lambda value: out.append(f"P {register} {value}") def outside(port: int): return lambda: pins[port] for line in lines[2:]: op, *args = line.split() or [""] if op == "s": chip.select(int(args[0])) elif op == "d": chip.write(int(args[0])) elif op == "w": chip.write_register(int(args[0]), int(args[1])) elif op == "r": out.append(f"r {chip.read()}") elif op == "rr": out.append(f"rr {chip.read_register(int(args[0]))}") elif op in ("pa", "pb"): port = 0 if op == "pa" else 1 pins[port] = None if args[0] == "-" else int(args[0]) # unset: no callable at all, as a board that never set one callable_ = None if pins[port] is None else outside(port) if port == 0: chip.port_a_in = callable_ else: chip.port_b_in = callable_ elif op == "la": chip.port_a_out = listener(14) if args[0] == "1" else None elif op == "lb": chip.port_b_out = listener(15) if args[0] == "1" else None elif op == "div": chip.clock_divider = int(args[0]) elif op == "reset": chip.reset() elif op == "tick": n = int(args[0]) polled = [] tick, levels = chip.tick, chip.levels for _ in range(n): tick() polled.append(levels()) if full: out.append(f"tick {n}{level_list(polled)}") else: out.append(f"tick {n} {digest(level_bytes(polled))}") elif op == "run": chip.run(int(args[0])) elif op == "take": taken = chip.take_outputs() if full: out.append(f"take {len(taken)}{level_list(taken)}") else: out.append(f"take {len(taken)} {digest(level_bytes(taken))}") elif op == "samples": values = chip.take_samples() out.append(f"samples {len(values)} {digest(struct.pack(f'<{len(values)}d', *values))}") elif op == "mix": out.append(f"mix {bits(chip.mix())}") elif op == "levels": a, b, c = chip.levels() out.append(f"levels {a} {b} {c}") elif op == "state": out.append(state(chip)) elif op == "tables": tables(out) elif op == "save": out.append("save") elif op == "": pass else: raise SystemExit(f"unknown op {op}") return out def main(argv: list[str]) -> None: out_dir = None scripts = [] i = 0 while i < len(argv): if argv[i] == "--out-dir": out_dir = Path(argv[i + 1]) i += 2 else: scripts.append(Path(argv[i])) i += 1 for script in scripts: result = "\n".join(trace(script.read_text().splitlines())) + "\n" if out_dir is None and len(scripts) == 1: sys.stdout.write(result) else: (out_dir or Path(".")).joinpath(script.stem + ".trace").write_text(result) if __name__ == "__main__": main(sys.argv[1:])