#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """examples/speed.rs on ay8910-python: chip time over wall time, polled as a board polls it and through run() and take_samples(). nice external/venv/bin/python scripts/speed_ref.py [seconds, default 5] """ import sys import time from ay8910_python import AY8910, PSG, SUNSOFT_5B FAMICOM_HZ = 1_789_773 SPECTRUM_HZ = 1_773_400 WRITES = ( (0, 0xFE), (1, 0), (2, 0x40), (3, 1), (4, 0x9C), (5, 0), (6, 7), (7, 0x30), (8, 12), (9, 0x10), (10, 9), (11, 0x20), (12, 0), (13, 0x0E), ) # fmt: skip def sounding(variant, record): chip = PSG(variant, record=record) for register, value in WRITES: chip.write_register(register, value) return chip seconds = int(sys.argv[1]) if len(sys.argv) > 1 else 5 chip = sounding(SUNSOFT_5B, False) start = time.perf_counter() last, level, changes = chip.levels(), 0.0, 0 tick, levels = chip.tick, chip.levels for _ in range(seconds * FAMICOM_HZ): tick() now = levels() if now is not last: last = now level = chip.mix() changes += 1 wall = time.perf_counter() - start print(f"Sunsoft 5B, polled: {seconds} s of chip time in {wall:.3f} s: {seconds / wall:.1f} x real time ({changes} changes, last level {level:.3f})") chip = sounding(AY8910, True) start = time.perf_counter() heard = 0 for _ in range(seconds): chip.run(SPECTRUM_HZ) heard += sum(1 for v in chip.take_samples() if v) wall = time.perf_counter() - start print(f"AY-3-8910, run + take_samples: {seconds} s of chip time in {wall:.3f} s: {seconds / wall:.1f} x real time ({heard} non-zero samples)")