# SPDX-License-Identifier: MIT """Display-list interrupts per frame, the board against MAME's NMI log. MAME's side is ``nmi.log`` from ``mame_trace.py --writes-only --nmi-log`` (every read of $FFFA, the NMI vector, with MAME's time and frame number; the display list list as memory held it then). The board's side is the same event on its own bus (a CPU read of $FFFA), with MARIA's frame, line and clock, and, for every DLI MARIA raised, the line and clock it was raised on and the DLL entry whose DLI bit raised it (its index from DPP). MAME's line and clock are computed from its time: 454 x 263 clocks of 7,159,090 Hz a frame, time 0 at line 251 (its Lua screen has no vpos). The board's power-on is placed so that its first instruction falls where MAME's does (docs/build-progress.md, milestone 1), so the two line numbers are comparable to within MAME's per-line DMA lump (mame-oracle.md trap 4). Prints, per frame, the lines of the NMIs on each side, and the first frame whose count differs, with the DLL (zones: first line, lines, DLI bit) as both sides hold it then. python scripts/dli_compare.py --seconds 6 [--frames A-B] """ from __future__ import annotations import argparse import collections import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) sys.path.insert(0, str(ROOT / "scripts")) from a7800_python.machine import NTSC # noqa: E402 from a7800_python.system import System # noqa: E402 MARIA_HZ = 7_159_090 LINE, LINES = 454, 263 # MAME's time 0 is its line 251, clock 0 (its first instruction, at cycle 8, # is at beamy 251 beamx 32: mame-oracle.md); its frame counts from line 0. MAME_START = 251 * LINE IRQS = collections.Counter() # IRQ/BRK vector fetches ($FFFE) per side def mame_nmis(log: Path, seconds: float): """[(frame, line, clock)] and {frame: (dpp, dll bytes)} from nmi.log.""" nmis, dlls = [], {} last_dll = None with log.open() as handle: for raw in handle: parts = raw.split() if parts[0] == "I" and float(parts[2]) <= seconds: IRQS["MAME"] += 1 if parts[0] == "L": last_dll = (int(parts[1], 16), bytes(int(b, 16) for b in parts[2:])) elif parts[0] == "N": t = float(parts[2]) if t > seconds: break clocks = round(t * MARIA_HZ) + MAME_START frame, rest = divmod(clocks, LINE * LINES) nmis.append((frame, rest // LINE, rest % LINE)) dlls.setdefault(frame, last_dll) return nmis, dlls def zones(dll: bytes, first_line: int = 16, last_line: int = 258): """[(entry, first line, lines, dli)] of a DLL from its first entry, to the end of DMA.""" out, line, entry = [], first_line, 0 while line < last_line and entry * 3 + 2 < len(dll): flags = dll[entry * 3] lines = (flags & 0x0F) + 1 out.append((entry, line, lines, flags >> 7)) line += lines entry += 1 return out def board_nmis(cart: Path, seconds: float, switches: bool): system = System.from_rom(cart, NTSC) board, maria = system.board, system.board.maria if switches: from harness import mame_switches mame_switches(board) nmis, raised = [], [] dlls = {} entry_of = maria._dll_entry def dll_entry(): address = maria.dll result = yield from entry_of() if maria.dli_pending: dpp = maria.registers[0x0C] << 8 | maria.registers[0x10] raised.append((maria.frame, maria.line, maria.clock_in_line, (address - dpp) // 3)) return result maria._dll_entry = dll_entry def watch(kind, address, value, clock): if kind == "R" and address == 0xFFFE: IRQS["board"] += 1 if kind == "R" and address == 0xFFFA: nmis.append((maria.frame, maria.line, maria.clock_in_line)) if maria.frame not in dlls: dpp = maria.registers[0x0C] << 8 | maria.registers[0x10] dlls[maria.frame] = (dpp, bytes(board.dma_read((dpp + i) & 0xFFFF) for i in range(192))) # fmt: skip board.bus_observers.append(watch) end = int(seconds * MARIA_HZ) step = system.cpu.step while board.clocks < end: step() return nmis, raised, dlls def describe(dll) -> str: if dll is None: return "(no DLL)" dpp, data = dll return f"DPP ${dpp:04X}: " + " ".join( f"{e}:{first}+{n}{'*' if dli else ''}" for e, first, n, dli in zones(data) ) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("log", type=Path) parser.add_argument("cart", type=Path) parser.add_argument("--seconds", type=float, default=5.0) parser.add_argument("--frames", help="print these frames in full, e.g. 100-110") parser.add_argument("--mame-switches", action="store_true") args = parser.parse_args() theirs, their_dlls = mame_nmis(args.log, args.seconds) ours, raised, our_dlls = board_nmis(args.cart, args.seconds, args.mame_switches) print(f"NMI entries in {args.seconds} s: MAME {len(theirs)}, board {len(ours)}; " f"IRQ/BRK vector fetches: MAME {IRQS['MAME']}, board {IRQS['board']}") by_t, by_o = collections.defaultdict(list), collections.defaultdict(list) for f, line, clock in theirs: by_t[f].append((line, clock)) for f, line, clock in ours: by_o[f].append((line, clock)) raised_by = collections.defaultdict(list) for f, line, clock, entry in raised: raised_by[f].append((line, clock, entry)) frames = sorted(set(by_t) | set(by_o)) first = next((f for f in frames if len(by_t[f]) != len(by_o[f])), None) hist = collections.Counter((len(by_t[f]), len(by_o[f])) for f in frames) print("frames by (MAME count, board count):", dict(sorted(hist.items()))) lines_t = collections.Counter(line for f in frames for line, _ in by_t[f]) lines_o = collections.Counter(line for f in frames for line, _ in by_o[f]) print("NMI lines, MAME:", sorted(lines_t.items())) print("NMI lines, board:", sorted(lines_o.items())) entries = collections.Counter((line, entry) for f in frames for line, _, entry in raised_by[f]) print("board DLIs raised (line, DLL entry): count", sorted(entries.items())) show = set() if first is not None: print(f"first frame whose count differs: {first}") show.update(range(max(0, first - 2), first + 3)) if args.frames: a, b = (int(x) for x in args.frames.split("-")) show.update(range(a, b + 1)) for f in sorted(show): print(f"frame {f}: MAME {by_t[f]} board {by_o[f]} raised {raised_by[f]}") print(f" MAME DLL {describe(their_dlls.get(f))}") print(f" board DLL {describe(our_dlls.get(f))}") return 0 if __name__ == "__main__": raise SystemExit(main())