// SPDX-License-Identifier: MIT //! Plain 2K/4K, and the 4K-bank schemes: Atari's F8/F6/F4 (optional Super Chip) and CBS's FA. //! //! Sources: Kevin Horton's mapper document (`reference/docs/kevtris-atari-2600-mappers.txt`, //! 2012, DOC) and his older `qotile-sizes.txt` (1997, DOC); docs/cartridge.md. //! //! - **F8, F6, F4** (DOC Horton): every access with A12 high to a hotspot selects //! a 4K bank, read or write, dummy or not ("ANY kind of access will trigger the //! switching"): F8 $1FF8-$1FF9 (2 banks), F6 $1FF6-$1FF9 (4), F4 $1FF4-$1FFB //! (8), the lowest hotspot selecting bank 0. //! - **FA** (CBS RAM Plus, DOC Horton): three 4K banks at $1FF8-$1FFA, and 256 //! bytes of RAM, written at $1000-$10FF and read at $1100-$11FF. //! - **The Super Chip** (SC, DOC Horton): 128 bytes of RAM written at //! $1000-$107F, read at $1080-$10FF, the ROM disabled for those 256 bytes. //! Without an R/W line any access to a write window writes the byte on the bus //! (connector.rs). //! //! **The byte a hotspot read returns** is the new bank's. HYPOTHESIS: the bank //! latch changes during the access and the ROM's output settles from the new bank //! before the data is sampled at the end of the cycle; a logic-analyser capture //! of a hotspot read would settle it. MAME switches in a read tap after the read //! and returns the old bank's byte (BSD-3 rom.cpp:125-126): //! docs/undocumented-behavior.md C20. Programs normally read hotspots only as //! dummy accesses or as the target of a bank-switch instruction, where the value //! is not used. //! //! **The power-on bank** is arbitrary on a console (HYPOTHESIS: no document //! states it; the programs switch banks from code present in every bank). The //! default is MAME 0.285's (BSD-3 rom.h:53, 70, 97: F6 and FA bank 0, F4 bank 7, //! F8 bank 1), which milestone 1's lockstep needs. //! //! Rust-forced differences (a2600-python `cartridge/atari.py` at 28fb095): the //! constructors return `Result` where Python raises `ValueError`; keyword //! options are explicit arguments. use serde::{Deserialize, Serialize}; use super::connector::{Cartridge, Restore}; use crate::cartridge_state; pub const BANK: usize = 0x1000; // DOC Horton: 4K banks, the whole A12-high window /// scheme: (first hotspot, banks, MAME's power-on bank, RAM bytes). Hotspots, /// banks and RAM: DOC Horton; the power-on bank: OBSERVED/DOC MAME 0.285 BSD-3 /// rom.cpp, a HYPOTHESIS for consoles (see the module notes). pub const SCHEMES: [(&str, (u16, usize, usize, usize)); 4] = [ ("F8", (0x1FF8, 2, 1, 0)), ("F6", (0x1FF6, 4, 0, 0)), ("F4", (0x1FF4, 8, 7, 0)), ("FA", (0x1FF8, 3, 0, 256)), ]; pub const SUPER_CHIP: usize = 128; // DOC Horton: 128 bytes, written at $1000-$107F, read at $1080-$10FF /// 2K or 4K with no banking; a 2K image appears twice in the 4K window (DOC Horton). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Plain { pub name: String, pub rom: Vec, } impl Plain { pub fn new(image: &[u8]) -> Result { if image.len() != 0x800 && image.len() != 0x1000 { return Err(format!( "2K/4K cartridge must be 2048 or 4096 bytes, not {}", image.len() )); } let name = if image.len() == 0x800 { "2K" } else { "4K" }; Ok(Plain { name: name.to_string(), rom: image.repeat(0x1000 / image.len()), }) } } impl Restore for Plain {} impl Cartridge for Plain { fn name(&self) -> &str { &self.name } fn access(&mut self, address: u16, _value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 != 0 { return Some(self.rom[(address & 0xFFF) as usize]); } None } cartridge_state!(); } /// F8, F6, F4 (with `super_chip` RAM if asked) or FA (always with its 256 bytes). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Banked { pub name: String, pub rom: Vec, pub first_hotspot: u16, pub last_hotspot: u16, pub bank: usize, pub ram: Option>, pub ram_size: usize, // the write window, then the read window, from $1000 } impl Banked { pub fn new( image: &[u8], scheme: &str, super_chip: bool, start_bank: Option, ram_fill: u8, ) -> Result { let (_, (first, banks, mame_start, mut ram_size)) = *SCHEMES .iter() .find(|(name, _)| *name == scheme) .ok_or_else(|| format!("no 4K-bank scheme {scheme}"))?; if image.len() != banks * BANK { return Err(format!("{scheme} needs {} bytes, not {}", banks * BANK, image.len())); } let name; if super_chip && ram_size == 0 { ram_size = SUPER_CHIP; name = format!("{scheme}SC"); } else { name = scheme.to_string(); } Ok(Banked { name, rom: image.to_vec(), first_hotspot: first, last_hotspot: first + banks as u16 - 1, bank: start_bank.unwrap_or(mame_start), ram: if ram_size != 0 { Some(vec![ram_fill; ram_size]) } else { None }, ram_size, }) } } impl Restore for Banked {} impl Cartridge for Banked { fn name(&self) -> &str { &self.name } fn access(&mut self, address: u16, value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 == 0 { return None; } if self.first_hotspot <= address && address <= self.last_hotspot { self.bank = (address - self.first_hotspot) as usize; } if let Some(ram) = self.ram.as_mut() { let offset = (address & 0xFFF) as usize; let size = self.ram_size; if offset < size { ram[offset] = value; return None; // the ROM is disabled and the RAM is being written } if offset < 2 * size { return Some(ram[offset - size]); } } Some(self.rom[(self.bank << 12) | (address & 0xFFF) as usize]) } cartridge_state!(); }