#!/usr/bin/env python # SPDX-License-Identifier: MIT """Seed faults into the APU, one at a time, and check the gate catches each. python scripts/mutation.py [--streams N] # N: replay only the first N streams (default all) A gate that cannot fail proves nothing. Each mutant changes one place in ``src/gb_apu_python/__init__.py`` -- a rule the APU is known to have, broken the way a port might break it -- and the gate is this core's own tests: the board streams (tests/test_board_streams.py) and the seam tests (tests/test_apu.py). A mutant the gate misses is listed. """ from __future__ import annotations import os import shutil import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent APU = ROOT / "src" / "gb_apu_python" / "__init__.py" #: (what is broken, the exact text, its replacement[, the file beside __init__.py it is in]) MUTANTS = [ ("duty 75% inverted", " (0, 1, 1, 1, 1, 1, 1, 0))", " (1, 0, 0, 0, 0, 0, 0, 1))"), ("noise divisors from 16", "NOISE_DIVISORS = (8, 16, 32, 48, 64, 80, 96, 112)", "NOISE_DIVISORS = (16, 16, 32, 48, 64, 80, 96, 112)"), ("wave volume shifts off by one", "WAVE_QUARTERS = (0, 4, 2, 1)", "WAVE_QUARTERS = (0, 2, 1, 0)"), ("wave nibbles swapped", "ch3.buffer = (byte >> 4) if not ch3.index & 1 else byte & 0xF", "ch3.buffer = (byte & 0xF) if not ch3.index & 1 else byte >> 4"), ("wave period at 1 MHz", "ch3.timer += (2048 - ch3.frequency) * 2", "ch3.timer += (2048 - ch3.frequency) * 4"), ("lengths clocked on the even steps", " if divider & 1:\n", " if not divider & 1:\n"), ("sweep on the wrong steps", " if divider & 3 == 3:\n", " if divider & 3 == 1:\n"), ("envelope on the wrong step", " if divider & 7 == 7:\n", " if divider & 7 == 6:\n"), ("DMG noise XOR, not XNOR", " lfsr = ch4.lfsr\n bit = ((lfsr ^ (lfsr >> 1)) & 1) ^ 1", " lfsr = ch4.lfsr\n bit = (lfsr ^ (lfsr >> 1)) & 1"), ("7-bit noise taps bit 5", "lfsr = (lfsr & ~0x40) | (bit << 6)", "lfsr = (lfsr & ~0x40) | (bit << 5)"), ("NR52 hides the power bit", "return 0x70 | (0x80 if self.power else 0) | status", "return 0x70 | status"), ("CH3 length is 6 bits", "self.ch3.length = 256 - value", "self.ch3.length = 256 - (value & 0x3F)"), ("DMG lengths read-only while off", "if not self.cgb and address in (0xFF11, 0xFF16, 0xFF1B, 0xFF20):", "if False and address in (0xFF11, 0xFF16, 0xFF1B, 0xFF20):"), ("CGB power-off keeps the lengths", " if self.cgb:\n channel.length = 0\n", " if False:\n channel.length = 0\n"), ("a high sequencer clock spends nothing", " self.skip_event = 1\n", " self.skip_event = 0\n"), ("wave starts 2 T early", "WAVE_TRIGGER_DELAY = 6", "WAVE_TRIGGER_DELAY = 4"), ("narrower DMG wave corruption", "CORRUPT_LO, CORRUPT_HI = 1, 3", "CORRUPT_LO, CORRUPT_HI = 1, 2"), ("DAC polarity flipped", "analog = (7.5 - digital) / 7.5", "analog = (digital - 7.5) / 7.5"), ("panning sides swapped", " if nr51 & (0x10 << i):\n", " if nr51 & (0x01 << i):\n"), # The AGB row lives in agb.py (clean-room rewrite, 2026-11-25): a fourth field names the file. ("AGB: the CPU reaches the playing bank", " return ((nr30 & NR30_BANK) >> 6) ^ 1", " return (nr30 & NR30_BANK) >> 6", "agb.py"), ("AGB: bank 0 always plays", " selected = (nr30 & NR30_BANK) >> 6", " selected = 0", "agb.py"), ("AGB: 64 digits never chain", "self.second = not self.second if nr30 & NR30_DIMENSION else False", "self.second = False", "agb.py"), ("AGB: the other bank starts a digit early", "if self.count == BANK_DIGITS:", "if self.count == BANK_DIGITS - 1:", "agb.py"), ("AGB: a pointer, not a shift register", "digit = digits.pop(0)\n digits.append(digit)", "digit = digits[self.count]", "agb.py"), ("AGB: a restart keeps the bank it was on", " self.count = 0\n self.second = False\n\n def shift", " self.count = 0\n\n def shift", "agb.py"), ("AGB: force bit ignored", "return FORCED_QUARTERS if nr32 & NR32_FORCE else quarters", "return quarters", "agb.py"), ("AGB: force gives 50 %", "FORCED_QUARTERS = 3", "FORCED_QUARTERS = 2", "agb.py"), ("AGB: NR30's bank bits unreadable", "masks[0x0A] = 0x1F # NR30", "masks[0x0A] = 0x7F # NR30", "agb.py"), ("AGB: wave digits swapped on write", "digits[2 * offset] = (value >> 4) & 0xF\n digits[2 * offset + 1] = value & 0xF", "digits[2 * offset] = value & 0xF\n digits[2 * offset + 1] = (value >> 4) & 0xF", "agb.py"), ("run() drops an odd T", " self._held_t = t & 1\n", " self._held_t = 0\n"), ("PCM nibbles swapped", "return outputs[2 * pair] | (outputs[2 * pair + 1] << 4)", "return (outputs[2 * pair] << 4) | outputs[2 * pair + 1]"), ] # fmt: skip def gate(src: Path, python: str, streams: int | None) -> bool: """True when the gate passes (the mutant survives).""" env = dict(os.environ, PYTHONPATH=str(src)) tests = ["tests/test_apu.py", "tests/test_board_streams.py"] if streams is not None: env["GB_APU_STREAMS"] = str(streams) result = subprocess.run( # -o pythonpath: pyproject puts the real src first; the mutated copy must win. [python, "-m", "pytest", "-q", "-x", "-p", "no:cacheprovider", "-o", "pythonpath=tests scripts", *tests], cwd=ROOT, env=env, capture_output=True, text=True, ) # fmt: skip return result.returncode == 0 def main() -> int: streams = None if "--streams" in sys.argv: streams = int(sys.argv[sys.argv.index("--streams") + 1]) python = sys.executable with tempfile.TemporaryDirectory() as tmp: src = Path(tmp) / "src" shutil.copytree(ROOT / "src", src) if not gate(src, python, streams): print("the gate fails without a mutant: fix that first") return 2 missed = [] for name, old, new, *where in MUTANTS: source = APU.with_name(where[0]) if where else APU original = source.read_text() target = src / "gb_apu_python" / source.name assert original.count(old) == 1, f"{name}: the text is not in {source.name} once" target.write_text(original.replace(old, new)) caught = not gate(src, python, streams) target.write_text(original) print(f"{'caught' if caught else 'MISSED'} {name}", flush=True) if not caught: missed.append(name) print(f"{len(MUTANTS) - len(missed)} of {len(MUTANTS)} caught") return 1 if missed else 0 if __name__ == "__main__": sys.exit(main())