#!/usr/bin/env python3 """Generate and check a horizontal-scroll fetch-gating probe for Nuked-MD. The ROM alternates H-scroll 0/16 on successive lines. On each V-counter change it disables display, waits (V & 7) DBRA iterations, then enables display again. A missed H-scroll request must retain both previous values, not read the current table. python scripts/run_hscroll_latch.py rom probe.bin [--h32] Run the headless referee with NMD_STOP=25089120, NMD_VDPSIG=probe.txt, NMD_VDPSIG_FROM=16128720 and NMD_VDPSIG_TO=25089120, without -pal. Then: python scripts/run_hscroll_latch.py check probe.txt The trace's HA/HB, DISPLAY and HREQUEST signals are independent of the board's renderer. This checks the observed rule, not whether the board implements it. """ from __future__ import annotations import argparse import collections from pathlib import Path from run_vscroll_latch import _w, nuked_md_cartridge def cartridge(h40: bool = True) -> bytes: code = bytearray(_w(0x46FC, 0x2700)) code += bytes.fromhex("23fc5345474100a14000") code += _w(0x41F9, 0x00C0, 0x0000, 0x43F9, 0x00C0, 0x0004, 0x45F9, 0x00C0, 0x0008) for reg, value in ( (0, 4), (1, 4), (2, 0x30), (4, 7), (5, 0x78), (7, 0), (10, 255), (11, 3), (12, 0x81 if h40 else 0), (13, 0x3F), (15, 2), (16, 1), (17, 0), (18, 0), ): code += _w(0x32BC, 0x8000 | reg << 8 | value) code += _w(0x22BC, 0xC000, 0, 0x30BC, 0, 0x30BC, 0xE, 0x30BC, 0xE0) code += _w(0x22BC, 0x4000, 0, 0x303C, 0x7FFF, 0x30BC, 0, 0x51C8, 0xFFFA) code += _w(0x22BC, 0x4020, 0) for colour in (0x1111, 0x2222): code += _w(0x303C, 15, 0x30BC, colour, 0x51C8, 0xFFFA) code += _w(0x22BC, 0x4000, 3) for _ in range(32): code += _w(0x30BC, 1, 0x30BC, 1, 0x303C, 61, 0x30BC, 2, 0x51C8, 0xFFFA) code += _w(0x22BC, 0x7C00, 3) for y in range(256): code += _w(0x30BC, (y & 1) * 16, 0x30BC, (y & 1) * 16) code += _w(0x22BC, 0x4000, 0x10, 0x30BC, 0, 0x30BC, 0) code += _w(0x32BC, 0x8144, 0x7200) loop = bytearray(_w(0x1012, 0xB001, 0x67FA, 0x1200)) loop += _w(0x32BC, 0x8104, 0x3400, 0x0242, 7, 0x51CA, 0xFFFE) loop += _w(0x32BC, 0x8144) loop += _w(0x6000, (-len(loop) - 2) & 0xFFFF) return nuked_md_cartridge(bytes(code + loop)) def check_trace(text: str) -> dict: """Check complete visible lines, excluding the trace's partial first/last lines.""" groups: list[list[tuple[str, int, int, int]]] = [] current: list[tuple[str, int, int, int]] = [] previous_v = None 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}") _, name, value, h, v = fields event = name, int(value), int(h), int(v) if previous_v is not None and event[3] != previous_v: groups.append(current) current = [] current.append(event) previous_v = event[3] a = b = None fetched = retained = 0 phases: collections.Counter[tuple[int, bool]] = collections.Counter() request_h: collections.Counter[int] = collections.Counter() for index, group in enumerate(groups): before = a, b requested = False enabled_at = None for name, value, h, v in group: if name == "HA": a = value elif name == "HB": b = value elif name == "DISPLAY" and value: enabled_at = h elif name == "HREQUEST" and value: requested = True if index and v < 224: request_h[h] += 1 v = group[0][3] if not index or v >= 224: continue if None in (*before, a, b): raise ValueError("trace is missing initial HA/HB latch values") expected = ((v & 1) * 16,) * 2 if requested else before if (a, b) != expected: raise ValueError( f"line {v}: scroll {(a, b)}, expected {expected}; requested={requested}" ) fetched += requested retained += not requested if enabled_at is not None: phases[enabled_at, requested] += 1 if not fetched or not retained: raise ValueError("probe must exercise both successful and missed fetches") if set(request_h) != {0x1E7}: raise ValueError(f"H-scroll requests must occur at H=$1E7, got {dict(request_h)}") if not phases[0x1E6, True] or not phases[0x1E7, False] or phases[0x1E7, True]: raise ValueError( "probe must exercise an H=$1E6 fetch and H=$1E7 miss, with no H=$1E7 fetch" ) return { "fetched": fetched, "retained": retained, "request_h": dict(request_h), "enable_phases": [ (h, requested, count) for (h, requested), count in sorted(phases.items()) ], } def main() -> None: parser = argparse.ArgumentParser(description=__doc__) commands = parser.add_subparsers(dest="command", required=True) rom = commands.add_parser("rom") rom.add_argument("output", type=Path) rom.add_argument("--h32", action="store_true") check = commands.add_parser("check") check.add_argument("trace", type=Path) args = parser.parse_args() if args.command == "rom": args.output.write_bytes(cartridge(not args.h32)) else: result = check_trace(args.trace.read_text()) print( f"fetched {result['fetched']}; retained {result['retained']}; " f"request H {result['request_h']}" ) for h, requested, count in result["enable_phases"]: print(f"enable H=${h:03X}: {'fetch' if requested else 'retain'} {count}") if __name__ == "__main__": main()