#!/usr/bin/env python3 """Check display-register propagation against CPU writes and observed pixel clocks. Generate either ROM with run_hscroll_latch.py. Run the headless oracle without -pal with NMD_STOP=17920800, NMD_TRACE=bus.trace, NMD_TRACE_MASK=1, NMD_TRACE_FROM=16128720, NMD_VDPSIG=signals.txt, NMD_VDPCLK=1, NMD_VDPSIG_FROM=16128720 and NMD_VDPSIG_TO=17920800. Then: python scripts/check_register_latch.py bus.trace signals.txt For ordinary 18-MCLK register writes, capture occurs at the first half-pixel edge at least 20 MCLK after /AS falls; the register changes at the following HCLK1 edge. Keep signal times in half-MCLK: the H32 run has half-integer MCLK edges. This checks the measured pipeline, not the board's implementation. Both traces must cover the same writes, with initial display state and clocks through the last commit. Add --hscroll to check visible H-scroll requests against the predicted display commits, at the HCLK2 rising edge with H=$1E7. This covers stable mode-5 H32/H40 traces, V=0..223; it does not check the later VRAM fetch or scroll-data latches. """ from __future__ import annotations import argparse from bisect import bisect_left, bisect_right from collections import Counter from pathlib import Path import nuked_md def check_trace(events: list[nuked_md.Event], text: str, *, hscroll: bool = False) -> dict: edges: list[int] = [] h1: list[int] = [] displays: list[tuple[int, int]] = [] clock_names: set[str] = set() gates: list[tuple[int, int]] = [] requests: list[tuple[int, int]] = [] previous = -1 for line in text.splitlines(): fields = line.split() if len(fields) == 3 and fields[1] == "L178": continue if len(fields) != 5: raise ValueError(f"malformed VDP signal record: {line!r}") timestamp, name, value, h, v = fields t, value, h, v = int(timestamp), int(value), int(h), int(v) if t < previous: raise ValueError("VDP signal times must be chronological") previous = t if name in ("HCLK1", "HCLK2", "DISPLAY") and value not in (0, 1): raise ValueError(f"invalid {name} level: {value}") if name == "DISPLAY": displays.append((t, value)) elif name in ("HCLK1", "HCLK2"): clock_names.add(name) if value: edges.append(t) if name == "HCLK1": h1.append(t) elif h == 0x1E7 and v < 224: gates.append((t, v)) elif hscroll and name == "HREQUEST": if value not in (0, 1): raise ValueError(f"invalid HREQUEST level: {value}") if value and v < 224: requests.append((t, v)) if clock_names != {"HCLK1", "HCLK2"} or not edges or not h1: raise ValueError("missing pixel-clock evidence; enable NMD_VDPCLK=1") if not displays: raise ValueError("missing initial display state") writes = sorted( (e for e in events if e.type == nuked_md.EV_M68K and e.by_m68k and not e.read and e.addr in (0xC00004, 0xC00006) and e.data >> 8 == 0x81), key=lambda e: e.t, ) index = 1 state = displays[0][1] commits = [displays[0]] delays: Counter[int] = Counter() for write in writes: if write.length != 18 or write.size != "w": raise ValueError("checker requires ordinary 18-MCLK word writes") if write.t * 2 <= displays[0][0]: raise ValueError("missing initial display state before the first write") capture = bisect_left(edges, (write.t + 20) * 2) if capture == len(edges): raise ValueError("missing capture clock after register write") commit = bisect_right(h1, edges[capture]) if commit == len(h1): raise ValueError("missing commit clock after register capture") wanted = bool(write.data & 0x40) if wanted == state: continue expected = h1[commit], int(wanted) if index == len(displays) or displays[index] != expected: actual = displays[index] if index < len(displays) else None raise ValueError(f"write at {write.t}: display {actual}, expected {expected}") delays[expected[0] - write.t * 2] += 1 state = wanted commits.append(expected) index += 1 if index != len(displays): raise ValueError("unmatched display transition") if index < 3: raise ValueError("probe must exercise both display transitions") result = { "writes": len(writes), "transitions": index - 1, "delay_half_mclk": dict(sorted(delays.items())), } if hscroll: if not gates or gates[0][0] <= commits[0][0]: raise ValueError("missing H-scroll gate clocks or preceding display state") commit_times = [t for t, _ in commits] predicted = [ (t, v) for t, v in gates if commits[bisect_left(commit_times, t) - 1][1] ] if predicted != requests: missing = sorted(set(predicted) - set(requests)) unexpected = sorted(set(requests) - set(predicted)) raise ValueError( f"H-scroll requests differ: missing={missing[:3]}, unexpected={unexpected[:3]}" ) result["hscroll"] = { "gates": len(gates), "fetched": len(predicted), "retained": len(gates) - len(predicted), } return result def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("bus", type=Path) parser.add_argument("signals", type=Path) parser.add_argument("--hscroll", action="store_true") args = parser.parse_args() print(check_trace(nuked_md.read_trace(args.bus), args.signals.read_text(), hscroll=args.hscroll)) if __name__ == "__main__": main()