"""The console's answers as ranges, and a check of any other report against them. python tools/hwref.py build hardware/reference.json from the console's saves python tools/hwref.py check SAVE a report (board, BlastEm, die) against it python tools/hwref.py check --board [--machine genesis] [--run PATH-TO-MD-RUN] build the ROM's report on a board first Each metric is the range over every console run and repeat in SAVES (RUNS). OK: inside it. NEAR: outside by no more than the console's own spread, or by 1 count. OUT: further than that. SEEN/NEW: walks and masks, where only an exact value the console gave counts. """ import json import os import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "tools")) import decode import timing REF = ROOT / "hardware" / "reference.json" SAVES = ["hardware/card2/MD-PROBE.SRM", "hardware/card2/NEW.SRM", "hardware/card2/NEW3.SRM", "hardware/card2/NEW4.SRM"] RUNS = range(3, 12) # runs 3-11: the full test set, Model 1601 NTSC, version A1 YMDK_SYNCED = range(7, 12) # 631 waits from the frame edge from run 7 on BOARD = r"D:\dev\pin2\target-megadrive-rust-fast\release\md-run.exe" COUNTS = {"CPU1", "CPU2", "CPU3", "CPU4", "CPU5", "ZRM1", "ZRM2", "ZRO1", "ZRO2", "RDV0", "RDV1", "IDL1", "IDL2", "RDLN", "YMDK"} EXACT = {"YMPT", "YMP2", "YMBY", "BSK1", "BSK2", "BSK3", "BSK4", "SPY1", "SPX1", "SPY0", "SPX0", "SFY1", "SFX1", "SFY0", "SFX0"} ELAPSED = {"DMA1", "DMA2", "DMA3", "DMA4", "DMA5", "DMB0"} PACE = {"DMPR": 200, "DMPW": 300, "DMPC": 1000, "DMPS": 1100} def metrics(run): """{name: [values]} for one run.""" out = {} for t in run["tests"]: tag = t["tag"] for i, rep, v in t["records"]: if tag == "YMDK" and i == 631 and run["run"] not in YMDK_SYNCED and run["run"] >= 0: continue if tag in COUNTS or tag in EXACT: out.setdefault(f"{tag} {i}", []).append(v) elif tag in ELAPSED: out.setdefault(f"{tag} {i} mclk", []).append(round(timing.steps(v >> 16, v & 0xFFFF) * timing.STEP)) elif tag == "VPOS": out.setdefault(f"VPOS {i} steps", []).append(round(timing.steps(0xE000, v & 0xFFFF), 1)) if tag in PACE and t["records"]: by = timing.dma(run, tag, PACE[tag]) xs = [s for s in by for _ in by[s]] ys = [y for s in by for y in by[s]] a, b, _ = timing.fit(xs, ys) out.setdefault(f"{tag} start mclk", []).append(round(a)) out.setdefault(f"{tag} mclk/word", []).append(round(b, 2)) return out def build(): seen, ref = set(), {} for p in SAVES: for run in decode.parse(ROOT / p)["run"]: if run["run"] in seen or run["run"] not in RUNS: continue seen.add(run["run"]) for k, vs in metrics(run).items(): ref.setdefault(k, []).extend(vs) out = {"runs": sorted(seen), "saves": SAVES, "metrics": {}} for k, vs in sorted(ref.items()): tag = k.split()[0] if tag in EXACT: out["metrics"][k] = {"kind": "exact", "values": sorted(set(vs))} else: out["metrics"][k] = {"kind": "range", "min": min(vs), "max": max(vs), "n": len(vs)} REF.write_text(json.dumps(out, indent=1) + "\n") print(f"{REF}: {len(out['metrics'])} metrics from runs {out['runs']}") def check(path): ref = json.loads(REF.read_text()) run = decode.latest(decode.parse(path)) run = dict(run, run=-1) got = metrics(run) tally = {} print(f"{'metric':22}{'console':>24}{'this':>24} verdict") for k, m in ref["metrics"].items(): vs = got.get(k) if not vs: verdict, cell = "MISSING", "-" elif m["kind"] == "exact": new = sorted(set(vs) - set(m["values"])) verdict = "NEW" if new else "SEEN" cell = " ".join(f"{v:X}" for v in sorted(set(vs)))[:23] else: lo, hi = min(vs), max(vs) spread = max(m["max"] - m["min"], 1) off = max(m["min"] - lo, hi - m["max"], 0) verdict = "OK" if off == 0 else "NEAR" if off <= spread else "OUT" cell = f"{lo}" if lo == hi else f"{lo}-{hi}" tally[verdict] = tally.get(verdict, 0) + 1 if m["kind"] == "exact": hw = " ".join(f"{v:X}" for v in m["values"])[:23] else: hw = f"{m['min']}" if m["min"] == m["max"] else f"{m['min']}-{m['max']}" if verdict not in ("OK", "SEEN") or "--all" in sys.argv: print(f"{k:22}{hw:>24}{cell:>24} {verdict}") print(" ".join(f"{k} {v}" for k, v in sorted(tally.items()))) def board_save(): run = sys.argv[sys.argv.index("--run") + 1] if "--run" in sys.argv else BOARD machine = sys.argv[sys.argv.index("--machine") + 1] if "--machine" in sys.argv else "genesis" subprocess.run([sys.executable, "build.py"], cwd=ROOT, check=True, capture_output=True) save = Path(tempfile.gettempdir()) / f"hwref-{machine}.sav" if save.exists(): save.unlink() subprocess.run([run, "out/alewman-probe.bin", "--machine", machine, "--frames", "7000", "--save", str(save), "--quiet"], cwd=ROOT, check=True) return save if __name__ == "__main__": if sys.argv[1:2] == ["build"]: build() elif sys.argv[1:2] == ["check"]: check(board_save() if "--board" in sys.argv else sys.argv[2]) else: print(__doc__)