#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 6: a2600-python's own gates, run through this board's 2600 mode. The gates live in ``a2600_python.gates`` (pinned at ``28fb095``, docs/api.md there, "Gates"). Each takes ``root=``, an a2600-python checkout with its references fetched (``--root``, default ``../a2600-python`` beside this repository), and a factory for the candidate; the 2600 dumps are read from ``$NO_INTRO_DIR/Atari - Atari 2600``. - ``--die``: every Sim2600 trace replayed into a TIA made as this board makes it (a2600-python's class with the NTSC 7800 row's revision, the reference 10444D the traces judge), from the die's own start (half clock 0). - ``--timers``, ``--smoke``, ``--serial``: on :class:`a7800_python.mode2600.System2600`, which starts a 7800, locks INPTCTRL into 2600 mode and hands its SALLY (adopted, ``System(..., cpu=)``), TIA and RIOT to a2600-python's board (``start="direct"``, so the start state is the one the 2600 rows' reference starts in; the BIOS-started runs are ``--bios``). ``--timers`` runs on both 7800 rows through ``gates.timer_roms(machine=)``. ``--smoke`` is ``gates.lockstep`` (every bus cycle beside a2600-python's own board on the NTSC 7800 row) and ``gates.run_hashes`` on both sides (pixels, audio and registers per frame, since the lockstep sees no picture), for the dumps of ``gates.smoke_names(root)``. - ``--bios``: six dumps from each BIOS row (Pitfall II for the DPC's clock), 60 TIA frames' ``gates.run_hashes`` digests printed for comparing runs. python scripts/gates_2600.py --die --timers --smoke --bios python scripts/gates_2600.py --serial PAL_7800 # seven tables, one console, slow """ from __future__ import annotations import argparse import sys import time import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from a2600_python import gates # noqa: E402 from a2600_python.cartridge import UnsupportedCartridge # noqa: E402 from a2600_python.cartridge import load as load2600 # noqa: E402 from a2600_python.machine import CONSOLE_ROWS, NTSC_7800 # noqa: E402 from a2600_python.system import System as System2600Reference # noqa: E402 from a2600_python.tia import TIA # noqa: E402 from a7800_python import romsets # noqa: E402 from a7800_python.machine import NTSC, PAL # noqa: E402 from a7800_python.mode2600 import ROWS, System2600, make_system # noqa: E402 A2600_ROOT = ROOT.parent / "a2600-python" # the sibling checkout; --root overrides ROMS = romsets.no_intro(romsets.DUMPS_2600) BIOS_DUMPS = [ "Combat (Europe)", "Pitfall! - Pitfall Harry's Jungle Adventure (USA)", "Space Invaders (USA)", "Adventure (USA)", "River Raid (USA)", # DPC: its music oscillator counts the row's CPU clock (a2600-python 28fb095) "David Crane's Pitfall II - Lost Caverns (USA)", ] # fmt: skip def read(name: str) -> bytes: with zipfile.ZipFile(ROMS / f"{name}.zip") as z: return z.read(z.infolist()[0]) def board_tia() -> TIA: """A TIA as board.py makes it: a2600-python's class, the NTSC 7800 row's revision.""" return TIA(revision=NTSC.mode_2600.tia_revision) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--root", type=Path, default=A2600_ROOT) parser.add_argument("--die", action="store_true") parser.add_argument("--timers", action="store_true") parser.add_argument("--smoke", action="store_true") parser.add_argument("--smoke-cycles", type=int, default=1_000_000, help="bus cycles compared per dump (gates.lockstep's default)") parser.add_argument("--smoke-frames", type=int, default=30, help="frames whose gates.run_hashes are compared per dump") parser.add_argument("--bios", action="store_true") parser.add_argument("--serial", choices=sorted(CONSOLE_ROWS), help="one #699 console's seven tables (PAL_7800 is this board's)") parser.add_argument("--phase-steps", type=int, default=0, help="for --serial: TIA half clocks stepped before the hand-off (U11)") args = parser.parse_args() ok = True if args.die: began = time.monotonic() results = gates.die_traces(args.root, tia_factory=board_tia) passed = sum(r[2] for r in results.values()) print(f"die traces: {passed} of {len(results)} pass ({time.monotonic() - began:.0f} s)" + "".join(f"\n FAIL {k}: {r[1]}" for k, r in results.items() if not r[2])) ok &= passed == len(results) if args.timers: for machine in (NTSC, PAL): # the row goes to the gate, which hands it to the factory (the console # results are NTSC 2600s': a pass says they reproduce on this row) began = time.monotonic() results = gates.timer_roms(args.root, system_factory=make_system, machine=machine.mode_2600) # fmt: skip passed = sum(results.values()) print(f"timer ROMs on {machine.mode_2600.name}: {passed} of {len(results)} pass " f"({time.monotonic() - began:.0f} s)" + "".join(f"\n FAIL {k}" for k, v in results.items() if not v)) # fmt: skip ok &= passed == len(results) if args.smoke: names = gates.smoke_names(args.root) same = refused = 0 began = time.monotonic() for name in names: path = ROMS / f"{name}.zip" if not path.exists(): print(f" missing: {name}") continue try: agreed, difference = gates.lockstep(path, system_factory=make_system, machine=NTSC_7800, cycles=args.smoke_cycles) # fmt: skip except UnsupportedCartridge as error: # a2600-python's loader refuses it refused += 1 print(f" {name}: not loadable on a2600-python ({error})", flush=True) continue # gates.lockstep compares bus cycles only; the picture, the audio and the # registers at each frame's end come from gates.run_hashes on both sides. hashes = [gates.run_hashes(factory(load2600(path).cartridge, NTSC_7800), args.smoke_frames) for factory in (System2600Reference, make_system)] # fmt: skip pictures = hashes[0] == hashes[1] same += difference is None and pictures verdict = "identical" if difference is None else str(difference) if not pictures: verdict += "; frame hashes differ: " + ", ".join( k for k in ("pixels", "audio", "registers") if hashes[0][k] != hashes[1][k]) print(f" {name}: {verdict} ({agreed} cycles, {args.smoke_frames} frames)", flush=True) # fmt: skip print(f"smoke group: {same} of {len(names)} identical to a2600-python's board over " f"{args.smoke_cycles} bus cycles and {args.smoke_frames} frames' hashes each, " f"{NTSC_7800.name} ({refused} not loadable; " f"{time.monotonic() - began:.0f} s)") # fmt: skip ok &= same + refused == len(names) if args.bios: for machine in (NTSC, PAL): for name in BIOS_DUMPS: began = time.monotonic() system = System2600.from_rom(read(name), machine, start="bios") frame = system.system.board.maria.frame # 60 TIA frames' digests (pixels, audio, registers), to compare runs hashes = gates.run_hashes(system, 60) digest = " ".join(f"{k} {v[:12]}" for k, v in sorted(hashes.items())) print(f" {machine.name}: {name}: BIOS handed over at MARIA frame {frame}, then " f"60 TIA frames: {digest} ({time.monotonic() - began:.0f} s)", flush=True) if args.serial: row = CONSOLE_ROWS[args.serial] if row not in ROWS: parser.error(f"{args.serial} is not a 7800 row; this board runs PAL_7800") def phased(cartridge, machine): return System2600(cartridge, ROWS[machine], start="direct", phase_steps=args.phase_steps) # fmt: skip began = time.monotonic() results = gates.serial_tables(args.root, row, args.serial, system_factory=phased) matched = sum(r[0] for r in results.values()) total = sum(r[1] for r in results.values()) for rom, (m, t, misses) in results.items(): print(f" {rom}: {m} of {t} cells" + (f", first miss {misses[0]}" if misses else "")) print(f"#699 {args.serial} tables on {row.name}, phase +{args.phase_steps} half " f"clocks: {matched} of {total} cells ({time.monotonic() - began:.0f} s)") ok &= matched == total return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())