# SPDX-License-Identifier: MIT """Milestone 6: which BIOS addresses a cartridge calls directly, and what each BIOS has there. Runs a cartridge for N frames on a machine row and records every step that goes from cartridge code ($4000-$BFFF) straight into the BIOS ($F800-$FFFF) without an interrupt (a JSR or JMP into the ROM), with the target address. For each target it prints the instruction both BIOS revisions hold there, so a call into the 4-port BIOS that lands mid-routine on revision A (the 22-byte shift, docs/undocumented-behavior.md C1) is named. It also reports whether the run jammed and where the PC was at the end. python scripts/bios_calls.py CART [--machine a5200_2port] [--frames 600] """ 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 sixfiveohtwo.debug import BoundaryKind, next_boundary # noqa: E402 from a5200_python.bios import find_bios # noqa: E402 from a5200_python.machine import BIOS_4PORT_SHA1, BIOS_REVA_SHA1 # noqa: E402 from a5200_python.system import Console # noqa: E402 def _fallback(data: bytes) -> str: return " ".join(f"{b:02X}" for b in data[:3]) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("cart") parser.add_argument("--machine", default="a5200") parser.add_argument("--frames", type=int, default=600) parser.add_argument( "--press", action="append", default=[], help="FRAME:KEY:FRAMES on port 1 (as scripted_run)" ) parser.add_argument("--stick", action="append", default=[], help="FRAME:X:Y on port 1") parser.add_argument( "--fault-bios", metavar="MACHINE", help="seeded fault: use that machine row's BIOS" ) args = parser.parse_args() bios = None if args.fault_bios: # Seeded fault: the row runs with the other row's BIOS (shows the gate can fail). from a5200_python.machine import MACHINES bios, _ = find_bios(MACHINES[args.fault_bios].bios_sha1) console = Console(args.cart, args.machine, bios=bios) cpu = console.board.cpu pad = console.controllers.pads[0] presses = [(int(f), k, int(n)) for f, k, n in (p.split(":") for p in args.press)] sticks = {int(f): (float(x), float(y)) for f, x, y in (s.split(":") for s in args.stick)} last_frame = -1 targets: dict[int, int] = {} reads: dict[int, int] = {} def observe(kind: str, address: int, value: int, cycle: int) -> None: # Data reads of the ROM made by cartridge code (tables, vectors, RTS tricks). if kind == "R" and address >= 0xF800 and 0x4000 <= cpu.pc < 0xC000: reads[address] = reads.get(address, 0) + 1 console.board.bus_observers.append(observe) limit = console.board.machine.cycles_per_frame * args.frames while console.board.cycles < limit and not cpu.halted: frame = console.board.frames if frame != last_frame: last_frame = frame held = [k for f, k, n in presses if f <= frame < f + n] pad.keys = {k for k in held if k not in ("trigger", "top")} pad.trigger = "trigger" in held pad.top = "top" in held if frame in sticks: pad.x, pad.y = sticks[frame] console.controllers.apply() before = cpu.pc kind = next_boundary(cpu.capture_state()) cpu.step() if kind is BoundaryKind.INSTRUCTION and 0x4000 <= before < 0xC000 and cpu.pc >= 0xF800: targets[cpu.pc] = targets.get(cpu.pc, 0) + 1 four, _ = find_bios(BIOS_4PORT_SHA1) rev_a, _ = find_bios(BIOS_REVA_SHA1) print( f"{Path(args.cart).name} on {args.machine}: {console.board.frames} frames, " f"{'JAMMED' if cpu.halted else 'running'}, final PC ${cpu.pc:04X}" ) for target in sorted(targets): print( f" calls ${target:04X} x{targets[target]}: 4-port {_fallback(four[target & 0x7FF :][:3])}" f" | rev A {_fallback(rev_a[target & 0x7FF :][:3])}" ) code = sorted(a for a in reads if a >= 0xFC00) if code: shown = ", ".join(f"${a:04X}" for a in code[:16]) print( f" reads the ROM's code area from cartridge code: {shown}" f"{' ...' if len(code) > 16 else ''}" ) return 0 if __name__ == "__main__": raise SystemExit(main())