"""Read the console's answers out of the probe pack's save files. python -I scripts/probe_results.py path/to/*.srm Each of our probes (probes/*/) starts its battery RAM with an 8-byte magic, "SNESPRB" and its number, then its payload: 0 revision boot count (16-bit LE), raw $4210, $213E, $213F 1 open_bus 11 raw bytes, one per case (open_bus.s) 2 hv_latch 16 (H, V) pairs latched by $2137, then 16 by WRIO, 16-bit LE 3 clock_ratio timer-2 ticks (24-bit LE) over a frame count (16-bit LE) 4 midframe 6 cases x 3 runs of: where the tag landed (16-bit LE, $FFFF none), changed entries (8-bit), H latched after the write (16-bit LE) 5 poweron 32 KiB: run count; raw $2134-$2136, $213E, $213F, $4210-$4212; two rows of 8 V values after WAI (/IRQ held, then released); WRAM, VRAM, OAM and CGRAM as the program found them (probes/poweron/poweron.s) 6 port_race 8 KiB: the S-CPU/S-SMP port-race grid; decoded by scripts/port_race_results.py (which also compares saves) """ from __future__ import annotations import argparse from collections import Counter from pathlib import Path MASTER_HZ = 21_477_272.727 # NTSC master clock, nominal FRAME_CLOCKS = 357_366 # mean NTSC frame: 262 x 1364, minus 4 every other frame TIMER2_HZ = 64_000 # 24.576 MHz / 384, nominal OPEN_BUS = [ # label, mask, fullsnes's prediction (open_bus.s) ("2100 abs (MDR)", 0xFF, 0x21), ("2100 long (MDR)", 0xFF, 0x00), ("213E, PPU1 = FF", 0x10, 0x10), ("213E, PPU1 = 00", 0x10, 0x00), ("213F, PPU2 = FF", 0x20, 0x20), ("213F, PPU2 = 00", 0x20, 0x00), ("CGRAM high, PPU2 = FF", 0x80, 0x80), ("213E, PPU1 FF then PPU2 00", 0x10, 0x10), ("213F, PPU1 FF then PPU2 00", 0x20, 0x00), ("213E, PPU1 00 then PPU2 FF", 0x10, 0x00), ("213F, PPU1 00 then PPU2 FF", 0x20, 0x20), ] def revision(p: bytes) -> None: count = int.from_bytes(p[0:2], "little") cpu, ppu1, ppu2 = p[2:5] print(f" boot count {count}") print(f" 5A22 version {cpu & 15} ($4210 = ${cpu:02X})") print(f" PPU1 version {ppu1 & 15} ($213E = ${ppu1:02X})") print(f" PPU2 version {ppu2 & 15} ($213F = ${ppu2:02X})") print(f" video {'PAL 50 Hz' if ppu2 & 0x10 else 'NTSC 60 Hz'}") if count < 2: print(" boot count 1: power off and run the probe again to prove saves persist") def open_bus(p: bytes) -> None: for (label, mask, want), got in zip(OPEN_BUS, p, strict=False): verdict = "OK" if got & mask == want else "DIFF" print(f" {label:28} ${got:02X} {verdict}") def hv_latch(p: bytes) -> None: def pairs(block: bytes) -> list[tuple[int, int]]: words = [int.from_bytes(block[i:i + 2], "little") for i in range(0, 64, 2)] return list(zip(words[0::2], words[1::2], strict=True)) for name, block in (("$2137 read", p[0:64]), ("WRIO 1->0", p[64:128])): hs = Counter(h for h, _ in pairs(block)) print(f" H by {name:11} " + ", ".join(f"${h:03X} x{n}" for h, n in sorted(hs.items()))) a = Counter(h for h, _ in pairs(p[0:64])).most_common(1)[0][0] b = Counter(h for h, _ in pairs(p[64:128])).most_common(1)[0][0] print(f" WRIO latches {b - a:+d} dots after $2137 (most common values)") def clock_ratio(p: bytes) -> None: ticks = int.from_bytes(p[0:3], "little") frames = int.from_bytes(p[3:5], "little") nominal = frames * FRAME_CLOCKS / MASTER_HZ * TIMER2_HZ ppm = (ticks / nominal - 1) * 1e6 print(f" ticks {ticks} in {frames} frames; nominal {nominal:.0f}") print(f" S-SMP / master ratio is {ppm:+.0f} ppm off nominal") print(f" (S-SMP at {24.576 * ticks / nominal:.4f} MHz if the master clock is exact)") MIDFRAME = [ # what was written where, and when (midframe.s) ("CGRAM, line 100 dot 100 (active)", 1), ("CGRAM, line 100 dot 280 (H-Blank)", 1), ("CGRAM, line 230 dot 100 (V-Blank)", 1), ("OAM, line 100 dot 100 (active)", 2), ("OAM, line 100 dot 280 (H-Blank)", 2), ("OAM, line 230 dot 100 (V-Blank)", 2), ] def midframe(p: bytes) -> None: print(" tag written to CGRAM colour $080 / OAM byte $080; a whole write changes " "1 colour / 2 bytes") for case, (label, _whole) in enumerate(MIDFRAME): runs = [] for run in range(3): o = (case * 3 + run) * 5 where = int.from_bytes(p[o:o + 2], "little") count, h = p[o + 2], int.from_bytes(p[o + 3:o + 5], "little") spot = "none" if where == 0xFFFF else f"${where:03X}" runs.append(f"{spot}/{count} H${h:03X}") print(f" {label:34} " + " ".join(runs)) def _summary(label: str, block: bytes) -> None: common = Counter(block).most_common(3) zeros = block.count(0) print(f" {label}: {len(block)} bytes, {zeros} zero, most common " + ", ".join(f"${v:02X} x{n}" for v, n in common) + f"; first 16: {block[:16].hex(' ')}") def poweron(p: bytes) -> None: def rows(at: int) -> list[int]: return [p[at + 2 * i - 8] | p[at + 2 * i - 7] << 8 & 0x100 for i in range(8)] print(f" run count {p[0] | p[1] << 8}") names = ("2134", "2135", "2136", "213E", "213F", "4210", "4211", "4212") print(" raw " + " ".join(f"{n}=${p[8 + i]:02X}" for i, n in enumerate(names))) print(f" WAI, /IRQ held (V after each): {rows(0x20)}") print(f" WAI, /IRQ released (control): {rows(0x30)}") print(" held: all near line 10 = WAI resumes while /IRQ stays low (WDC, the board);" " 225s = it waits for a new event (MAME 0.285)") for label, start, size in (("WRAM $7E:0000-$3FFF", 0x100, 0x4000), ("WRAM $7F:F000-$FFFF", 0x4100, 0x1000), ("VRAM words $0000-$03FF", 0x5100, 0x800), ("OAM", 0x5900, 544), ("CGRAM", 0x5C00, 512)): _summary(label, p[start - 8:start - 8 + size]) def port_race(p: bytes) -> None: import sys sys.path.insert(0, str(Path(__file__).resolve().parent)) import port_race_results port_race_results.grid(port_race_results.parse(b"SNESPRB6" + p), " port-race") DECODERS = {b"5": poweron, b"0": revision, b"1": open_bus, b"2": hv_latch, b"3": clock_ratio, b"4": midframe, b"6": port_race} def main() -> None: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("srm", type=Path, nargs="+") for path in parser.parse_args().srm: data = path.read_bytes() decode = DECODERS.get(data[7:8]) if data[:7] == b"SNESPRB" else None print(path.name) if decode is None: print(f" not one of our probe saves: starts {data[:8]!r}") continue decode(data[8:]) if __name__ == "__main__": main()