"""Step the 68000 and log every entry to the player's interrupt handlers with the hardware frame, the VDP's V counter, and the master clock - exact timing of A/B.""" from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT.parent / "megadrive-python" / "src")) sys.path.insert(0, str(ROOT.parent / "m68000-python" / "src")) sys.path.insert(0, str(ROOT.parent / "z80-python" / "src")) from megadrive_python.__main__ import load_image # noqa: E402 from megadrive_python.cartridge import Cartridge # noqa: E402 from megadrive_python.machine import MACHINES # noqa: E402 from megadrive_python.system import System # noqa: E402 WATCH = {0x346: "vblank(A?)", 0x388: "vblank_b", 0x3E0: "vblank_end", 0x2BA: "fetch_frame", 0x29E: "main_loop", 0x3E8: "EXCEPTION"} VARS = 0xF000 def main(rom: Path, frames: int) -> None: system = System(MACHINES["megadrive"], Cartridge.from_image(load_image(rom))) system.board.fm.enabled = False cpu, vdp, ram = system.m68k, system.vdp, system.board.ram last_pc = -1 seen_main = False while vdp.frame < frames: pc = cpu.PC if pc != last_pc and pc in WATCH: name = WATCH[pc] if name == "main_loop": if seen_main: last_pc = pc system.step() continue seen_main = True if name == "fetch_frame": seen_main = False v = vdp.v phase = int.from_bytes(ram[VARS + 4 : VARS + 6], "big") runs = int.from_bytes(ram[VARS + 8 : VARS + 12], "big") extra = "" if name.startswith("vblank(A"): sp = cpu.ssp off = sp & 0xFFFF sr = int.from_bytes(ram[off : off + 2], "big") ret = int.from_bytes(ram[off + 2 : off + 6], "big") extra = (f" stacked SR={sr:04X} return={ret:06X} vint_pending={vdp.vint_pending} " f"hint_pending={getattr(vdp, 'hint_pending', None)} vdp.ipl={vdp.ipl}") print(f"hw {vdp.frame:3d} mclk {system.mclk:12d} V={v:3d} {name:12s} phase={phase} runs_ptr={runs:08X}{extra}") last_pc = pc system.step() if __name__ == "__main__": main(Path(sys.argv[1]), int(sys.argv[2]) if len(sys.argv) > 2 else 10)