#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Seeded faults for the milestone 4 judge (the sprites1-5 CRCs): each must lower a count. python -I scripts/m4_faults.py [--machine lynx2] """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from lynx_python import testrom from lynx_python.machine import MACHINES from lynx_python.suzy import engine def fault_shadow_error_fixed(): """Type 7's pen E collides (the documented missing inverter 'repaired').""" saved = engine.TYPES types = list(saved) types[7] = (types[7][0], ("O", "D"), types[7][2], types[7][3]) engine.TYPES = tuple(types) return lambda: setattr(engine, "TYPES", saved) def fault_last_bit_decoded(): """The line's last bit decoded (the end-of-line bug lost).""" saved = engine.SpriteEngine._decode def decode(self, line, length, bpp): if self.suzy.sprctl1 & 0x80: data = [self._read(line + i) for i in range(length)] bits = 8 * length for k in range(bits // bpp): value = 0 for p in range(k * bpp, (k + 1) * bpp): value = (value << 1) | ((data[p >> 3] >> (7 - (p & 7))) & 1) yield value return yield from saved(self, line, length, bpp) engine.SpriteEngine._decode = decode return lambda: setattr(engine.SpriteEngine, "_decode", saved) def fault_no_depository(): """The collision depository never written.""" saved = engine.DEPOSITORY_TYPES engine.DEPOSITORY_TYPES = () return lambda: setattr(engine, "DEPOSITORY_TYPES", saved) def fault_tilt_sign(): """Tilt moves the other way.""" saved = engine._signed original_sprite = engine.SpriteEngine._sprite def sprite(self, scb, depth): suzy = self.suzy suzy.set_word(engine.TILT, (-saved(suzy.word(engine.TILT))) & 0xFFFF) yield from original_sprite(self, scb, depth) engine.SpriteEngine._sprite = sprite return lambda: setattr(engine.SpriteEngine, "_sprite", original_sprite) def fault_xor_as_opaque(): """Exclusive-or sprites write their pens instead of XORing.""" saved = engine.TYPES engine.TYPES = tuple( tuple((("O" if mode == "X" else mode), coll) for mode, coll in rules) for rules in saved ) return lambda: setattr(engine, "TYPES", saved) def fault_tilt_free(): """Milestone 5: the tilt stage costs nothing (judged on the durations).""" saved = engine.COSTS["tilt"] engine.COSTS["tilt"] = 0 return lambda: engine.COSTS.__setitem__("tilt", saved) def fault_row_doubled(): """Milestone 5: the fixed row work doubled (judged on the durations).""" saved = engine.COSTS["row"] engine.COSTS["row"] = 2 * saved return lambda: engine.COSTS.__setitem__("row", saved) def fault_pixel_halved(): """Milestone 5, the measured timeline: 1 tick a pixel instead of 2.""" saved = engine.MEASURED["pixel"] engine.MEASURED["pixel"] = 1 return lambda: engine.MEASURED.__setitem__("pixel", saved) def fault_line_end_free(): """Milestone 5, the measured timeline: no line-end wait.""" saved = engine.MEASURED["end"] engine.MEASURED["end"] = 0 return lambda: engine.MEASURED.__setitem__("end", saved) # (name, seed, suite, timeline, judge). "windows": tests inside their console windows # must drop. "durations": any change of a measured duration counts (a regression gate, # not a hardware judge): added for tilt-free, the windows' blind spot (sprites5's tilt # cases stay inside their 32 us windows without the 18-tick stage, validation "M5"). TIMING_FAULTS = [ ("row-doubled", fault_row_doubled, "sprites5", "paths", "windows"), ("tilt-free", fault_tilt_free, "sprites5", "paths", "durations"), # A blind spot of the windows: neither sprites2's five passing windows nor # sprites5's eight see it (2026-11-14); the durations gate does. ("pixel-halved", fault_pixel_halved, "sprites5", "measured", "durations"), ("line-end-free", fault_line_end_free, "sprites5", "measured", "windows"), ] def _durations(outcome) -> tuple[int, ...]: data = outcome.results return tuple(data[4 * t] | (data[4 * t + 1] << 8) for t in range(len(data) // 4)) FAULTS = [ ("shadow-error-fixed", fault_shadow_error_fixed, "sprites3"), ("last-bit-decoded", fault_last_bit_decoded, "sprites1"), ("no-depository", fault_no_depository, "sprites3"), ("tilt-sign", fault_tilt_sign, "sprites5"), ("xor-as-opaque", fault_xor_as_opaque, "sprites3"), ] def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--machine", default="lynx2", choices=sorted(MACHINES)) parser.add_argument("--only", help="run only the fault with this name") args = parser.parse_args() machine = MACHINES[args.machine] caught = 0 if args.only: FAULTS[:] = [f for f in FAULTS if f[0] == args.only] TIMING_FAULTS[:] = [f for f in TIMING_FAULTS if f[0] == args.only] for name, seed, suite in FAULTS: clean = sum(testrom.run(suite, machine).function) undo = seed() try: faulty = sum(testrom.run(suite, machine).function) finally: undo() verdict = "caught" if faulty < clean else "MISSED" caught += faulty < clean print(f"{name:20} {suite:9} CRCs clean {clean} faulty {faulty}: {verdict}", flush=True) for name, seed, suite, timeline, judge in TIMING_FAULTS: saved_timeline = engine.TIMELINE engine.TIMELINE = timeline try: clean_run = testrom.run(suite, machine) undo = seed() try: faulty_run = testrom.run(suite, machine) finally: undo() finally: engine.TIMELINE = saved_timeline if judge == "durations": hit = _durations(faulty_run) != _durations(clean_run) clean, faulty = "durations", "changed" if hit else "same" else: clean, faulty = sum(clean_run.passed), sum(faulty_run.passed) hit = faulty < clean caught += hit print(f"{name:20} {suite:9} {timeline:8} {judge}: clean {clean} faulty {faulty}: " f"{'caught' if hit else 'MISSED'}", flush=True) # fmt: skip total = len(FAULTS) + len(TIMING_FAULTS) print(f"{caught}/{total} caught") return 0 if caught == total else 1 if __name__ == "__main__": sys.exit(main())