#!/usr/bin/env python3 """Generate an active-H40 FIFO probe or replay its measured CPU write arrivals. python scripts/run_fifo_release.py rom probe.bin --target vsram Run the oracle without -pal with NMD_STOP=5000000, NMD_TRACE=bus.trace, NMD_TRACE_FROM=2000000, NMD_TRACE_MASK=1, NMD_VDPSIG=signals.txt, NMD_VDPCLK=1, NMD_FIFOSIG=1, NMD_VDPSIG_FROM=2000000, NMD_VDPSIG_TO=5000000, then: python scripts/run_fifo_release.py check bus.trace --signals signals.txt --target vsram The probe keeps display enabled, autoincrement zero and the destination fixed. Replay begins at a confirmed empty FIFO after a physical line origin. Die H=$14A marks that origin; the existing HBlank/status calibration places it 15 MCLK after the board origin. This is not an HV-derived or fitted per-probe phase. The checker compares CPU-clock-rounded holds, including blank lines, and fails on the first mismatch. It does not model the physical FIFO status bits. """ from __future__ import annotations import argparse from bisect import bisect_right from pathlib import Path import nuked_md from run_vscroll_latch import _w, nuked_md_cartridge from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System from megadrive_python.vdp import H40 TARGETS = {"vram": (1, 0x4000, 0), "cram": (3, 0xC000, 0), "vsram": (5, 0x4000, 0x10)} def cartridge(target: str) -> bytes: if target not in TARGETS: raise ValueError(f"unknown FIFO destination: {target}") code = bytearray(_w(0x46FC, 0x2700)) code += bytes.fromhex("23fc5345474100a14000") code += _w(0x41F9, 0x00C0, 0, 0x43F9, 0x00C0, 4, 0x45F9, 0x00C0, 8) for word in (0x8004, 0x8144, 0x8C81, 0x8F00): code += _w(0x32BC, word) code += _w(0x22BC, *TARGETS[target][1:], 0x323C, 0x1234) loop = _w(0x3081, 0x3412) * 32 code += loop + _w(0x6000, (-len(loop) - 2) & 0xFFFF) return nuked_md_cartridge(bytes(code)) def check_trace(events: list[nuked_md.Event], text: str, target: str) -> dict[str, int]: if target not in TARGETS: raise ValueError(f"unknown FIFO destination: {target}") anchors: list[tuple[float, int]] = [] empty: list[float] = [] 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 == "HC" and h == H40.vinc: anchors.append((t / 2, v)) elif name == "FEMPTY": if value not in (0, 1): raise ValueError(f"invalid FEMPTY level: {value}") if value: empty.append(t / 2) if not anchors: raise ValueError("missing physical line origins; enable NMD_VDPCLK=1") start = next((t for t in empty if t > anchors[0][0]), None) if start is None: raise ValueError("missing confirmed empty FIFO after a line origin; enable NMD_FIFOSIG=1") origin, row = anchors[bisect_right([t for t, _ in anchors], start) - 1] vdp = System(MACHINES["genesis"], Cartridge(bytes(0x10000))).vdp vdp.rendering = False vdp.registers[1], vdp.registers[12], vdp.registers[15] = 0x44, 0x81, 0 vdp.code = TARGETS[target][0] vdp.v, vdp.previous_v, vdp.border = row, row - 1, row >= 224 vdp.hmode = vdp.vmode.h40_line(row, False, False) vdp._line_events() vdp._choose_slots() writes = held_writes = blank_writes = 0 for event in events: if (event.t < start or event.type != nuked_md.EV_M68K or not event.by_m68k or event.read or event.addr != 0xC00000): continue if event.size != "w" or event.length < 18: raise ValueError("FIFO probe requires complete word writes") if event.t * 2 > previous: raise ValueError("signal trace does not cover the last write") arrival = event.t - origin + 15 if vdp.mclk > arrival: raise ValueError(f"replay advanced past the next arrival at {arrival}") hold = vdp.write_word(0, event.data, arrival) rounded = -(-hold // 7) * 7 expected = event.length - 18 if rounded != expected: raise ValueError( f"write {writes} at {event.t}: hold {rounded}, expected {expected}" ) writes += 1 held_writes += expected > 0 blank_writes += vdp.v >= 224 if not writes or not held_writes: raise ValueError("probe must exercise FIFO-held writes") return {"writes": writes, "held": held_writes, "blank_writes": blank_writes} def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("command", choices=("rom", "check")) parser.add_argument("path", type=Path) parser.add_argument("--target", choices=TARGETS, required=True) parser.add_argument("--signals", type=Path) args = parser.parse_args() if args.command == "rom": args.path.write_bytes(cartridge(args.target)) else: if args.signals is None: parser.error("check requires --signals") print(check_trace(nuked_md.read_trace(args.path), args.signals.read_text(), args.target)) if __name__ == "__main__": main()