"""Readers for the two SingleStepTests 68000 corpora. ``read_m68000(path)`` parses a SingleStepTests/m68000 ``*.json.bin`` file directly (the container is described in docs/validation.md, "Record shape"); ``read_680x0(path)`` adapts a SingleStepTests/680x0 ``*.json.gz`` file to the same :class:`Case` shape. Both corpora are fetched by scripts/fetch_test_vectors.py and are never committed. The m68000 corpus's conventions, kept here rather than converted away: * ``pc`` is MAME's ``m_au``, the address the next prefetch will come from: the executing instruction's address + 4. :attr:`State.pc` keeps that value and :attr:`State.instruction_address` gives the instruction's own address. * ``ram`` is a list of 16-bit words at even addresses. * A transaction's ``data`` for a byte access is the whole 16-bit bus, with the byte in the half its strobe selects (``0xAB00`` for an upper-byte access). """ from __future__ import annotations import gzip import json import struct from dataclasses import dataclass from pathlib import Path #: The pinned corpus commits. scripts/fetch_test_vectors.py fetches these #: and nothing else; the CI cache keys and docs/validation.md carry copies. M68000_REVISION = "64b253116a3de04aaac4346c43680960dc9b67e5" H680X0_REVISION = "e0d5ece9670205cc84a0101081837deb446f86a3" FILE_MAGIC = 0x1A3F5D71 TEST_MAGIC = 0xABC12367 NAME_MAGIC = 0x89ABCDEF STATE_MAGIC = 0x01234567 TRANSACTIONS_MAGIC = 0x456789AB REGISTERS = ("d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "usp", "ssp", "sr", "pc") # fmt: skip #: Transaction kinds in the binary container (docs/validation.md). KIND_IDLE, KIND_WRITE, KIND_READ, KIND_TAS, KIND_READ_ERROR, KIND_WRITE_ERROR = range(6) KIND_NAMES = {1: "w", 2: "r", 3: "t", 4: "re", 5: "we"} @dataclass(frozen=True, slots=True) class State: """One side of a case: registers, SR, the prefetch pair, and RAM words.""" registers: dict[str, int] prefetch: tuple[int, int] ram: dict[int, int] # even address -> 16-bit word (m68000) or byte address -> byte (680x0) @property def pc(self) -> int: return self.registers["pc"] @dataclass(frozen=True, slots=True) class Transaction: """One bus access: kind ("r", "w", "t", "re", "we"), address, size, data.""" kind: str cycles: int fc: int address: int size: str # ".b" or ".w" data: int uds: int lds: int @dataclass(frozen=True, slots=True) class Case: name: str initial: State final: State cycles: int transactions: tuple[Transaction, ...] #: Idle entries ("n", cycles) interleaved with the accesses, for reading only. idle: tuple[tuple[int, int], ...] = () def _state(buffer: bytes, offset: int) -> tuple[State, int]: size, magic = struct.unpack_from(" tuple[int, tuple, tuple, int]: size, magic, cycles, count = struct.unpack_from(" list[Case]: """Parse one SingleStepTests/m68000 ``*.json.bin`` file.""" buffer = Path(path).read_bytes() magic, count = struct.unpack_from(" State: registers = {name: record[name] for name in REGISTERS} ram = dict(record["ram"]) return State(registers, tuple(record["prefetch"]), ram) def read_680x0(path: str | Path) -> list[Case]: """Adapt one SingleStepTests/680x0 ``*.json.gz`` file to :class:`Case`. Differences from the m68000 corpus, normalised here: ``pc`` is the instruction's own address (converted to +4 so both corpora mean the same thing), ``ram`` is byte-addressed (kept byte-addressed; see :attr:`Case.name`), and transactions carry no strobes and a byte-sized ``data`` (the strobes are rebuilt from the address). """ with gzip.open(path, "rt", encoding="utf-8") as handle: records = json.load(handle) cases = [] for record in records: initial = _harte_state(record["initial"]) final = _harte_state(record["final"]) initial.registers["pc"] += 4 final.registers["pc"] += 4 accesses = [] idle = [] for index, entry in enumerate(record.get("transactions", ())): if entry[0] == "n": idle.append((index, entry[1])) continue kind, length, fc, address, size, data = entry[:6] if size == ".b": uds, lds = (0, 1) if address & 1 else (1, 0) data = (data & 0xFF) if address & 1 else (data & 0xFF) << 8 else: uds = lds = 1 accesses.append(Transaction(kind, length, fc, address, size, data, uds, lds)) cases.append( Case(record["name"], initial, final, record["length"], tuple(accesses), tuple(idle)) ) return cases