# SPDX-License-Identifier: MIT """A whole console: SALLY (6502-python's MOS6502) on the :class:`~a7800_python.board.Board`, with a cartridge, started through the BIOS or directly. **SALLY** is Atari's C014806, an NMOS 6502 with a HALT pin; HALT is board logic around the bus callbacks (DOC: [MT84] 2.1, C1; docs/timing.md "SALLY's HALT"), so the class adds nothing to the core but its name. **Starting.** The certified path is the BIOS, found by hash (:mod:`.machine`); the board then starts exactly as the console does, with INPTCTRL at 0 (DOC: [SG]). The other path, ``start="direct"``, is labelled "BIOS skipped": INPTCTRL is preset to $07 (locked, MARIA on, cartridge mapped) and HALT enabled, the CPU runs the cartridge's reset vector, and RAM, the TIA and RIOT state and the position on the frame differ from a real start (docs/start-here.md "Two machines in one case"; Open decision 7). The preset is a labelled convenience, not a hardware value. """ from __future__ import annotations import hashlib import zipfile from collections.abc import Callable from sixfiveohtwo import MOS6502 from .board import EXT, LOCK, MEN, Board from .cartridge import formats from .cartridge.connector import Cartridge from .cartridge.description import CartDescription from .controllers import ProLine from .machine import BIOS_PLACES, BIOSES, NTSC, Machine from .romsets import RomsetUnset, mame_rompath class Sally(MOS6502): """Atari C014806: the NMOS 6502 with HALT (the board holds its clock).""" def find_bios(machine: Machine) -> bytes | None: """The machine's first BIOS image found in place below ``$MAME_ROMPATH``, by hash (None when the variable is unset or no image matches).""" try: sets = mame_rompath() except RomsetUnset: return None for bios in machine.bios: place = BIOS_PLACES.get(bios.sha256) if place is None or not (sets / place[0]).exists(): continue with zipfile.ZipFile(sets / place[0]) as z: data = z.read(place[1]) if hashlib.sha256(data).hexdigest() == bios.sha256: return data return None def check_bios(image: bytes) -> str: """The BIOS row's name for ``image``, or raise if the hash is not a known BIOS.""" digest = hashlib.sha256(image).hexdigest() if digest not in BIOSES: raise ValueError(f"not a known 7800 BIOS (SHA-256 {digest})") return BIOSES[digest].name class System: """Machine + cartridge, powered on.""" def __init__( self, cartridge: Cartridge, machine: Machine = NTSC, *, bios: bytes | None = None, start: str = "bios", description: CartDescription | None = None, ) -> None: if start not in ("bios", "direct"): raise ValueError("start is 'bios' or 'direct'") if start == "bios": if bios is None: bios = find_bios(machine) if bios is None: raise FileNotFoundError( f"no BIOS for {machine.name} found by hash below $MAME_ROMPATH " "(a7800.zip, a7800p.zip); pass bios= or start='direct'" ) self.bios_name = check_bios(bios) else: bios = None self.bios_name = "BIOS skipped (direct start, labelled)" self.machine = machine self.cartridge = cartridge self.description = description self.start = start self.board = board = Board(machine, cartridge, bios) self.cpu = cpu = Sally(board.read_byte, board.write_byte) board.cpu = cpu self.instructions = 0 self.mode_log: list[tuple[int, int]] = [] # (clock, INPTCTRL value) per write board.on_inptctrl = lambda value: self.mode_log.append((board.clocks, value)) self.controllers = ProLine(board.tia, board.riot) self.power() @classmethod def from_rom(cls, rom, machine: Machine = NTSC, **options) -> System: """Load ``rom`` through the one loader (cartridge/formats.py) and power on.""" overrides = options.pop("overrides", None) description = formats.load(rom, overrides=overrides) if description.console != "7800": raise NotImplementedError("2600 dumps run in 2600 mode (milestone 6)") cartridge = formats.build(description) return cls(cartridge, machine, description=description, **options) def power(self) -> None: power = self.machine.power_on cpu = self.cpu cpu.a, cpu.x, cpu.y, cpu.s, cpu.p = power.a, power.x, power.y, power.s, power.p if self.start == "direct": board = self.board board.inptctrl = LOCK | MEN | EXT board.locked = True board.inptctrl_writes = 2 board.maria.enabled = True board.maria.granted = False cpu.request_reset() # -- running --------------------------------------------------------------- @property def clocks(self) -> int: return self.board.clocks @property def mode(self) -> str: """ "bios", "7800" or "2600" from the INPTCTRL latch.""" board = self.board if not board.inptctrl & EXT: return "bios" return "7800" if board.inptctrl & MEN else "2600" def step(self) -> int: """One instruction (or reset, or interrupt entry); returns MARIA clocks it took.""" before = self.board.clocks self.cpu.step() self.instructions += 1 return self.board.clocks - before def run_clocks(self, clocks: int) -> None: board, step = self.board, self.cpu.step end = board.clocks + clocks count = 0 while board.clocks < end: step() count += 1 self.instructions += count def run_frames(self, frames: int) -> None: """Run until MARIA has started ``frames`` more frames.""" maria, step = self.board.maria, self.cpu.step target = maria.frame + frames count = 0 while maria.frame < target: step() count += 1 self.instructions += count def run_until(self, done: Callable[[], bool], *, max_clocks: int) -> bool: board, step = self.board, self.cpu.step end = board.clocks + max_clocks while board.clocks < end: step() self.instructions += 1 if done(): return True return False