# SPDX-License-Identifier: MIT """Deterministic JSON Lines traces: one record per instruction, one per host access. Schema (version 1), one JSON object per line: - ``{"type": "step", "version": 1, "sequence": n, "pc": .., "word": .., "text": "..", "before": {register: value}, "after": {register: value}, "ram": [[kind, index, value]]}`` where the registers are :data:`~upd7725_python.state.REGISTER_NAMES` plus ``stack`` and ``ram`` lists the data RAM accesses (``debug.ram_accesses``); - ``{"type": "host", "version": 1, "direction": "R"|"W", "port": "DR"|"SR", "value": .., "instruction": n}``, a host access made after ``n`` instructions. Two cores that write equal traces for the same program and host stream are, as far as the host can tell, the same chip. ``first_divergence`` compares two traces record by record. """ from __future__ import annotations import json from collections.abc import Iterable, Iterator from itertools import zip_longest from typing import TextIO from upd7725_python.debug import HostAccess, StepRecord from upd7725_python.state import REGISTER_NAMES, CPUState TRACE_SCHEMA_VERSION = 1 def _registers(state: CPUState) -> dict[str, object]: out: dict[str, object] = {name: getattr(state, name) for name in REGISTER_NAMES} out["stack"] = list(state.stack) return out def step_to_dict(record: StepRecord) -> dict[str, object]: return { "type": "step", "version": TRACE_SCHEMA_VERSION, "sequence": record.sequence, "pc": record.before.pc, "word": record.instruction.word, "text": record.instruction.text, "before": _registers(record.before), "after": _registers(record.after), "ram": [list(access) for access in record.ram_accesses], } def host_to_dict(access: HostAccess) -> dict[str, object]: return { "type": "host", "version": TRACE_SCHEMA_VERSION, "direction": access.direction, "port": access.port, "value": access.value, "instruction": access.instruction, } def encode(record: StepRecord | HostAccess) -> str: data = step_to_dict(record) if isinstance(record, StepRecord) else host_to_dict(record) return json.dumps(data, separators=(",", ":"), sort_keys=True) def write_trace(records: Iterable[StepRecord | HostAccess], stream: TextIO) -> int: count = 0 for record in records: stream.write(encode(record) + "\n") count += 1 return count def read_trace(stream: TextIO) -> Iterator[dict[str, object]]: """Yield the records as dictionaries, checking type and version.""" for number, line in enumerate(stream, start=1): if not line.strip(): continue record = json.loads(line) if record.get("version") != TRACE_SCHEMA_VERSION or record.get("type") not in ( "step", "host", ): raise ValueError(f"line {number}: not a version {TRACE_SCHEMA_VERSION} record") yield record def first_divergence( left: Iterable[dict[str, object]], right: Iterable[dict[str, object]] ) -> tuple[int, dict | None, dict | None] | None: """The first position where two traces differ (ignoring ``text``), or ``None``.""" for position, (a, b) in enumerate(zip_longest(left, right)): if a is None or b is None: return position, a, b if {k: v for k, v in a.items() if k != "text"} != { k: v for k, v in b.items() if k != "text" }: return position, a, b return None __all__ = [ "TRACE_SCHEMA_VERSION", "encode", "first_divergence", "host_to_dict", "read_trace", "step_to_dict", "write_trace", ]