// SPDX-License-Identifier: MIT //! 2600 mode: the same SALLY, TIA and RIOT handed to a2600-rust'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, as they stand, to //! `a2600::board::Board` (its socket, docs/api.md there, "The board and the console": //! `Board::new(machine, cartridge, Some(tia), Some(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 [`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. //! //! After the 7800 side has run, [`System2600::system`] *is* an `a2600::system::System`, //! built with `System::with_cpu(board, row, 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 the reference's issue 0001). So //! a2600-rust's gates run on the very type their `SystemFactory` names ([`make_system`] //! is the gates' `system_factory`), and every step, frame and cycle count after the //! hand-off is a2600-rust's code. //! //! Rust-forced differences (a7800-python `mode2600.py` at d6b220d): //! //! - `System2600` is a subclass of a2600-python's `System` in the reference; here it //! holds one ([`System2600::system`]) and the 7800 side it started from //! ([`System2600::seven`]), since a Rust struct cannot extend another. //! - SALLY is a `Mos6502` before the hand-off and must be a //! `Mos6502` after it (a2600-rust's `with_cpu`). It is moved with //! 6502-rust's `capture_state` / `restore_state` (a2600-rust's docs: "a host whose CPU //! sits on another bus type moves it ... first"), and then the rest of its frame (the //! stall fields and `dma_stalled`, which a CPU snapshot leaves to the host) and its //! ANE/LXA constants are copied too, so the adopted CPU is the running one, field for //! field (the reference passes the same object). //! - The 7800 board's TIA is moved out (a fresh one left in its place on the board that //! is no longer run) and its RIOT `Rc` is shared; the cartridge is taken back out of the //! adapter (`into_any`). //! - The adapter's snoop is delivered from the data bus that the board hands `tick` //! (connector.rs), where the reference reads `self.bus.data_bus` from the board it holds. use a2600::board::Board as Board2600; use a2600::cartridge::connector::{Cartridge as Cartridge2600, NoCartridge}; use a2600::cartridge::{load as load2600, mame_hash, LoadedInfo, Options, Rom}; use a2600::machine::Machine as Machine2600; use a2600::system::{System as System2600Base, MOS6507}; use sixfiveohtwo::Mos6502; use crate::board::{EXT, LOCK, MEN, TIA_EN}; use crate::cartridge::connector::{Cartridge, EmptySlot}; use crate::cartridge_any; use crate::machine::{Machine, NTSC, PAL}; use crate::system::System; pub const LOCKED_2600: u8 = LOCK | EXT | TIA_EN; // $0D: the latch after the BIOS's $FD (DOC: [SG] bits) /// 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 /// `tick`: the snoop is delivered there (the connector carries the data bus, /// so this reads a wire, not another part's state). pub struct Cart2600 { pub cartridge: Box, pub snoops: bool, pub unsnooped: bool, // a CPU read whose settled byte the cartridge has not seen pub cycle: u64, } impl Cart2600 { pub fn new(cartridge: Box) -> Cart2600 { let snoops = cartridge.snoops_reads(); Cart2600 { cartridge, snoops, unsnooped: false, cycle: 0, } } } impl Cartridge for Cart2600 { fn name(&self) -> &str { "2600 cartridge" } fn access(&mut self, address: u16, data: u8, read: bool, halt: bool, _t: u64) -> Option { if halt { return None; // MARIA's DMA: none happens in 2600 mode (MARIA in reset, DOC: [MT84]) } let driven = self.cartridge.access(address & 0x1FFF, data, self.cycle, !read); self.cycle += 1; self.unsnooped = read && self.snoops; if read && address & 0x1000 != 0 { driven } else { None } } fn tick(&mut self, data_bus: u8) { if self.unsnooped { self.unsnooped = false; self.cartridge.snoop(data_bus); } } cartridge_any!(); } /// A 7800 running a 2600 cartridge in 2600 mode, started through the BIOS or directly. /// /// The 7800 side ([`System2600::seven`], this board's [`System`]) runs first; then /// [`System2600::system`] is an a2600-rust `System` on the same SALLY, TIA and RIOT, /// with its `machine` the 2600-mode row and [`System2600::machine7800`] the console's own. pub struct System2600 { pub machine7800: Machine, pub seven: System, pub system: System2600Base, pub loaded: Option, } impl System2600 { /// `cartridge` is an a2600-rust cartridge (`a2600::cartridge::load`; /// [`System2600::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. pub fn new( cartridge: Box, machine: Machine, start: &str, bios: Option>, phase_steps: u32, ) -> Result { System2600::new_with(cartridge, machine, start, bios, phase_steps, &mut |_| {}) } /// [`System2600::new`] with `built` called on the 7800 [`System`] as soon as it exists /// (the trace's hook; the reference's trace wraps the module's `System` class instead). pub fn new_with( cartridge: Box, machine: Machine, start: &str, bios: Option>, phase_steps: u32, built: &mut dyn FnMut(&mut System), ) -> Result { let adapter = Cart2600::new(cartridge); let mut seven = System::new( Box::new(adapter), machine, bios, if start == "bios" { "bios" } else { "direct" }, None, )?; built(&mut seven); if start == "direct" { let board = seven.board_mut(); board.inptctrl = LOCKED_2600; board.locked = true; board.inptctrl_writes = 2; board.maria.enabled = false; for _ in 0..phase_steps { board.tia.step(None); } } else { boot(&mut seven, 120)?; } let system = handoff(&mut seven)?; Ok(System2600 { machine7800: machine, seven, system, loaded: None, }) } /// Load `rom` as a2600-rust'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. pub fn from_rom( rom: Rom, machine: Machine, start: &str, bios: Option>, phase_steps: u32, scheme: Option<&str>, options: Options, ) -> Result { System2600::from_rom_with(rom, machine, start, bios, phase_steps, scheme, options, &mut |_| {}) } /// [`System2600::from_rom`] with the [`System2600::new_with`] hook. pub fn from_rom_with( rom: Rom, machine: Machine, start: &str, bios: Option>, phase_steps: u32, scheme: Option<&str>, mut options: Options, built: &mut dyn FnMut(&mut System), ) -> Result { if options.cpu_hz.is_none() { options.cpu_hz = Some(machine.mode_2600.timing.cpu_hz()); } let loaded = load2600(rom, scheme, None, &mame_hash(), options).map_err(|e| e.0)?; let info = loaded.info(); let mut system = System2600::new_with(loaded.cartridge, machine, start, bios, phase_steps, built)?; system.loaded = Some(info); Ok(system) } } /// Run the real BIOS until it locks the latch with MARIA off. pub fn boot(seven: &mut System, frames: u64) -> Result<(), String> { let max_clocks = seven.machine.frame_clocks() * frames; let locked = seven.run_until(|s| s.board().locked && s.board().inptctrl & MEN == 0, max_clocks); if !locked { return Err(format!("the BIOS did not enter 2600 mode within {frames} frames")); } Ok(()) } /// a2600-rust'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. pub fn handoff(seven: &mut System) -> Result { let row = seven.machine.mode_2600; let old = seven.board_mut(); let adapter = std::mem::replace(&mut old.cartridge, Box::new(EmptySlot) as Box); let adapter: Box = adapter .into_any() .downcast() .map_err(|_| "the 7800 board's cartridge is not a 2600 cartridge".to_string())?; let tia = std::mem::take(&mut old.tia); // a fresh TIA left on the board no longer run let mut board = Board2600::new(row, adapter.cartridge, Some(tia), Some(old.riot.clone()), old.periods)?; board.data_bus = old.data_bus; // SALLY onto a2600-rust's bus type, as she stands (the module header). let sally = &seven.cpu; let spare = Board2600::new(row, Box::new(NoCartridge), None, None, 0)?; let mut cpu: MOS6507 = Mos6502::new(spare); cpu.restore_state(&sally.capture_state())?; cpu.frame = sally.frame.clone(); cpu.ane_constant = sally.ane_constant; cpu.lxa_constant = sally.lxa_constant; let (system, _spare) = System2600Base::with_cpu(board, row, cpu); Ok(system) } /// The 2600-mode row -> the console it hosts on. pub fn row_console(machine: &Machine2600) -> Option { [NTSC, PAL].into_iter().find(|m| m.mode_2600 == *machine) } /// a2600-rust's gates' `system_factory`: `machine` is one of a2600-rust's /// 7800 rows (`HOSTED_MACHINES`), started directly. pub fn make_system(cartridge: Box, machine: Machine2600) -> System2600Base { let console = row_console(&machine).expect("a 7800 row (HOSTED_MACHINES)"); System2600::new(cartridge, console, "direct", None, 0) .expect("a direct start") .system }