#!/usr/bin/env python3 """Rebuild the kept changes as a clean chain: for each, the tree (with the not-kept changes undone), a bench binary and the gate. Usage: rebuild.py [--drop-c10] [--only N]""" import subprocess, sys, pathlib, re ROOT = pathlib.Path("/data/emu/pcengine-rust-fast") WT = ROOT / "external" / "abl" T = ROOT / "external" / "target-chain" BINS = ROOT / "external" / "bins" TREES = ROOT / "external" / "trees" DROP_C10 = "--drop-c10" in sys.argv ONLY = int(sys.argv[sys.argv.index("--only") + 1]) if "--only" in sys.argv else None # (name, original commit, undo c6?, undo c10?) STEPS = [ ("k01", "2fe149a", False, False), ("k02", "d9e74e2", False, False), ("k03", "d065c2d", False, False), ("k04", "c3134f9", False, False), ("k05", "414ff81", False, False), ("k06", "3860abf", True, False), ("k07", "c9a2fd3", True, False), ("k08", "f4aecf0", True, False), ("k09", "36bc4a2", True, False), ("k10", "4508359", True, DROP_C10), ("k11", "6d90c0e", True, DROP_C10), ("k12", "6d90c0e", True, DROP_C10), # and the whole-program profile (external/lto.patch) ("x13", "6d90c0e", True, DROP_C10), # and the second sprite attempt (external/x13.patch) ] if DROP_C10: STEPS = [s for s in STEPS if s[0] != "k09"] def sh(*cmd, cwd=WT, check=True, quiet=False): done = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True) if check and done.returncode: print(done.stdout[-3000:], done.stderr[-3000:]) raise SystemExit(f"failed: {' '.join(map(str, cmd))}") return done.stdout def show(rev_path): return subprocess.run(["git", "show", rev_path], cwd=ROOT, capture_output=True, text=True, check=True).stdout def undo_c6(): base = show("1cc528d:src/vdc/sprites.rs") old_draw = base[base.index("/// Draw the entries over `row`"):] p = WT / "src/vdc/sprites.rs" s = p.read_text() s = s[: s.index("/// Draw the entries over `row`")] + old_draw s = s.replace("\n\nuse crate::vdc::background::SPREAD;\n", "\n") p.write_text(s) p = WT / "src/vdc/background.rs" p.write_text(p.read_text().replace("pub(crate) const SPREAD", "const SPREAD")) p = WT / "scripts/seeded_faults.py" s = p.read_text() i = s.index(' "sprite-flip-ignored": (') j = s.index(' "any-path-is-direct": (') s = s[:i] + s[j:] s = s.replace("the bitplane table puts a tile line's (and a sprite row's) pixels in the other order", "the bitplane table puts a tile line's pixels in the other order") p.write_text(s) def undo_c10(): (WT / "src/video.rs").write_text(show("f4aecf0:src/video.rs")) p = WT / "scripts/seeded_faults.py" s = p.read_text() a = '" let split = &row[..0];\\n if used == 0 || used != 0 {\\n",' assert a in s p.write_text(s.replace(a, '" let split = &row[..0];\\n {\\n",')) for n, (name, orig, c6, c10) in enumerate(STEPS, 1): if ONLY and n < ONLY: continue sh("git", "checkout", "-q", "--detach", orig) if c6: undo_c6() if c10: undo_c10() if name in ("k12", "x13"): sh("git", "apply", str(ROOT / "external/lto.patch")) if name == "x13": sh("git", "apply", str(ROOT / "external/x13.patch")) p = WT / "scripts/seeded_faults.py" s = p.read_text() a = "}\n\nLOCKSTEP = [" b = ''' "sprite-flip-ignored": ( "vdc/sprites.rs", "let nibble = if entry.hflip { 15 - x } else { x };", "let nibble = x;", "a sprite's sixteen pixels come out of the table unflipped whatever its flip bit", ), "sprite-merge-skips-the-first-entry": ( "vdc/sprites.rs", " for entry in entries {\\n for column in span(entry) {", " for entry in entries.iter().skip(1) {\\n for column in span(entry) {", "the columns under a line's first sprite entry are not merged into the line", ), } LOCKSTEP = [''' assert s.count(a) == 1 p.write_text(s.replace(a, b)) sh("cargo", "fmt", "--check") sh("git", "add", "-A") tree = sh("git", "write-tree").strip() (TREES / name).write_text(tree + "\n") faults = sh("python3", "scripts/seeded_faults.py", "--check", check=False).strip().splitlines()[-1] manifest = WT / "Cargo.toml" manifest.write_text(manifest.read_text().replace('path = "../huc6280-rust"', 'path = "/data/emu/huc6280-rust"')) lto = [] target = str(T) + ("-lto" if name in ("k12", "x13") else "") sh("nice", "cargo", "build", "--release", "-q", "--target-dir", target) T2 = pathlib.Path(target) sh("cp", str(T2 / "release/pce-bench"), str(BINS / name)) tests = sh("nice", "cargo", "test", "--release", "-q", "--target-dir", target, *lto) passed = sum(int(m) for m in re.findall(r"(\d+) passed", tests)) lock = sh("nice", "python3", str(ROOT / "scripts/lockstep.py"), "--rust", str(T2 / "release/pce-trace"), "--reference-cache", str(ROOT / "external/refcache"), "--jobs", "12", "--quiet", cwd=ROOT).strip().splitlines()[-1] states = sh("nice", "python3", str(ROOT / "scripts/state_roundtrip.py"), "--binary", str(T2 / "release/pce-state-roundtrip"), "--jobs", "12", cwd=ROOT).strip().splitlines()[-1] sh("git", "reset", "-q", "--hard") print(f"{name} ({orig}): tree {tree[:10]}; {faults}; {passed} tests pass; {lock}; {states}", flush=True)