# SPDX-License-Identifier: MIT """A flat-memory host for the core: 64 KB of RAM and nothing else. Not a machine: no devices, no time. It is what the self-checking test programs (Dormann's, Clark's), the directed tests and ``python -m w65c02_python`` need: load an image, start at an address or through the reset vector, run until the program traps on a jump or branch to itself. """ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass from w65c02_python.cpu import CMOS65C02, G65SC02, R65C02, W65C02S, Lynx2Cell #: Row names as ``python -m w65c02_python --row`` takes them. ROWS: dict[str, type[CMOS65C02]] = { row.__name__.lower(): row for row in (W65C02S, R65C02, G65SC02, Lynx2Cell) } @dataclass class RunResult: """Why :meth:`Harness.run` stopped, and when.""" reason: str # "trap", "condition", "halted" or "cycles" cycles: int pc: int class Harness: """Run a memory image on a 65C02 with 64 KB of RAM. ``memory`` is the RAM, a bytearray a test can read results from; ``cpu`` is the core, of ``row`` (a CPU class). ``cycles`` counts bus callbacks. """ def __init__(self, row: type[CMOS65C02] = W65C02S) -> None: self.memory = bytearray(0x10000) self.cycles = 0 self.cpu = row(self.read_byte, self.write_byte) def load(self, image: bytes, address: int = 0) -> None: """Copy ``image`` into memory at ``address``.""" if address + len(image) > 0x10000: raise ValueError("the image does not fit in 64 KB from that address") self.memory[address : address + len(image)] = image def start(self, address: int | None = None) -> None: """Run the reset sequence; then, with ``address``, set PC to it (else the vector's).""" self.cpu.request_reset() self.cpu.step() if address is not None: self.cpu.pc = address # -- the bus --------------------------------------------------------- def read_byte(self, address: int) -> int: self.cycles += 1 return self.memory[address] def write_byte(self, address: int, value: int) -> None: self.cycles += 1 self.memory[address] = value def peek(self, address: int) -> int: """The byte at ``address`` without a bus cycle (a debugger's view).""" return self.memory[address] # -- running ----------------------------------------------------------- def run( self, max_cycles: int, until: Callable[[Harness], bool] | None = None, step: Callable[[], object] | None = None, ) -> RunResult: """Step until the program traps (an instruction that leaves PC where it was: a jump or branch to itself), ``until(self)`` (checked after each step), the CPU stops (``STP``, or ``WAI`` with nothing to wake it) or ``max_cycles`` more cycles. ``step`` replaces ``cpu.step`` (a debug session's, to trace the run).""" cpu = self.cpu step = cpu.step if step is None else step limit = self.cycles + max_cycles reason = "cycles" while self.cycles < limit: before = cpu.pc step() if cpu.halted: reason = "halted" break if cpu.pc == before: reason = "trap" break if until is not None and until(self): reason = "condition" break return RunResult(reason, self.cycles, cpu.pc) __all__ = ["ROWS", "Harness", "RunResult"]