# SPDX-License-Identifier: MIT """Public ARM7TDMI CPU API. The host owns memory and every device. It passes ``read``, ``write`` and ``idle`` in, calls :meth:`ARM7TDMI.step`, and advances its devices **inside** the callbacks: every bus cycle is one callback, in chip order, internal cycles included, so the host never adds ``step()``'s return value on top. See docs/handoff-brief.md "The bus contract". """ from __future__ import annotations from collections.abc import Callable from arm7tdmi_python._alu import ALUMixin from arm7tdmi_python._block import BlockMixin from arm7tdmi_python._branch import BranchMixin from arm7tdmi_python._exception import ExceptionMixin from arm7tdmi_python._modes import MASK_F, MASK_I, SVC from arm7tdmi_python._multiply import MultiplyMixin from arm7tdmi_python._psr import PSRMixin from arm7tdmi_python._swap import SwapMixin from arm7tdmi_python._tables import ARM_HANDLERS, THUMB_HANDLERS from arm7tdmi_python._transfer import TransferMixin Read = Callable[[int, int, int], int] Write = Callable[[int, int, int, int], None] Idle = Callable[[int], None] def _no_idle(address: int) -> None: """The default ``idle``: a host that ignores internal cycles.""" class ARM7TDMI( ALUMixin, PSRMixin, MultiplyMixin, TransferMixin, BlockMixin, SwapMixin, BranchMixin, ExceptionMixin, ): """An ARM7TDMI core whose bus is callables supplied by a host. - ``read(address, width, access) -> int``: one N or S cycle that reads ``width`` (8, 16 or 32) bits; ``access`` is a set of :data:`~arm7tdmi_python.access.SEQ`, :data:`~arm7tdmi_python.access.CODE` and :data:`~arm7tdmi_python.access.LOCK`. - ``write(address, width, value, access)``: one N or S cycle that writes. - ``idle(address)``: one internal cycle; ``address`` is what the core drives. A CP14 coprocessor cycle also comes here, with ``cpu.coprocessor_cycle`` true. The address is the one the core computes, low bits included; aligning it is the memory's job. ``irq`` and ``fiq`` are the interrupt lines (True = asserted), sampled at the start of each ``step()``. A new CPU is in the reset state; call :meth:`reset` to make the two refill fetches at address 0, or load a state with :meth:`restore_state`. """ def __init__(self, read: Read, write: Write, idle: Idle | None = None) -> None: for name, bus in (("read", read), ("write", write)): if not callable(bus): raise TypeError(f"{name} must be callable") if idle is not None and not callable(idle): raise TypeError("idle must be callable") self._bus_read = read self._bus_write = write self._bus_idle = idle if idle is not None else _no_idle self._init_registers() self._arm_table = [getattr(self, name) for name in ARM_HANDLERS] self._thumb_table = [getattr(self, name) for name in THUMB_HANDLERS] def reset(self) -> None: """nRESET: Supervisor mode, ARM state, I and F set, PC = 0, then the two refill fetches, N at 0 and S at 4 (TRM 2.10, 3.10). Other registers keep their values.""" self._switch_mode(SVC) self.cpsr = (self.cpsr | MASK_I | MASK_F) & ~0x20 self._cycles = 0 self._set_pc(0) def start_at(self, address: int) -> None: """R15 <- ``address`` and the two refill fetches, as a branch there would (in the current state, ARM or Thumb). For hosts that start without a BIOS: the minimal harness, and a board's skip-BIOS start.""" self._cycles = 0 self._set_pc(address)