"""Per-frame margins on the board-matched power-on attract trajectory (paper analysis, 2026-10-16). Question: the P1 board's running counters (first return) read OVR3 = 32 at game frame ~5653 and 41 at ~7175, while the model (build26, same probe image, unarmed, power-on) has 23 and 31 at the same frames, with the first overrun on the same frame (488). Which model frames are near misses that the model's known pass deficit (about 1.8 %) would turn into board overruns? Reuses the census runner census_run.py (ROM patching, loader pin, hidden muted launch) with margin_probe.lua = census_probe.lua plus a read-only copy of the probe's current record every frame. Writes frame_board_paper_20261016//{census.tsv,records.tsv,cur.tsv,status.txt,report.json}. The census.tsv of this run must equal slowdown_census_20261015/attract_probe_b26 frame for frame (the added reads go through the 22000000 mirror); the script checks that and records the result. usage: python margin_run.py --out NAME [--frames 9000] """ import argparse, csv, hashlib, json, os, shutil, sys from pathlib import Path sys.dont_write_bytecode = True HERE = Path(__file__).resolve().parent AF = HERE.parents[1] sys.path.insert(0, str(AF / 'slowdown_census_20261015')) import census_run as cr EXE = AF / 'emulator_board_20261013/p2_build26/x86.exe' EXE_SHA = '8b1bff0518b2bc5c4d9d47f38748798b9a79fc191eb11396059ae27a4a7820ce' REFERENCE = AF / 'slowdown_census_20261015/attract_probe_b26/episodes/attract/census.tsv' OUT_ROOT = AF / 'frame_board_paper_20261016' def sha(p): return hashlib.sha256(Path(p).read_bytes()).hexdigest() def main(): p = argparse.ArgumentParser() p.add_argument('--out', required=True) p.add_argument('--frames', type=int, default=9000) a = p.parse_args() out = (OUT_ROOT / a.out).resolve() assert not out.exists(), out out.mkdir(parents=True) assert sha(EXE) == EXE_SHA roms, nchanges, info = cr.patched_roms(cr.DEFAULT_BUILD, out) pin = cr.package_crc.build(EXE.resolve(), EXE_SHA, roms, out / 'fcadefbneo.exe') runtime = cr.make_runtime(out / 'w0', out / 'fcadefbneo.exe', roms, out / 'patch.lua') shutil.copy2(HERE / 'margin_probe.lua', runtime / 'probe.lua') (runtime / 'research_config.lua').write_text('return {frames=%d, poweron=true, probe=true, arm=false}\n' % a.frames) env = dict(os.environ) for k in ('INFINITE_CPS3_TIMING', 'INFINITE_CPS3_TIMING_IMPORT', 'INFINITE_CPS3_SPEEDHACK'): env.pop(k, None) code, failure, seconds = cr.launch(runtime, ['sfiii3nr1', 'probe.lua', '-w'], env, 90 + a.frames // 60, runtime / 'process.log') for f in (runtime / 'results').glob('*'): shutil.copy2(f, out / f.name) for f in ('infinite_cmdline.log', 'process.log'): if (runtime / f).exists(): shutil.copy2(runtime / f, out / f) status = cr.parse_status((out / 'status.txt').read_text()) if (out / 'status.txt').exists() else {} diag = (out / 'infinite_cmdline.log').read_text(errors='replace') if (out / 'infinite_cmdline.log').exists() else '' version_ok = 'CPS3 timing: version 20261017' in diag same = None if (out / 'census.tsv').exists(): mine = list(csv.reader(open(out / 'census.tsv'), delimiter='\t')) ref = list(csv.reader(open(REFERENCE), delimiter='\t')) n = min(len(mine), len(ref)) diffs = [i for i in range(n) if mine[i] != ref[i]] same = dict(rows_compared=n, rows_mine=len(mine), rows_ref=len(ref), differing_rows=len(diffs), first_difference=diffs[0] if diffs else None) report = dict(passed=code == 0 and failure is None and status.get('ok') == '1' and version_ok and same is not None and same['differing_rows'] == 0, exit=code, failure=failure, seconds=seconds, status=status, timing_version_ok=version_ok, census_equal_to_reference=same, program_changes=nchanges, loader_pin=pin, inputs={str(EXE): EXE_SHA, str(HERE / 'margin_probe.lua'): sha(HERE / 'margin_probe.lua'), str(Path(__file__)): sha(Path(__file__)), str(REFERENCE): sha(REFERENCE), str(cr.DEFAULT_BUILD / 'program_probe.bin'): info['program_sha256']}) (out / 'report.json').write_text(json.dumps(report, indent=2)) (runtime / 'fcadefbneo.exe').unlink() for dll in runtime.glob('*.dll'): dll.unlink() print(json.dumps({k: v for k, v in report.items() if k not in ('inputs', 'loader_pin')}, indent=1)) return 0 if report['passed'] else 1 if __name__ == '__main__': sys.exit(main())