#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Find the DPRAM slot and refresh phases that align this board with a capture. The captures show where the timer/audio DPRAM slots and the refresh requests fall against the program, and the program synchronises itself to the timers (the page-mode ROM waits for Timer 2's reload before each measurement). So the two phases the bus cannot show directly (``board.DPRAM_PHASE`` against the timer prescaler, ``board.REFRESH_PHASE`` within its 256 ticks) are the ones whose run makes the same cycle-by-cycle gaps as the capture. For each candidate pair this runs ``la_compare.py``'s comparison and counts the differing gaps. python -I scripts/la_phase.py CAPTURE.dsl ROM ROUTINE --align ADDR [--count N] """ from __future__ import annotations import argparse import contextlib import io import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) import la_compare from lynx_python import board def differing(argv: list[str]) -> int: out = io.StringIO() with contextlib.redirect_stdout(out): la_compare.main(argv) last = out.getvalue().strip().splitlines()[-1] return int(last.split()[0]) def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("capture") parser.add_argument("rom") parser.add_argument("routine") parser.add_argument("--align", required=True) parser.add_argument("--count", default="1500") parser.add_argument("--slots", default="0-15") parser.add_argument("--refresh", default="0-15", help="refresh phase in --step-tick steps") parser.add_argument("--step", type=int, default=16) args = parser.parse_args(argv) def span(text: str) -> range: a, _, b = text.partition("-") return range(int(a), int(b or a) + 1) common = [args.capture, args.rom, args.routine, "--align", args.align, "--count", args.count, "--diff-only"] # fmt: skip results = [] for p in span(args.slots): for q in span(args.refresh): board.DPRAM_PHASE = p board.REFRESH_PHASE = p + args.step * q n = differing(common) results.append((n, p, board.REFRESH_PHASE)) print(f"slot phase {p:2} refresh {board.REFRESH_PHASE:3}: {n} differing", flush=True) results.sort() print("best:", results[:5]) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))