# SPDX-License-Identifier: MIT """Harness settings shared by the detector comparisons (lockstep_mame.py, frames_mame.py). Each one makes the board look like MAME 0.285 where the two differ by a *setting* or by a *named, counted exclusion*; none is a board behaviour, and each run reports how often it acted. MAME is a detector (docs/validation.md). - :func:`mame_switches`: the console switches as MAME defaults them, as measured: Asteroids's ``LDX SWCHB`` loads $EB on MAME (both difficulty switches at A, SWCHB D7 = D6 = 1) and $2B on the board; the board's machine row has them at B. A user setting. - :func:`mask_tia_undriven` (C17): MAME drives all eight bits of a TIA read. Measured from the outside, in its own logged reads ($00-$1F) over six 13-second traces (Centipede, Ms. Pac-Man, Joust, Galaga, Robotron, Food Fight; 152,000 reads): $x0-$x7 always $00, $x8-$xB $00 and $xC/$xD $80 with nothing pressed (D6-D0 always 0), $xE/$xF always $FF. The rule below is that measurement. The TIA drives D7 (and D6 on the collision latches) and floats the rest, which then read the bus's last byte; the 2600 board settled this against its die and MAME's 2600 driver agrees, and the 7800 wiki's console-verified list says M.I.A. hangs at level 2 unless the undriven bits hold the last opcode byte (docs/undocumented-behavior.md C17). With the exclusion the board's reads in 7800 mode take MAME's bits, keeping the board's D7. - :func:`force_timer`: the board's k-th read of the 6532 timer (INTIM/TIMINT) returns MAME's k-th value. The timer counts Φ2 periods on the board and CPU cycles at 1.79 MHz on MAME, which never stretches TIA/RIOT cycles (C11, C12): a value read early in a cartridge differs and any game that seeds a random number from it diverges from then on. """ from __future__ import annotations from pathlib import Path def mame_switches(board) -> None: board.riot.input_b |= 0xC0 def mask_tia_undriven(board) -> dict: read_device = board._read_device count = {"reads": 0, "differed": 0} def read(address): value = read_device(address) if board.inptctrl & 2 and address < 0x400 and not address & 0xE0: register = address & 0x0F if register < 8: mame_like = 0x00 elif register < 0x0E: mame_like = value & 0x80 else: mame_like = 0xFF count["reads"] += 1 count["differed"] += mame_like != value return mame_like return value board._read_device = read return count def mask_open_bus(board) -> dict: """C18, a named exclusion: a CPU read that no device and no cartridge drives returns the bus's last byte on the board (normally the address's high byte, the instruction's last operand byte) and $00 on MAME 0.285 (measured: Ms. Pac-Man's ``LDA $3900`` loads $00 in MAME's trace, $39 on the board). With the exclusion the board returns $00 there. MARIA's DMA reads are left alone.""" access = board.cartridge.access read_device = board._read_device last = {"driven": None, "halt": False} count = {"reads": 0, "differed": 0} def access_seen(address, data, read, halt, clock): driven = access(address, data, read, halt, clock) last["driven"], last["halt"] = driven, halt return driven def read(address): value = read_device(address) if value is None and last["driven"] is None and not last["halt"]: count["reads"] += 1 count["differed"] += board.data_bus != 0 return 0x00 return value board.cartridge.access = access_seen board._read_device = read return count def mame_timer_reads(log: Path) -> list[int]: """MAME's 6532 timer reads (INTIM, TIMINT) in order, from mame_trace.py's error.log.""" values = [] with log.open("rb") as handle: for raw in handle: if raw.startswith(b"R 28") or raw.startswith(b"R 29"): parts = raw.split() address = int(parts[1], 16) if 0x280 <= address < 0x2A0 and address & 0x04: values.append(int(parts[2], 16)) return values def force_timer(board, values: list[int]) -> dict: riot_read = board.riot.read count = {"n": 0, "differed": 0} def read(address, cycle): value = riot_read(address, cycle) if address & 0x204 == 0x204 and count["n"] < len(values): count["differed"] += value != values[count["n"]] if len(count.setdefault("first", [])) < 4: count["first"].append((value, values[count["n"]])) # (board, MAME) value = values[count["n"]] count["n"] += 1 return value board.riot.read = read return count