# SPDX-License-Identifier: MIT """Run SingleStepTests 65816 cases (Tom Harte, JSMoo; @ 307c63f) through the core. Each case gives an initial state, the RAM it touches, the final state and every cycle as [address, value, flags], flags being eight characters: VDA, VPA, VPB, R/W, E, M, X, MLB ('d', 'p', 'v', 'r' or 'w', 'e', 'm', 'x', 'l', or '-'). Emulator-derived (validation.md): a detector, with these readings decided by the user (handoff-brief.md): - the initial state gets SH = $01 in emulation mode and XH = YH = 0 when X = 1 (DS sections 2.11, 2.7; the corpus stores whatever its generator held); - C1: the two pointer reads of JMP/JSR (a,x) ($7C, $FC) are VPA in the core, VDA in the corpus: their VDA/VPA are not compared; - C2: WDM's ($42) second cycle is a read in the core, IO in the corpus: its kind is not compared; - C4: (d,x) in emulation mode with D.l not 0 and the pointer's low byte at $xxFF reads the high byte from the same page in the core, the next page in the corpus: those cases are excluded (counted by the caller); - C9: RTI ($40) shows the pulled P on the M/X pins from the cycle after its pull in the core, only after the instruction in the corpus: the E/M/X pins of its cycles after the pull are not compared; - WAI and STP end with a [null, null, '--------'] row, the stopped clock. """ import json from functools import cache from pathlib import Path from w65c816 import W65C816 CORPUS = Path(__file__).resolve().parents[1] / "reference" / "src" / "65816" / "v1" FETCH_HINT = "SingleStepTests 65816 not fetched: python -I scripts/fetch_references.py" C1_OPCODES = {0x7C, 0xFC} C4_OPCODES = {0x01, 0x21, 0x41, 0x61, 0x81, 0xA1, 0xC1, 0xE1} RTI_P_PULL = 3 # RTI's cycle that pulls P: opcode, IO, IO, P (DS Table 5-7 row 22g) REGISTERS = ("pc", "s", "p", "a", "x", "y", "dbr", "d", "pbr", "e") def corpus_present() -> bool: return (CORPUS / "a9.n.json").is_file() @cache def load_cases(name: str) -> list[dict]: return json.loads((CORPUS / f"{name}.json").read_text()) def initial_state(state: dict) -> dict: state = dict(state) if state["e"]: state["s"] = 0x0100 | (state["s"] & 0xFF) state["p"] |= 0x30 if state["p"] & 0x10: state["x"] &= 0xFF state["y"] &= 0xFF return state def excluded(opcode: int, case: dict) -> bool: """C4: the corpus reads (d,x)'s high byte from the next page; the core does not.""" if opcode not in C4_OPCODES: return False state = initial_state(case["initial"]) if not state["e"] or not state["d"] & 0xFF: return False ram = dict(case["initial"]["ram"]) operand = ram.get(state["pbr"] << 16 | (state["pc"] + 1) & 0xFFFF, 0) return (state["d"] + operand + state["x"]) & 0xFF == 0xFF def run_case(case: dict) -> list[str]: """Run one case; return a description of each difference (empty: it passes).""" state = initial_state(case["initial"]) memory = dict(case["initial"]["ram"]) seen: list[tuple] = [] def kinds(write: bool) -> tuple[bool, ...]: return (cpu.vda, cpu.vpa, cpu.vpb, write, cpu.mlb, bool(cpu.e), cpu.m, cpu.x_flag) def read_byte(address: int) -> int: value = memory.get(address, 0) seen.append((address, value, kinds(False))) return value def write_byte(address: int, value: int) -> None: memory[address] = value seen.append((address, value, kinds(True))) def idle(address: int) -> None: seen.append((address, None, kinds(False))) cpu = W65C816(read_byte, write_byte, idle) for name in REGISTERS: setattr(cpu, name, state[name]) opcode = memory.get(state["pbr"] << 16 | state["pc"], 0) expected = [row for row in case["cycles"] if row[0] is not None] while len(seen) < len(expected): before = len(seen) cpu.step() if len(seen) == before or opcode in (0xCB, 0xDB): # WAI, STP: one step break problems = [] final = case["final"] for name in REGISTERS: got = int(getattr(cpu, name)) if got != final[name]: problems.append(f"{name}: got ${got:X}, want ${final[name]:X}") for address, value in final["ram"]: if memory.get(address, 0) != value: problems.append( f"ram ${address:06X}: got ${memory.get(address, 0):02X}, want ${value:02X}" ) if len(seen) != len(expected): problems.append(f"cycles: got {len(seen)}, want {len(expected)}") for index, ((address, value, flags), (got_address, got_value, got_kinds)) in enumerate( zip(expected, seen, strict=False) ): want_kinds = ( flags[0] == "d", flags[1] == "p", flags[2] == "v", flags[3] == "w", flags[7] == "l", flags[4] == "e", flags[5] == "m", flags[6] == "x", ) if opcode in C1_OPCODES and index >= len(expected) - 2: want_kinds, got_kinds = want_kinds[2:], got_kinds[2:] if opcode == 0x42 and index == 1: want_kinds = got_kinds[:5] + want_kinds[5:] # C2; the E/M/X pins still count if opcode == 0x40 and index > RTI_P_PULL: want_kinds, got_kinds = want_kinds[:5], got_kinds[:5] # C9 if got_address != address: problems.append(f"cycle {index}: address ${got_address:06X}, want ${address:06X}") if value is not None and got_value is not None and got_value != value: problems.append(f"cycle {index}: value ${got_value:02X}, want ${value:02X}") if got_kinds != want_kinds: problems.append(f"cycle {index}: kinds {got_kinds}, want {want_kinds} ({flags})") return problems