# SPDX-License-Identifier: MIT """Measure line and frame timing from the pinned sigrok captures of real SNK 68000 boards. python -I scripts/sync_capture.py Reads reference/captures/IKARI3_20MHz.sr (Ikari III, A7007: probes SYNC, VBL, HBL, CLK at 20 MHz) and POW_CSYNC_50MHz.sr (P.O.W., A7008: composite sync on probe 0 at 50 MHz), both recorded by the va7deo developers and pinned in scripts/pinned_files.json. A .sr file is a zip with a `metadata` text and raw `logic-1-N` sample chunks; nothing in it is executed. Prints, per signal, the rising-edge count and the median period in samples and in Hz; for composite sync, the spacing of the long (vertical) gaps in lines. """ from __future__ import annotations import array import sys import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CAPTURES = ROOT / "reference" / "captures" def samples(path: Path) -> tuple[array.array, float, list[str], int]: with zipfile.ZipFile(path) as archive: meta = archive.read("metadata").decode() chunks = sorted(n for n in archive.namelist() if n.startswith("logic-1-")) raw = b"".join(archive.read(n) for n in chunks) rate = probes = unit = None names = [] for line in meta.splitlines(): key, _, value = line.partition("=") if key == "samplerate": number, unit_name = value.split() rate = float(number) * {"kHz": 1e3, "MHz": 1e6, "GHz": 1e9}[unit_name] elif key == "unitsize": unit = int(value) elif key == "total probes": probes = int(value) elif key.startswith("probe"): names.append(value) data = array.array("B" if unit == 1 else "H") data.frombytes(raw) return data, rate, names[:probes], unit def rising(data: array.array, bit: int) -> list[int]: edges, previous = [], data[0] >> bit & 1 for index in range(1, len(data)): value = data[index] >> bit & 1 if value != previous: if value: edges.append(index) previous = value return edges def report(path: Path, bits: range) -> None: data, rate, names, _ = samples(path) print(f"{path.name}: {len(data)} samples at {rate / 1e6:g} MHz ({len(data) / rate:.4f} s)") for bit in bits: edges = rising(data, bit) if len(edges) < 3: continue periods = sorted(b - a for a, b in zip(edges, edges[1:])) median = periods[len(periods) // 2] rate_edges = len(edges) / (len(data) / rate) print(f" {names[bit]:12} {len(edges):8} edges median {median} samples = " f"{rate / median:.6g} Hz (edges per second {rate_edges:.6g})") # fmt: skip line = None if median and periods[-1] > 3 * median: # composite sync: find the vertical gaps line = median gaps = [b for a, b in zip(edges, edges[1:]) if b - a > 3 * line] spacing = [b - a for a, b in zip(gaps, gaps[1:])] if spacing: print(f" vertical gaps every {spacing[len(spacing) // 2]} samples = " f"{spacing[len(spacing) // 2] / line:.4f} lines = " f"{rate / spacing[len(spacing) // 2]:.6g} Hz") # fmt: skip def main() -> int: ikari = CAPTURES / "IKARI3_20MHz.sr" pow_ = CAPTURES / "POW_CSYNC_50MHz.sr" missing = [p.name for p in (ikari, pow_) if not p.exists()] if missing: print("run scripts/fetch_references.py first; missing:", *missing, file=sys.stderr) return 2 report(ikari, range(4)) report(pow_, range(1)) return 0 if __name__ == "__main__": sys.exit(main())