"""What games write to the SN76489A: tone periods 0 and 1, and whether they are heard. python scripts/psg_streams.py tests/mame_traces/io-*/error.log [--board-log FILE] Reads ``IW FF data clock`` lines (any port $E0-$FF reaches the chip) from MAME port logs (scripts/mame_io.py) and replays the chip's register file (TI SN76489AN data manual: a byte with bit 7 set latches register rrr and writes its low 4 bits; a byte with bit 7 clear writes the latched tone register's high 6 bits). For each tone channel it reports the time spent with period 0 or 1 while the channel's attenuation is not 15 (off): that is when the two readings of C8 (period 0 held high, or played as 1,024; period 1 held high or toggling) sound different. Channel 2's period also drives the noise when the noise control's rate bits are 3. """ from __future__ import annotations import argparse import sys from collections import Counter from pathlib import Path def stream(path: Path): with path.open("rb") as handle: for line in handle: if line.startswith(b"IW "): _, port, value, clock = line.split() if int(port, 16) >= 0xE0: yield int(value, 16), int(clock) def analyse(events): period = [0x000] * 3 # power-on registers: not documented; zeros, as MAME attenuation = [0xF] * 4 # assume silent until written (the BIOS writes 15 to all four first) noise = 0 latched = 0 writes = 0 low_period_writes = Counter() # (channel, period) -> number of writes leaving it there heard = Counter() # (channel, period) -> CPU clocks heard since = None state = None def audible_low(): out = set() for ch in range(3): if period[ch] <= 1 and attenuation[ch] != 0xF: out.add((ch, period[ch])) if (noise & 3) == 3 and period[2] <= 1 and attenuation[3] != 0xF: out.add(("noise", period[2])) return frozenset(out) for value, clock in events: writes += 1 if since is not None and state: for key in state: heard[key] += clock - since if value & 0x80: latched = (value >> 4) & 7 channel, is_volume = latched >> 1, latched & 1 if is_volume: attenuation[channel] = value & 0xF elif channel < 3: period[channel] = (period[channel] & 0x3F0) | (value & 0xF) else: noise = value & 7 else: channel, is_volume = latched >> 1, latched & 1 if not is_volume and channel < 3: period[channel] = ((value & 0x3F) << 4) | (period[channel] & 0xF) # (a data byte after a volume or noise latch: behaviour not used by these games) if not (value & 0x80 and latched & 1) and (latched >> 1) < 3 and period[latched >> 1] <= 1: low_period_writes[(latched >> 1, period[latched >> 1])] += 1 since, state = clock, audible_low() return writes, low_period_writes, heard def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("logs", nargs="+", type=Path) args = parser.parse_args() totals = Counter() for path in args.logs: writes, low, heard = analyse(stream(path)) title = path.parent.name cells = ", ".join(f"ch{c} p{p}: {n} writes" for (c, p), n in sorted(low.items(), key=str)) or "none" loud = ", ".join(f"{c} p{p}: {n:,} clocks" for (c, p), n in sorted(heard.items(), key=str)) or "never" print(f"{title[:50]:50s} {writes:6d} sound writes; period 0/1 set: {cells}; heard: {loud}") totals["titles"] += 1 totals["with_low"] += bool(low) totals["heard"] += bool(heard) print( f"{totals['titles']} logs; {totals['with_low']} set a period of 0 or 1; " f"{totals['heard']} leave one audible" ) return 0 if __name__ == "__main__": sys.exit(main())