# SPDX-License-Identifier: MIT """A whole console: 6502-python's MOS6502, as the 6507, on a :class:`~a2600_python.board.Board`. The 6507 is the 6502 die with A13-A15, IRQ and NMI not bonded out (DOC: CSG 6500 data sheet, 6507 pin-out; docs/undocumented-behavior.md "The CPU"): the board masks every address to 13 bits and never requests an interrupt, and nothing else differs (DIE: Sim2600 runs the 6507 netlist, and the board's writes land on its half clocks, scripts/die_board.py). The CPU keeps its 16-bit PC, as MAME and Stella show it (OBSERVED: MAME 0.285, Stella 2c3d702 traces). """ from __future__ import annotations from collections.abc import Callable from pathlib import Path from sixfiveohtwo import MOS6502 # 741419a: bus callables replaceable (read_byte/write_byte) from .board import Board from .cartridge import Loaded, load from .cartridge.connector import Cartridge from .machine import VCS_NTSC, Machine class MOS6507(MOS6502): """The 6507: a 6502 whose board decodes 13 address lines and wires no interrupts.""" class System: """Machine + cartridge, powered on.""" def __init__( self, cartridge: Cartridge, machine: Machine = VCS_NTSC, *, board: Board | None = None, cpu: MOS6502 | None = None, ) -> None: """``board`` hands in a board built elsewhere (with existing chips, gates.py). ``cpu`` hands in a 6502 that has been running: the 7800's SALLY, which executes the BIOS and then jumps through the cartridge's reset vector with nothing resetting it (a7800-python's issue 0001, citing the BIOS disassembly; the 7800's fact, not this board's). Its bus is rewired to this board and it is not powered on: registers, the instruction in progress and any pending request stay as they stand, as the TIA and RIOT handed to :class:`Board` do. Without it a fresh 6507 is powered on.""" self.machine = machine self.cartridge = cartridge self.board = board = board if board is not None else Board(machine, cartridge) self.instructions = 0 self.loaded: Loaded | None = None if cpu is None: self.cpu: MOS6502 = MOS6507(board.read_byte, board.write_byte) self.power() else: cpu.read_byte = board.read_byte # 6502-python's replaceable bus callables cpu.write_byte = board.write_byte self.cpu = cpu @classmethod def from_rom(cls, rom: str | Path | bytes, machine: Machine = VCS_NTSC, **options) -> System: """Load ``rom`` (identified as :func:`.cartridge.load` does; ``options`` go to it). The row's CPU clock goes to the cartridge as ``cpu_hz`` unless ``options`` name one. A clocked scheme's oscillator (DPC's music) runs in its own hertz, free of the console's clock: DOC Crane's patent US 4,644,495 ("an internally generated asynchronous clock", "free running"; the cartridge receives only address, chip-select, data and power lines), DOC Horton (a 560 kOhm resistor on the cartridge and a capacitor on the DPC die set it; touching the resistor bends the pitch, on his own cartridge), DOC nocash (the 24-pin slot carries no clock). So its ticks per CPU cycle follow each row's crystal, and the music plays at the same pitch on every row (docs/cartridge.md).""" if options.get("cpu_hz") is None: options["cpu_hz"] = machine.timing.cpu_hz loaded = load(rom, **options) system = cls(loaded.cartridge, machine) system.loaded = loaded return system def power(self) -> None: """Switch on: registers from the machine's power-on parameters, then the reset sequence.""" 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 cpu.request_reset() # -- running --------------------------------------------------------- @property def cycles(self) -> int: return self.board.cycles def step(self) -> int: """One instruction (or the reset sequence); returns CPU cycles, halted ones included.""" before = self.board.cycles self.cpu.step() self.instructions += 1 return self.board.cycles - before def run_cycles(self, cycles: int) -> None: """Run whole instructions until at least ``cycles`` more CPU cycles have passed.""" board, step = self.board, self.cpu.step end = board.cycles + cycles count = 0 while board.cycles < end: step() count += 1 self.instructions += count def run_frames(self, frames: int) -> None: """Run until the program has started ``frames`` more frames (VSYNC rising edges).""" tia, step = self.board.tia, self.cpu.step target = tia.frame + frames count = 0 while tia.frame < target: step() count += 1 self.instructions += count def run_until(self, done: Callable[[], bool], *, max_cycles: int) -> bool: """Run until ``done()`` (checked after every instruction); False on timeout.""" board, step = self.board, self.cpu.step end = board.cycles + max_cycles while board.cycles < end: step() self.instructions += 1 if done(): return True return False