// 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-rust calls [`Board::read_byte`] or [`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.rs "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.rs). //! //! **The floating bus**: [`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. //! //! Rust-forced differences (a2600-python `board.py` at 28fb095): the RIOT is //! shared (`Rc>`), since a keypad reads its port when the TIA //! is read (in Python every controller holds the RIOT); a handed-in RIOT is //! therefore an `Rc` the host keeps too. The cartridge is a boxed trait //! object. Bus observers are boxed closures `(kind, address, value, cycle)` //! with kind `'R'` or `'W'`. `Board::new` returns `Result` where Python raises //! `ValueError`. The board is the 6502 core's [`sixfiveohtwo::Bus`]. use std::cell::RefCell; use std::rc::Rc; use sixfiveohtwo::{Bus, Frame}; use crate::cartridge::connector::Cartridge; use crate::machine::Machine; use crate::riot::RIOT; use crate::tia::TIA; pub const CYCLE_STEPS: u32 = 6; // half colour clocks per CPU cycle; DOC: Φ0 = OSC / 3 (SPG); DIE: tia-fpga PH0 /// An observer of every CPU-bus cycle: `(kind, address, value, cycle)`. pub type BusObserver = Box; /// 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.rs), so the board must go on /// counting from there. A fresh TIA and RIOT come from the machine row. pub struct Board { pub machine: Machine, pub cartridge: Box, pub snoops: bool, pub tia: TIA, pub riot: Rc>, pub cycles: u64, // CPU cycles (Φ2 pulses) since power, halted ones included pub halted_cycles: u64, // of which spent with RDY low pub data_bus: u8, // Observers of every CPU-bus cycle, (kind, address, value, cycle) with // kind 'R' or 'W'; the trace and the debugger attach here. pub bus_observers: Vec, } impl Board { pub fn new( machine: Machine, cartridge: Box, tia: Option, riot: Option>>, cycles: u64, ) -> Result { let snoops = cartridge.snoops_reads(); let power = machine.power_on; let tia = match tia { Some(tia) => tia, 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 && (2 * power.hclock + power.half_step) % CYCLE_STEPS != 4 { return Err("PowerOn.hclock must put power-on on the CPU cycle grid (2 mod 3)".to_string()); } let mut tia = TIA::new(power.hclock, machine.tia_revision); for _ in 0..power.half_step { tia.step(None); } // 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; tia } }; let riot = match riot { Some(riot) => riot, None => { let mut riot = RIOT::new( power.ram_fill, power.timer_value, power.timer_divider, power.timer_first_decrement, ); riot.input_b = machine.switches; Rc::new(RefCell::new(riot)) } }; Ok(Board { machine, cartridge, snoops, tia, riot, cycles, halted_cycles: 0, data_bus: 0, bus_observers: Vec::new(), }) } // -- one CPU cycle --------------------------------------------------- pub fn read_byte(&mut self, address: u16) -> u8 { let tia = &mut self.tia; loop { tia.step(None); tia.step(None); tia.step(None); if !tia.rdy_low { break; } tia.step(None); // a halted cycle: the 6507 repeats this read tia.step(None); tia.step(None); self.cycles += 1; self.halted_cycles += 1; } tia.step(None); tia.step(None); let address = address & 0x1FFF; let cycle = self.cycles; let driven = self.cartridge.access(address, self.data_bus, cycle, false); let value = if address & 0x1000 != 0 { driven.unwrap_or(self.data_bus) } else if address & 0x80 == 0 { self.tia.read(address) | (self.data_bus & 0x3F) } else { self.riot.borrow_mut().read(address, cycle) }; self.data_bus = value; if self.snoops { self.cartridge.snoop(value); } for observer in self.bus_observers.iter_mut() { observer('R', address, value, cycle); } self.tia.step(None); self.cycles = cycle + 1; value } pub fn write_byte(&mut self, address: u16, value: u8) { for _ in 0..CYCLE_STEPS - 1 { self.tia.step(None); } let address = address & 0x1FFF; let cycle = self.cycles; self.data_bus = value; self.cartridge.access(address, value, cycle, true); if address & 0x1000 != 0 { self.tia.step(None); } else if address & 0x80 == 0 { self.tia.step(Some(((address & 0xFF) as u8, value))); } else { self.riot.borrow_mut().write(address, value, cycle); self.tia.step(None); } for observer in self.bus_observers.iter_mut() { observer('W', address, value, cycle); } self.cycles = cycle + 1; } } impl Bus for Board { fn read(&mut self, address: u16, _frame: &mut Frame) -> u8 { self.read_byte(address) } fn write(&mut self, address: u16, value: u8, _frame: &mut Frame) { self.write_byte(address, value) } }