// SPDX-License-Identifier: MIT //! The 32-pin connector protocol (docs/cartridge.md "The connector"). //! //! The board calls [`Cartridge::access`] once for **every** bus cycle: the //! CPU's cycles (dummy ones included) with `halt` false and each of MARIA's DMA //! reads with `halt` true, with the full 16-bit address. The return value is //! the byte the cartridge drives, or `None` when it drives nothing (the data //! bus keeps its last value). `t` is the MARIA clock of the access. //! //! Separately, [`Cartridge::tick`] is called once per Φ2 period, after that period's //! access (DOC: [1702B] p. 29, C22), DMA periods included (DOC: two reports, no //! trace; U10: a PCLK0 capture across DMA would settle it; docs/timing.md "Φ2 //! periods at the cartridge port"); cartridge sound chips (POKEY) count on it. //! `irq` is the level of the cartridge's IRQ pin, read by the board after every //! period (the board's sampling granularity: not a hardware value). //! //! The console's own devices win where they respond (DOC: [MT84] Figure 2); the //! cartridge sees every cycle anyway (bank-select hot spots decode writes; banksets //! decode HALT; DOC: the connector carries the whole bus, [SG] "Cartridge"). //! //! Rust-forced differences (a7800-python `cartridge/connector.py` at d6b220d): the //! protocol is a trait. `tick` is handed the data bus as it stands at the period's end //! (`data_bus`): the reference's 2600 adapter reads it from the board it holds //! (`self.bus.data_bus`, "the connector carries the data bus, so this reads a wire"), //! and a Rust cartridge cannot hold the board that owns it. `irq` is a method. //! `snapshot` / `restore` are bytes (the reference's dictionaries, serialised). //! `pokeys` gives the cartridge's POKEY chips to the sound tap (the reference's //! `getattr(board.cartridge, "pokeys", {})`), shared (`Rc>`) because the //! TIA's sample callback reads them while the cartridge owns them. `as_any` / //! `into_any` let 2600 mode take its cartridge back from the adapter at the hand-off. use std::any::Any; use std::cell::RefCell; use std::rc::Rc; use pokey::POKEY; pub trait Cartridge: Any { fn name(&self) -> &str { "empty slot" } fn irq(&self) -> bool { false } fn access(&mut self, _address: u16, _data: u8, _read: bool, _halt: bool, _t: u64) -> Option { None } /// One Φ2 period has passed (after its access). fn tick(&mut self, _data_bus: u8) {} /// The cartridge's audio pin, 0.0-1.0, or None if nothing drives it. fn audio_level(&self) -> Option { None } /// The console's reset button (RESET reaches the cartridge port). fn reset(&mut self) {} fn snapshot(&self) -> Vec { Vec::new() } fn restore(&mut self, _state: &[u8]) -> Result<(), String> { Ok(()) } /// The cartridge's POKEY chips, in base-address order of its description. fn pokeys(&self) -> Vec>> { Vec::new() } fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; fn into_any(self: Box) -> Box; } /// `as_any`, `as_any_mut` and `into_any` for a cartridge type. #[macro_export] macro_rules! cartridge_any { () => { fn as_any(&self) -> &dyn std::any::Any { self } fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self } fn into_any(self: Box) -> Box { self } }; } /// The empty slot. pub struct EmptySlot; impl Cartridge for EmptySlot { cartridge_any!(); } /// No switching: the ROM ends at $FFFF (up to 48 KiB from $4000; 52 KiB from /// $3000 exists in homebrew). Nothing below the ROM is driven (DOC: Stolberg's /// guide, the wiki). pub struct Linear { pub rom: Vec, pub base: u32, } impl Linear { pub fn new(rom: &[u8]) -> Result { if rom.is_empty() || rom.len() > 0xD000 { return Err(format!("a linear cartridge is 1 to 52 KiB, not {} bytes", rom.len())); } Ok(Linear { rom: rom.to_vec(), base: 0x10000 - rom.len() as u32, }) } } impl Cartridge for Linear { fn name(&self) -> &str { "linear" } #[inline] fn access(&mut self, address: u16, _data: u8, read: bool, _halt: bool, _t: u64) -> Option { if read && address as u32 >= self.base { return Some(self.rom[(address as u32 - self.base) as usize]); } None } cartridge_any!(); }