// SPDX-License-Identifier: MIT // Bounty Bob and Super Cart bank rules: facts restated (no code) from MAME's // bus/a800/bbsb.cpp and bus/a800/a5200_supercart.cpp, BSD-3-Clause, copyright // Fabio Priuli and Angelo Salese; their notice is in NOTICE. //! The cartridge port: what each cartridge class puts on the data bus. //! //! The 5200's cartridge connector carries D0-D7, A0-A13 and two decoded //! enables, "Enable 40-7F" and "Enable 80-BF"; there is no A14, A15 or R/W //! (docs/cartridge.md "The connector"). Every class below is which ROM chips //! hang on which enable. The protocol (docs/cartridge.md "The cartridge port //! protocol"): //! //! - `read(address) -> int | None` for a cycle with the address in //! $4000-$BFFF; None means nothing drives the bus (the board keeps its //! floating-bus latch). A bank-switching class switches on this access too. //! - `access(address)` for a write cycle in $4000-$BFFF: the connector has no //! R/W, so a hotspot switches on a write as well; ROM ignores the data. //! //! ANTIC's DMA reads come through `read` as well: the cartridge cannot tell //! who drives the address. This module imports nothing from the board //! (tests/test_boundaries.py). //! //! Evidence tags (methodology): the connector's lines are DOC ([FAQ] 4.6 //! pinout, [AHRM] 13.2); the classes' chip arrangements are DOC (docs/cartridge.md //! "Classes"; MAME's CC0 software list for which titles use which) and every //! class boots the commercial dumps that use it (milestone 5, MEASURED on dumps //! read here, judged by the board's own run). Unknowns are tagged where they are. //! //! A transcription of a5200-python's `cartridge/mappings.py` (at cf82d44). Rust-forced //! differences: Python's class hierarchy (`Cartridge` and its subclasses overriding //! `_build_window`, `read`, `access`, `snapshot`, `restore`) is one struct `Cartridge` whose //! `kind` names the class, with each method matching on it in the order the classes are //! defined; `window` entries are `i16` (-1 where nothing drives the bus); `read` returns //! `Option` (Python's `int | None`); `snapshot()` is the bank state as a list of numbers //! (Python's dict: `{}`, `{"bank": [a, b]}`, `{"bank": n}`). use serde::{Deserialize, Serialize}; /// Which class a [`Cartridge`] is (Python's subclass). #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Kind { /// The base class: a fixed 32K window, None where no chip is enabled. Plain, Cart32K, Cart16KTwoChip, Cart16KOneChip, Cart8K, Cart4K, BountyBob, SuperCart, } /// A cartridge on the port. /// /// Classes (Python's docstrings): /// /// - `Cart32K`: Two 16K ROMs: the first on Enable 40-7F, the second on Enable 80-BF. /// - `Cart16KTwoChip`: Two 8K ROMs with A13 unconnected: each is seen twice in its 16K half. /// - `Cart16KOneChip`: One 16K ROM on Enable 80-BF, and on Enable 40-7F as well. /// /// C4: Atari800 leaves $4000-$7FFF empty, MAME's hash file says the 16K is /// seen at $4000 too. The board follows MAME (mirrored) unless a cartridge /// is described with `low_mirror=False`. HYPOTHESIS (C4, undecidable here): /// a one-chip cartridge's board traces, or a probe reading $4000 with a /// one-chip cartridge on a console, would settle it. /// - `Cart8K`: One 8K ROM on Enable 80-BF: $8000-$BFFF shows it twice; $4000-$7FFF is undriven. /// - `Cart4K`: One 4K ROM on Enable 80-BF: seen four times at $8000-$BFFF. /// - `BountyBob`: Bounty Bob Strikes Back! (40K): two switched 4K windows and a fixed 8K. /// /// Payload in canonical (Atari800 type 7) order: four 4K banks for /// $4000-$4FFF, four for $5000-$5FFF, then the fixed 8K at $8000-$9FFF and /// again at $A000-$BFFF (DOC: [A800] DOC/cart.txt type 7; [MAME] /// bus/a800/bbsb.cpp, BSD-3-Clause, Fabio Priuli and Angelo Salese, notice in /// NOTICE: facts restated, no code). An access (read or write) to $4FF6-$4FF9 selects the /// first window's bank 0-3, to $5FF6-$5FF9 the second's. What $6000-$7FFF /// shows is not stated in the sources (U9b): undriven here. Power-on banks /// are not stated either (U9b): bank 0 in both windows, a labelled default. /// Both HYPOTHESIS: the cartridge's PCB (a 74-series latch pair) would /// settle them. /// - `SuperCart`: The homebrew "Super Cart" (64K-512K): 32K banks over the whole window. /// /// DOC: [A800] DOC/cart.txt types 71-74 (citing the designer's post); [MAME] /// bus/a800/a5200_supercart.cpp, BSD-3-Clause, Angelo Salese, notice in /// NOTICE: facts restated, no code. A read of $BFD0-$BFDF sets bank bits /// 0-1 from address bits 2-3; a read of $BFC0-$BFCF sets bits 2-3 from /// address bits 2-3; a read of $BFE0-$BFFF selects the last bank, which the /// BIOS's own trailer reads do at power-on. The board applies the same rule /// to write cycles (the connector has no R/W: DOC [FAQ] 4.6). Power-on: the /// last bank (DOC, as the bank rule implies through the BIOS's trailer reads). #[derive(Clone, Debug, PartialEq)] pub struct Cartridge { pub kind: Kind, pub mapping: &'static str, pub payload: Vec, /// $4000-$BFFF as 32 768 entries, -1 where nothing drives the bus. pub window: Vec, // Cart16KOneChip pub low_mirror: bool, // BountyBob: two banks; SuperCart: bank[0] is the bank pub bank: [usize; 2], // SuperCart pub banks: usize, } fn bytes(data: &[u8]) -> Vec { data.iter().map(|&b| b as i16).collect() } impl Cartridge { pub fn new(kind: Kind, payload: &[u8]) -> Self { Self::with_low_mirror(kind, payload, true) } pub fn with_low_mirror(kind: Kind, payload: &[u8], low_mirror: bool) -> Self { let mapping = match kind { Kind::Plain => "none", Kind::Cart32K => "32k", Kind::Cart16KTwoChip => "16k-two", Kind::Cart16KOneChip => "16k-one", Kind::Cart8K => "8k", Kind::Cart4K => "4k", Kind::BountyBob => "bbsb", Kind::SuperCart => "supercart", }; let mut cart = Cartridge { kind, mapping, payload: payload.to_vec(), window: Vec::new(), low_mirror, bank: [0, 0], banks: 0, }; if kind == Kind::SuperCart { cart.banks = payload.len() / 0x8000; cart.bank[0] = cart.banks - 1; } cart.window = cart._build_window(); cart } fn _build_window(&self) -> Vec { let payload = &self.payload; match self.kind { Kind::Plain | Kind::SuperCart => vec![-1; 0x8000], Kind::Cart32K => bytes(payload), Kind::Cart16KTwoChip => { let (low, high) = (&payload[..0x2000], &payload[0x2000..]); [low, low, high, high] .concat() .iter() .map(|&b| b as i16) .collect() } Kind::Cart16KOneChip => { let mut low = if self.low_mirror { bytes(payload) } else { vec![-1; 0x4000] }; low.extend(bytes(payload)); low } Kind::Cart8K => { let mut window = vec![-1; 0x4000]; window.extend(bytes(payload)); window.extend(bytes(payload)); window } Kind::Cart4K => { let mut window = vec![-1; 0x4000]; for _ in 0..4 { window.extend(bytes(payload)); } window } Kind::BountyBob => { let mut window = vec![-1; 0x8000]; let fixed = bytes(&payload[0x8000..0xA000]); window[0x4000..0x6000].copy_from_slice(&fixed); window[0x6000..0x8000].copy_from_slice(&fixed); window } } } pub fn read(&mut self, address: u16) -> Option { match self.kind { Kind::BountyBob => { if address < 0x6000 { self.access(address); if address < 0x5000 { return Some( self.payload[self.bank[0] * 0x1000 + (address as usize & 0xFFF)], ); } return Some( self.payload[0x4000 + self.bank[1] * 0x1000 + (address as usize & 0xFFF)], ); } let value = self.window[address as usize - 0x4000]; if value < 0 { None } else { Some(value as u8) } } Kind::SuperCart => { self.access(address); Some(self.payload[self.bank[0] * 0x8000 + address as usize - 0x4000]) } _ => { let value = self.window[address as usize - 0x4000]; if value < 0 { None } else { Some(value as u8) } } } } pub fn access(&mut self, address: u16) { match self.kind { Kind::BountyBob => { let low = address & 0xFFF; if (0xFF6..=0xFF9).contains(&low) { if (0x4000..0x5000).contains(&address) { self.bank[0] = (low - 0xFF6) as usize; } else if (0x5000..0x6000).contains(&address) { self.bank[1] = (low - 0xFF6) as usize; } } } Kind::SuperCart => { if address >= 0xBFC0 { let mask = self.banks - 1; let address = address as usize; if address >= 0xBFE0 { self.bank[0] = mask; } else if address >= 0xBFD0 { self.bank[0] = ((self.bank[0] & 0x0C) | ((address >> 2) & 3)) & mask; } else { self.bank[0] = ((self.bank[0] & 0x03) | (address & 0x0C)) & mask; } } } _ => {} } } // -- state ------------------------------------------------------------ pub fn snapshot(&self) -> Vec { match self.kind { Kind::BountyBob => self.bank.to_vec(), Kind::SuperCart => vec![self.bank[0]], _ => Vec::new(), } } pub fn restore(&mut self, state: &[usize]) { match self.kind { Kind::BountyBob => self.bank = [state[0], state[1]], Kind::SuperCart => self.bank[0] = state[0], _ => {} } } } /// `CLASSES`: the class for a mapping name. pub fn class(mapping: &str) -> Option { Some(match mapping { "4k" => Kind::Cart4K, "8k" => Kind::Cart8K, "16k-one" => Kind::Cart16KOneChip, "16k-two" => Kind::Cart16KTwoChip, "32k" => Kind::Cart32K, "bbsb" => Kind::BountyBob, "supercart" => Kind::SuperCart, _ => return None, }) } /// The mapping names of `CLASSES`, in its order. pub const CLASS_NAMES: [&str; 7] = ["4k", "8k", "16k-one", "16k-two", "32k", "bbsb", "supercart"]; /// The port object for `mapping` (one of `CLASSES`) over `payload`. pub fn make_port(mapping: &str, payload: &[u8]) -> Cartridge { Cartridge::new(class(mapping).expect("a mapping of CLASSES"), payload) }