# SPDX-License-Identifier: MIT """The console board: the 13-bit bus, the floating data bus, WSYNC, and the one clock. Evidence: the cycle grid, the write edge and RDY's sampling are DIE (the tia-fpga traces and this project's Sim2600 runs of the whole board, ``scripts/die_board.py``); the decode is DOC (Stella Programmer's Guide, 6532 data sheet); the floating bus is DOC + OBSERVED (Stella issue #173, MAME 0.285 lockstep, milestone 1). **One timeline** (docs/timing.md "The units and the one timeline"). 6502-python calls :meth:`Board.read_byte` or :meth:`Board.write_byte` exactly once per CPU cycle, in chip order, dummy accesses included, so each call *is* a CPU cycle: the TIA runs the cycle's six half colour clocks, and the access happens on the sixth, where the 6507's Φ2 falls (tia.py "Writes"). A read takes the TIA's D7/D6 as the die drives them after the fifth, while Φ2 is high. ``step()``'s returned count is never added on top. **The cycle grid is the TIA's.** The TIA divides the colour clock by three for the 6507's Φ0 and reloads that divider at every line start, so CPU cycles end on steps 4, 10, 16, ... of every line (tia-fpga: PH0 "rises on exactly the half clock where HBLANK starts"). The power-on point must lie on that grid. **WSYNC as a stretched read** (docs/timing.md "WSYNC, RDY and the stretched read"). A WSYNC write pulls RDY low; the NMOS 6502 ignores RDY on writes and halts on its next read, repeating it until RDY rises. The board samples RDY on each read cycle's third step: Sim2600's 6507 halts a cycle for an RDY that fell on the cycle's first step (a WSYNC that overlapped the release pulse), and lets one run whose RDY rose on the step before it, and RDY only ever changes on a cycle's first or last step, so steps one to five all fit the die (``scripts/die_board.py``). A halted cycle performs no access here (the halted read is always a cartridge-ROM fetch on the 2600, so performing it once is exact). **Decode** (docs/start-here.md "Memory map"): the 6507 has A0-A12, so the board masks to 13 bits; A12 high is the cartridge, else A7 low is the TIA, else the RIOT (which splits RAM from I/O on A9). The cartridge sees every cycle (cartridge/connector.py). **The floating bus**: :attr:`Board.data_bus` is the last byte on the data bus. TIA reads drive D7 and D6 only and the rest float; an undriven read returns the latch. """ from __future__ import annotations from collections.abc import Callable from .cartridge.connector import Cartridge from .machine import Machine from .riot import RIOT from .tia import TIA CYCLE_STEPS = 6 # half colour clocks per CPU cycle; DOC: Φ0 = OSC / 3 (SPG); DIE: tia-fpga PH0 class Board: """The 2600's bus around a TIA, a RIOT and a cartridge. ``tia`` and ``riot`` hand in chips that already exist (the 7800 board's 2600 mode: its BIOS has used both before it locks into 2600 mode, so their state carries over); they are used as they stand, with nothing reset or set from the machine row. ``cycles`` is the count of Φ2 pulses the handed-in RIOT has already seen: its timer is evaluated lazily from the cycle number each access passes (riot.py), so the board must go on counting from there. A fresh TIA and RIOT come from the machine row. """ def __init__( self, machine: Machine, cartridge: Cartridge, *, tia: TIA | None = None, riot: RIOT | None = None, cycles: int = 0, ) -> None: self.machine = machine self.cartridge = cartridge self.snoops = cartridge.snoops_reads power = machine.power_on if tia is None: # DIE: on a 2600 the TIA's Φ0 clocks the 6507, so power-on lies on # its cycle grid; a host that clocks the CPU itself enters anywhere. if machine.tia_clocks_cpu and (2 * power.hclock + power.half_step) % CYCLE_STEPS != 4: raise ValueError("PowerOn.hclock must put power-on on the CPU cycle grid (2 mod 3)") tia = TIA(power.hclock, machine.tia_revision) for _ in range(power.half_step): tia.step() # I0-I3 with joysticks (or nothing) plugged in: no pot, so nothing # charges the paddle capacitors and they read 0 (C19); I4/I5 have # pull-ups. controllers.Paddles supplies them when paddles are in. tia.inputs = 0x30 self.tia = tia if riot is None: riot = RIOT( power.ram_fill, power.timer_value, power.timer_divider, power.timer_first_decrement ) riot.input_b = machine.switches self.riot = riot self.cycles = cycles # CPU cycles (Φ2 pulses) since power, halted ones included self.halted_cycles = 0 # of which spent with RDY low self.data_bus = 0 # Observers of every CPU-bus cycle, (kind, address, value, cycle) with # kind "R" or "W"; the trace and the debugger attach here. self.bus_observers: list[Callable[[str, int, int, int], None]] = [] # -- one CPU cycle --------------------------------------------------- def read_byte(self, address: int) -> int: tia = self.tia step = tia.step while True: step() step() step() if not tia.rdy_low: break step() # a halted cycle: the 6507 repeats this read step() step() self.cycles += 1 self.halted_cycles += 1 step() step() address &= 0x1FFF cycle = self.cycles driven = self.cartridge.access(address, self.data_bus, cycle) if address & 0x1000: value = self.data_bus if driven is None else driven elif not address & 0x80: value = tia.read(address) | (self.data_bus & 0x3F) else: value = self.riot.read(address, cycle) self.data_bus = value if self.snoops: self.cartridge.snoop(value) if self.bus_observers: for observer in self.bus_observers: observer("R", address, value, cycle) step() self.cycles = cycle + 1 return value def write_byte(self, address: int, value: int) -> None: tia = self.tia step = tia.step for _ in range(CYCLE_STEPS - 1): step() address &= 0x1FFF cycle = self.cycles self.data_bus = value self.cartridge.access(address, value, cycle, True) if address & 0x1000: step() elif not address & 0x80: step((address, value)) else: self.riot.write(address, value, cycle) step() if self.bus_observers: for observer in self.bus_observers: observer("W", address, value, cycle) self.cycles = cycle + 1