// SPDX-License-Identifier: MIT //! The cartridge connector, as signals: the only thing a scheme knows of the console. //! //! docs/cartridge.md "The connector is 24 pins and has no R/W line" has the reasons. //! A scheme module imports this module and nothing else of the board //! (`tests/cartridge_boundary.rs` enforces it). //! //! - **Every CPU cycle**, whatever its address, reaches [`Cartridge::access`]: //! the slot carries A0-A12 always, and schemes watch TIA writes (3F) and stack //! accesses (FE) as well as their own window. //! - **No R/W.** `value` is the byte on the data bus as the cartridge would see //! it before driving: the CPU's byte on a write cycle, the floating bus on a read //! cycle. A scheme decides by address alone, as the hardware must; a Super //! Chip's write window stores `value` either way, which is how a read of the //! write window corrupts RAM on a console. //! - `write` says whether the CPU drives this cycle. The slot has no such pin, //! so a scheme may use it only where its documentation says the cartridge //! reacts to writes alone and the mechanism is not known (3F: Horton, //! "writing to any location between 0000 and 003F"); each such use is tagged //! HYPOTHESIS for the mechanism. `tests/cartridge_boundary.rs` lists the schemes //! allowed to read it. //! - **The return value** is the byte the cartridge drives, or `None` if it //! leaves the bus alone. The board uses it on read cycles only; on a write //! cycle the CPU drives the bus. //! - **Snooping.** A scheme that latches a byte someone else drives (FE takes the //! bank from D5 of the byte after a $01FE access, and on an RTS that byte comes //! from RIOT RAM) sets `snoops_reads`; the board then calls [`Cartridge::snoop`] with //! the byte that settled on the bus at the end of every read cycle. //! - `t` is the CPU cycle count at the access (the board's one timeline), for //! schemes with their own clock (the DPC's music oscillator). //! //! Rust-forced differences (a2600-python `cartridge/connector.py` at 28fb095): //! the base class is a trait; Python's class attributes `name` and //! `snoops_reads` are methods; `write` has no default; the class "no //! cartridge" is [`NoCartridge`]. Each scheme also says how to save and load //! its state (`save_state`, `load_state`: bincode of the scheme's fields) and //! can be downcast (`as_any`), neither of which the reference needs. use std::any::Any; pub trait Cartridge: Any { /// The scheme's name ("F6SC", "E7", ...). fn name(&self) -> &str; /// Whether the board must call [`Cartridge::snoop`] after every read cycle. fn snoops_reads(&self) -> bool { false } /// One CPU cycle; `address` is A0-A12. fn access(&mut self, address: u16, value: u8, t: u64, write: bool) -> Option; /// The byte on the data bus at the end of the read cycle just accessed. fn snoop(&mut self, _value: u8) {} /// The console's power was cycled with the cartridge in (32IN1 counts these). fn reset(&mut self) {} /// The scheme's whole state, for a save state. fn save_state(&self) -> Vec; /// Restore what [`Cartridge::save_state`] wrote. fn load_state(&mut self, data: &[u8]) -> Result<(), String>; fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; } /// `save_state`, `load_state`, `as_any` and `as_any_mut` for a scheme whose /// fields are all serde (skipped fields keep the loading console's). #[macro_export] macro_rules! cartridge_state { () => { fn save_state(&self) -> Vec { bincode::serialize(self).expect("cartridge state") } fn load_state(&mut self, data: &[u8]) -> Result<(), String> { let loaded: Self = bincode::deserialize(data).map_err(|e| e.to_string())?; $crate::cartridge::connector::Restore::restore(self, loaded); Ok(()) } fn as_any(&self) -> &dyn std::any::Any { self } fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self } }; } /// How a loaded state replaces a scheme's fields: all of them, unless the /// scheme keeps a host attachment (the Supercharger's tape input). pub trait Restore: Sized { fn restore(&mut self, loaded: Self) { *self = loaded; } } /// No cartridge: nothing driven, nothing remembered. #[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct NoCartridge; impl Restore for NoCartridge {} impl Cartridge for NoCartridge { fn name(&self) -> &str { "none" } fn access(&mut self, _address: u16, _value: u8, _t: u64, _write: bool) -> Option { None } cartridge_state!(); }