#!/usr/bin/env python3 """Summarise a ``scripts/nmd_edclk_probe.c`` log of Nuked-MD (docs/worklog.md, Session 9): per full line (two /HSYNC falls 3,420 MCLK apart), in MCLK from /HSYNC's fall, EDCLK's cycles and its slow runs, the V counter's increment, the die's H counter at /HSYNC, and the first and last non-black output of the RGB pins (on megadrive-python's ``vscroll-latch-planeA-*`` carts, plane A from display pixel 0, so the picture's edges). python scripts/nmd_edclk_report.py out-h40.txt [out-h32.txt ...] """ from __future__ import annotations import bisect import sys from collections import Counter from itertools import pairwise def report(path: str) -> None: with open(path) as f: records = [line.split() for line in f] falls = [int(r[1]) for r in records if r[0] == "H" and r[2] == "0"] modes = Counter(" ".join(r[3:]) for r in records if r[0] == "H") edges = [int(r[1]) for r in records if r[0] == "E"] colours = [(int(r[1]), r[2]) for r in records if r[0] == "C"] colour_t = [t for t, _ in colours] vinc = [int(r[1]) for r in records if r[0] == "V"] hcount = [(int(r[1]), int(r[2])) for r in records if r[0] == "K"] hcount_t = [t for t, _ in hcount] rows: Counter[tuple] = Counter() for a, b in pairwise(falls): if b - a != 6840: # half-MCLK: a full line, not a VSYNC serration continue es = edges[bisect.bisect_left(edges, a) : bisect.bisect_left(edges, b)] runs, k = [], 0 while k < len(es) - 1: if es[k + 1] - es[k] > 8: s = k while k < len(es) - 1 and es[k + 1] - es[k] > 8: k += 1 runs.append(((es[s] - a) / 2, k - s)) else: k += 1 before = [t for t in vinc if a - 6840 < t <= a] h = hcount[bisect.bisect_right(hcount_t, a) - 1][1] if hcount else None span = colours[bisect.bisect_left(colour_t, a) : bisect.bisect_left(colour_t, b)] lit = [t for t, c in span if c != "000000"] dark = [t for t, c in span if c == "000000" and lit and t > lit[0]] rows[ ( len(es), tuple(runs), (a - before[-1]) / 2 if before else None, hex(h) if h is not None else None, (lit[0] - a) / 2 if lit else None, (dark[-1] - a) / 2 if dark else None, ) ] += 1 print(path, dict(modes)) for (cycles, runs, vinc_to_hsync, h, on, off), n in rows.most_common(3): print( f" {n} lines: EDCLK {cycles} cycles; slow runs (MCLK after /HSYNC, cycles) {runs};" f" V increment -> /HSYNC {vinc_to_hsync} MCLK; die H at /HSYNC {h};" f" picture {on}..{off} MCLK after /HSYNC" ) if __name__ == "__main__": for name in sys.argv[1:]: report(name)