#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Seeded faults for the milestone 2 judge: each one-line change must lower a suite's count. python -I scripts/m2_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")) sys.path.insert(0, str(ROOT / "scripts")) from lynx_python import testrom from lynx_python.machine import MACHINES from lynx_python.mikey import timers as timers_module from lynx_python.suzy import math as math_module def fault_borrow_at_zero(): """The interrupt on reaching zero instead of on the clock after (LT timers).""" original = timers_module.TimerBank.clock def clock(self, timer, when, forced=False): original(self, timer, when, forced) if timer.count == 0 and timer.ctla & 0x80 and timer.irq_bit: self.pending |= timer.irq_bit timers_module.TimerBank.clock = clock return lambda: setattr(timers_module.TimerBank, "clock", original) def fault_reset_done_pulse(): """RESET_DONE as a pulse (cleared once on the write) instead of a level (Spec ยง16).""" original = timers_module.TimerBank.clock def clock(self, timer, when, forced=False): saved = timer.ctla timer.ctla &= ~0x40 original(self, timer, when, forced) timer.ctla = saved timers_module.TimerBank.clock = clock return lambda: setattr(timers_module.TimerBank, "clock", original) def fault_divide_remainder(): """The remainder written one too high.""" original = math_module.MathUnit._divide def divide(self, now): original(self, now) self._put(self._get(math_module.L, math_module.M) + 1, math_module.L, math_module.M) math_module.MathUnit._divide = divide return lambda: setattr(math_module.MathUnit, "_divide", original) def fault_sdone_not_sticky(): """The Suzy-done latch never set (CPUSLEEP after a completion would sleep again).""" from lynx_python import mikey as mikey_module original = mikey_module.Mikey.__setattr__ def setattr_(self, name, value): if name == "sdone_pending": value = 0 original(self, name, value) mikey_module.Mikey.__setattr__ = setattr_ return lambda: setattr(mikey_module.Mikey, "__setattr__", original) def fault_page_mode_off(): """No 4-tick fetches (the page-mode discount lost).""" from lynx_python import board saved = board.PAGE_MODE_TICKS board.PAGE_MODE_TICKS = 5 return lambda: setattr(board, "PAGE_MODE_TICKS", saved) def fault_dpram_no_slot(): """Timer/audio DPRAM accesses a flat 5 ticks (the slot wait lost).""" from lynx_python import board saved = board.Lynx._dpram_cost board.Lynx._dpram_cost = lambda self, address: board.NORMAL_TICKS return lambda: setattr(board.Lynx, "_dpram_cost", saved) def fault_refresh_off(): """No refresh cycles outside display DMA.""" from lynx_python import board saved = board.REFRESH_TICKS board.REFRESH_TICKS = 0 return lambda: setattr(board, "REFRESH_TICKS", saved) def fault_dummy_not_stream(): """Any instruction-stream read may use page mode, wherever it is (the address test against the last stream read lost: JSR/RTS/branch targets become 4-tick reads).""" from lynx_python import board saved = board.Lynx._read_cost def read_cost(self, address): if self.cpu.fetch: self.last_stream = (address - 1) & 0xFFFF return saved(self, address) board.Lynx._read_cost = read_cost return lambda: setattr(board.Lynx, "_read_cost", saved) def fault_lfsr_unclocked(): """M6: an audio timer's borrow no longer clocks its polynomial counter.""" from lynx_python import mikey as mikey_module saved = mikey_module.Mikey._audio_clock mikey_module.Mikey._audio_clock = lambda self, channel: None return lambda: setattr(mikey_module.Mikey, "_audio_clock", saved) FAULTS = [ ("lfsr-unclocked", fault_lfsr_unclocked, "audio"), ("borrow-at-zero", fault_borrow_at_zero, "timers"), ("reset-done-pulse", fault_reset_done_pulse, "timers"), ("divide-remainder", fault_divide_remainder, "math"), ("sdone-not-sticky", fault_sdone_not_sticky, "sdoneack"), ("page-mode-off", fault_page_mode_off, "timers"), ("page-mode-off", fault_page_mode_off, "math"), ("dpram-no-slot", fault_dpram_no_slot, "timers2"), ("refresh-off", fault_refresh_off, "page-mode"), # The test ROMs do not see this one (their timed loops hold no JSR, RTS or taken # branch: a blind spot of that judge); the capture comparison does. ("stream-address-lost", fault_dummy_not_stream, "capture"), ("dpram-no-slot", fault_dpram_no_slot, "capture"), ] def score(suite: str, machine) -> int: """Tests passed, or for "capture" minus the gaps that differ from the page-mode capture over its first 3,000 CPU cycles (``la_compare.py``).""" if suite != "capture": return sum(testrom.run(suite, machine).passed) import la_phase capture = ROOT / "reference/src/gearlynx/tests/logic-analyzer/lynx2-mikey-page_mode.dsl" return -la_phase.differing( [str(capture), "page-mode", "TriggerPulse", "--align", "02AD", "--count", "3000", "--diff-only"] ) # fmt: skip def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--machine", default="lynx2", choices=sorted(MACHINES)) args = parser.parse_args() machine = MACHINES[args.machine] caught = 0 for name, seed, suite in FAULTS: clean = score(suite, machine) undo = seed() try: faulty = score(suite, machine) finally: undo() verdict = "caught" if faulty < clean else "MISSED" caught += faulty < clean print(f"{name:18} {suite:9} clean {clean} faulty {faulty}: {verdict}") print(f"{caught}/{len(FAULTS)} caught") return 0 if caught == len(FAULTS) else 1 if __name__ == "__main__": sys.exit(main())