# SPDX-License-Identifier: MIT """2600 mode: the same SALLY, TIA and RIOT handed to a2600-python's board. When INPTCTRL is locked with MEN = 0 (the BIOS's last write on the 2600 path is $FD to COLUPF: lock, EXT, TIA-EN, MARIA off; DOC: the wiki's BIOS disassembly), the 7800 is a 2600 built from the same parts (DOC: [MT84] 2.1: MARIA "held in a passive reset state", providing "address mapping identical to that of the Atari 2600"). This module hands the CPU, TIA and RIOT objects, as they stand, to ``a2600_python.board.Board`` (its socket, docs/api.md there, "The board and the console": ``Board(machine, cartridge, *, tia=, riot=, cycles=)``), with the RIOT's Φ2 pulse count so far, and the machine row ``NTSC_7800`` / ``PAL_7800`` from its ``HOSTED_MACHINES`` (``tia_clocks_cpu`` False: MARIA's divider clocks the CPU, so the board keeps whatever phase the TIA stands at; HYPOTHESIS, U11: the Stella #699 PAL 7800 rows and a2600-python's die traces bound it). Before the hand-off the 2600 cartridge sits on the 7800 board's connector through :class:`Cart2600`: it sees every CPU cycle with A0-A12 only, as a 2600 cartridge in the middle 24 contacts does (DOC: the connector, [SG] "Cartridge"), so a bank-switching cartridge sees the BIOS's reads of its hot spots too. :class:`System2600` *is* an ``a2600_python.system.System``: after the 7800 side has run, it is built with ``System(cartridge, row, board=Board(...), cpu=SALLY)``, which adopts the CPU as it stands (registers, the instruction in progress, any pending request) and does not power it on (a2600-python's docs/api.md, granted for this board's issue 0001 at ``ae3ad35``, pinned at ``28fb095``). So ``a2600_python.gates`` run on the very class their ``SystemFactory`` names (``make_system`` is the gates' ``system_factory``), and every step, frame and cycle count after the hand-off is a2600-python's code. """ from __future__ import annotations from a2600_python.board import Board as Board2600 from a2600_python.cartridge import load as load2600 from a2600_python.cartridge.connector import Cartridge as Cartridge2600 from a2600_python.system import System as System2600Base from .board import EXT, LOCK, MEN, TIA_EN from .cartridge.connector import Cartridge from .machine import NTSC, PAL, Machine from .system import System LOCKED_2600 = LOCK | EXT | TIA_EN # $0D: the latch after the BIOS's $FD (DOC: [SG] bits) class Cart2600(Cartridge): """A 2600 cartridge on the 7800's connector: A0-A12, no R/W, one call per CPU cycle. a2600-python's connector protocol (its cartridge/connector.py): ``access`` every CPU cycle with the bus byte and the cycle count, ``write`` only for the schemes its documentation allows, and ``snoop`` with the byte that settled at the end of a read cycle for the schemes that latch it (FE). The 7800 board's connector protocol has no such call, but the byte is the board's data-bus latch when the read's Φ2 period ends, which is when the board calls :meth:`tick`: the snoop is delivered there (the connector carries the data bus, so this reads a wire, not another part's state). ``bus`` is set by :class:`System2600` to the 7800 board.""" name = "2600 cartridge" def __init__(self, cartridge: Cartridge2600) -> None: self.cartridge = cartridge self.snoops = cartridge.snoops_reads self.unsnooped = False # a CPU read whose settled byte the cartridge has not seen self.bus = None # the 7800 board, for its data-bus latch self.cycle = 0 def access(self, address: int, data: int, read: bool, halt: bool, t: int) -> int | None: if halt: return None # MARIA's DMA: none happens in 2600 mode (MARIA in reset, DOC: [MT84]) driven = self.cartridge.access(address & 0x1FFF, data, self.cycle, not read) self.cycle += 1 self.unsnooped = read and self.snoops return driven if read and address & 0x1000 else None def tick(self) -> None: if self.unsnooped: self.unsnooped = False self.cartridge.snoop(self.bus.data_bus) class System2600(System2600Base): """A 7800 running a 2600 cartridge in 2600 mode, started through the BIOS or directly. The 7800 side (:attr:`system`, this board's :class:`~a7800_python.system.System`) runs first; then the class is an a2600-python ``System`` on the same SALLY, TIA and RIOT, with ``machine`` the 2600-mode row and :attr:`machine7800` the console's own.""" def __init__(self, cartridge: Cartridge2600, machine: Machine = NTSC, *, start: str = "direct", bios: bytes | None = None, phase_steps: int = 0) -> None: # fmt: skip """``cartridge`` is an a2600-python cartridge (``a2600_python.cartridge.load``; :meth:`from_rom` loads one). ``phase_steps`` (direct start only) steps the TIA that many half colour clocks before the hand-off, putting the CPU on another of the six phases (HYPOTHESIS, U11); 0 is the 2600's own phase.""" self.machine7800 = machine self.adapter = Cart2600(cartridge) self.system = System(self.adapter, machine, bios=bios, start="bios" if start == "bios" else "direct") # fmt: skip self.adapter.bus = self.system.board if start == "direct": board = self.system.board board.inptctrl, board.locked, board.inptctrl_writes = LOCKED_2600, True, 2 board.maria.enabled = False for _ in range(phase_steps): board.tia.step() else: self.boot() self.handoff(cartridge) @classmethod def from_rom(cls, rom, machine: Machine = NTSC, *, start: str = "direct", bios: bytes | None = None, phase_steps: int = 0, **options) -> System2600: # fmt: skip """Load ``rom`` as a2600-python's ``System.from_rom`` does (``options`` go to its ``cartridge.load``; the 2600-mode row's CPU clock goes as ``cpu_hz`` unless they name one, for a clocked scheme's oscillator, DPC's music: a2600-python's docs/api.md), then start it on this console.""" if options.get("cpu_hz") is None: options["cpu_hz"] = machine.mode_2600.timing.cpu_hz loaded = load2600(rom, **options) system = cls(loaded.cartridge, machine, start=start, bios=bios, phase_steps=phase_steps) system.loaded = loaded return system def boot(self, frames: int = 120) -> None: """Run the real BIOS until it locks the latch with MARIA off.""" system, board = self.system, self.system.board locked = system.run_until( lambda: board.locked and not board.inptctrl & MEN, max_clocks=self.machine7800.frame_clocks * frames, ) if not locked: raise RuntimeError(f"the BIOS did not enter 2600 mode within {frames} frames") def handoff(self, cartridge: Cartridge2600) -> None: """a2600-python's board on the 7800's TIA and RIOT with the RIOT's Φ2 count, the data-bus latch as it stands, and SALLY adopted as it stands.""" old = self.system.board board = Board2600(self.machine7800.mode_2600, cartridge, tia=old.tia, riot=old.riot, cycles=old.periods) # fmt: skip board.data_bus = old.data_bus super().__init__(cartridge, self.machine7800.mode_2600, board=board, cpu=self.system.cpu) # fmt: skip ROWS = {NTSC.mode_2600: NTSC, PAL.mode_2600: PAL} def make_system(cartridge: Cartridge2600, machine) -> System2600: """``a2600_python.gates``' ``system_factory``: ``machine`` is one of a2600-python's 7800 rows (``HOSTED_MACHINES``), started directly.""" return System2600(cartridge, ROWS[machine], start="direct")