// SPDX-License-Identifier: MIT //! The other schemes licensed cartridges used: E0, 3F, FE, E7, CV, UA, F0 and 32IN1. //! //! Main source: Kevin Horton, *Mostly Inclusive Atari 2600 Mapper / Selected //! Hardware Document* v1.00 (2012; `reference/docs/kevtris-atari-2600-mappers.txt`, //! cited "Horton"), whose author built most of these schemes in his own FPGA //! console. Where Horton leaves a detail open, MAME 0.285's BSD-3-Clause cartridge //! devices (`src/devices/bus/vcs/rom.cpp`, copyright Fabio Priuli; notice in //! NOTICE) are cited as "MAME rom.cpp": an emulator's source, a lower tier than a //! document. Details neither settles are HYPOTHESIS, with the experiment that //! would settle them. Power-on banks are arbitrary on hardware (HYPOTHESIS: no //! document states them); the defaults are MAME's, for the lockstep. //! //! Rust-forced differences (a2600-python `cartridge/licensed.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; /// Parker Brothers 8K: four 1K slices (DOC Horton). /// /// $1000/$1400/$1800 each show any 1K slice of the 8K, chosen by an access to /// $1FE0-$1FE7, $1FE8-$1FEF and $1FF0-$1FF7 respectively; $1C00-$1FFF is fixed /// to the last slice. Power-on slices 4, 5, 6 (MAME rom.cpp:424-429). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct E0 { pub rom: Vec, pub slices: [usize; 4], } impl E0 { pub fn new(image: &[u8], start_slices: (usize, usize, usize)) -> Result { if image.len() != 0x2000 { return Err(format!("E0 needs 8192 bytes, not {}", image.len())); } let (a, b, c) = start_slices; Ok(E0 { rom: image.to_vec(), slices: [a, b, c, 7], }) } } impl Restore for E0 {} impl Cartridge for E0 { fn name(&self) -> &str { "E0" } fn access(&mut self, address: u16, _value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 == 0 { return None; } if (0x1FE0..=0x1FF7).contains(&address) { self.slices[((address >> 3) & 3) as usize] = (address & 7) as usize; } Some(self.rom[(self.slices[((address >> 10) & 3) as usize] << 10) | (address & 0x3FF) as usize]) } cartridge_state!(); } /// Tigervision 3F: 2K pages, the last fixed at $1800 (DOC Horton). /// /// A write to $0000-$003F latches the page for $1000-$17FF from the data bus /// (Horton: "selectable by writing to any location between 0000 and 003F"; /// the TIA sees the same write, which is why games use $3F, a register the /// TIA lacks). Only writes count: HYPOTHESIS for the mechanism, since the slot /// has no R/W line (Horton names a 74LS173 latch without its wiring); a game /// reading a TIA register at $00-$3F would show whether reads latch too. /// The latch holds as many bits as the image has pages (MAME rom.cpp:401-405 /// masks the same way). Power-on: the last page (MAME rom.cpp:384-387). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Tigervision3F { pub rom: Vec, pub mask: usize, pub last: usize, pub page: usize, } impl Tigervision3F { pub fn new(image: &[u8], start_page: Option) -> Result { if image.len() % 0x800 != 0 || image.len() < 0x1000 { return Err(format!( "3F needs a whole number of 2K pages, not {} bytes", image.len() )); } let mask = image.len() / 0x800 - 1; Ok(Tigervision3F { rom: image.to_vec(), mask, last: mask << 11, page: start_page.unwrap_or(mask) << 11, }) } } impl Restore for Tigervision3F {} impl Cartridge for Tigervision3F { fn name(&self) -> &str { "3F" } fn access(&mut self, address: u16, value: u8, _t: u64, write: bool) -> Option { if address & 0x1000 != 0 { if address & 0x800 != 0 { return Some(self.rom[self.last | (address & 0x7FF) as usize]); } return Some(self.rom[self.page | (address & 0x7FF) as usize]); } if write && address < 0x40 { self.page = (value as usize & self.mask) << 11; } None } cartridge_state!(); } /// Activision FE: two 4K banks chosen through the stack (DOC Horton). /// /// Horton: JSR and RTS both end with the byte that holds the new PC's high /// byte (the JSR's high address fetch; the RTS's PCH pull from $01FF), right /// after a stack access at $01FE (the JSR's PCL push, the RTS's PCL pull), and /// the chip "latch[es] D5. This is the NEW bank". The programs are assembled /// at $Fxxx in one bank and $Dxxx in the other, so D5 = 1 ($Fxxx) selects /// bank 0, the image's first 4K, and D5 = 0 ($Dxxx) bank 1 (DOC: the dumps' /// own reset vectors and JSR targets; MAME rom.cpp:284-290 has the same). /// /// The trigger is modelled as an access to $01FE, read or write, arming a /// latch of the next cycle's data bus (MAME rom.cpp:258-302 likewise). /// HYPOTHESIS: Horton describes the chip as also watching for the $20/$60 /// opcodes; whether a bare $01FE access arms it (as the reset sequence's /// stack reads do when S powers up at 0, and MAME's model skips that first /// one) is untested. A probe cartridge that touches $01FE with no JSR/RTS /// would settle it on a console. Power-on: bank 0 (MAME rom.cpp:250-256). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ActivisionFE { pub rom: Vec, pub bank: usize, pub armed: bool, // an access to $01FE was the previous cycle pub latch: bool, // this cycle's byte is the new bank } impl ActivisionFE { pub fn new(image: &[u8], start_bank: usize) -> Result { if image.len() != 0x2000 { return Err(format!("FE needs 8192 bytes, not {}", image.len())); } Ok(ActivisionFE { rom: image.to_vec(), bank: start_bank << 12, armed: false, latch: false, }) } } impl Restore for ActivisionFE {} impl Cartridge for ActivisionFE { fn name(&self) -> &str { "FE" } fn snoops_reads(&self) -> bool { true } fn access(&mut self, address: u16, value: u8, _t: u64, write: bool) -> Option { self.latch = self.armed; self.armed = address == 0x01FE; if write && self.latch { self.snoop(value); } if address & 0x1000 != 0 { return Some(self.rom[self.bank | (address & 0xFFF) as usize]); } None } fn snoop(&mut self, value: u8) { if self.latch { self.bank = if value & 0x20 != 0 { 0 } else { 0x1000 }; self.latch = false; } } cartridge_state!(); } /// M-Network E7 ("Big Game"): 2K ROM pages, 2K of RAM (DOC Horton). /// /// - $1000-$17FF: ROM page 0-6 chosen by an access to $1FE0-$1FE6; an access /// to $1FE7 puts 1K of RAM there instead, written at $1000-$13FF, read at /// $1400-$17FF. /// - $1800-$19FF: the other 1K of RAM, 256 bytes at a time, written at /// $1800-$18FF and read at $1900-$19FF, the block chosen by $1FE8-$1FEB. /// - $1A00-$1FFF: the last 1.5K of the ROM. /// /// (Horton's 1997 `sizes.txt` put the block select at $1FF8-$1FFB; his 2012 /// document and MAME rom.cpp:513 put it at $1FE8-$1FEB, and the dumps' own /// code touches $1FE8-$1FEB: the 2012 text is used.) /// /// A 12K image (BurgerTime's proper dump) holds ROM pages 0-1 and 4-7 of the /// 16K map, with pages 2-3 mirroring 0-1: DOC, from comparing the 12K and 16K /// dumps of BurgerTime page by page (the 16K dump's pages 2-3 equal its 0-1). /// An 8K image (Bump 'n' Jump's USA dump, which Stella's `-rominfo` calls /// E7) has four pages; HYPOTHESIS: the ROM's address lines repeat it, so /// page n is page n mod 4 and the fixed area is the image's last 1.5K. A /// 16K dump of the same cartridge, or the board, would settle it. /// Power-on: ROM page 0, RAM block 0 (MAME rom.cpp:489-493). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct MNetworkE7 { pub rom: Vec, pub ram: Vec, // $000-$3FF low 1K, $400-$7FF the blocks pub page: usize, // 7 = RAM pub block: usize, } impl MNetworkE7 { pub fn new(image: &[u8], start_page: usize, ram_fill: u8) -> Result { let pages: Vec<&[u8]> = image.chunks(0x800).collect(); let image: Vec = if image.len() == 0x3000 { [&pages[..2], &pages[..2], &pages[2..]].concat().concat() } else if image.len() == 0x2000 { [&pages[..], &pages[..]].concat().concat() } else { image.to_vec() }; if image.len() != 0x4000 { return Err(format!("E7 needs 16384, 12288 or 8192 bytes, not {}", image.len())); } Ok(MNetworkE7 { rom: image, ram: vec![ram_fill; 0x800], page: start_page, block: 0, }) } } impl Restore for MNetworkE7 {} impl Cartridge for MNetworkE7 { fn name(&self) -> &str { "E7" } fn access(&mut self, address: u16, value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 == 0 { return None; } if (0x1FE0..=0x1FEB).contains(&address) { if address <= 0x1FE7 { self.page = (address & 7) as usize; } else { self.block = (address & 3) as usize; } } let offset = (address & 0xFFF) as usize; if offset < 0x800 { if self.page == 7 { if offset < 0x400 { self.ram[offset] = value; return None; } return Some(self.ram[offset - 0x400]); } return Some(self.rom[(self.page << 11) | offset]); } if offset < 0xA00 { let index = 0x400 | (self.block << 8) | (offset & 0xFF); if offset < 0x900 { self.ram[index] = value; return None; } return Some(self.ram[index]); } Some(self.rom[0x3800 | (offset & 0x7FF)]) } cartridge_state!(); } /// CommaVid CV: 2K of ROM at $1800-$1FFF, 1K of RAM read at $1000-$13FF /// and written at $1400-$17FF (DOC Horton; MAME rom.cpp:587-593 agrees). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct CommaVid { pub rom: Vec, pub ram: Vec, } impl CommaVid { pub fn new(image: &[u8], ram_fill: u8) -> Result { let image = if image.len() == 0x1000 { // a 4K dump with the ROM in its upper half &image[0x800..] } else { image }; if image.len() != 0x800 { return Err(format!("CV needs 2048 bytes, not {}", image.len())); } Ok(CommaVid { rom: image.to_vec(), ram: vec![ram_fill; 0x400], }) } } impl Restore for CommaVid {} impl Cartridge for CommaVid { fn name(&self) -> &str { "CV" } fn access(&mut self, address: u16, value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 == 0 { return None; } let offset = (address & 0xFFF) as usize; if offset >= 0x800 { return Some(self.rom[offset & 0x7FF]); } if offset < 0x400 { return Some(self.ram[offset]); } self.ram[offset & 0x3FF] = value; None } cartridge_state!(); } /// UA Ltd 8K: two 4K banks, an access to $0220 selecting the first and $0240 /// the second (DOC Horton). /// /// HYPOTHESIS on the decode: Horton names the two addresses only. The model /// decodes A12 = 0, A9 = 1, A7 = 0 (the RIOT's I/O half and the TIA never use /// that combination for these programs) and takes the bank from A6, as MAME /// does over $0200-$027F (rom.cpp:555-568); a game touching another mirror /// would tell the two apart. Power-on: bank 0 (MAME rom.cpp:550-553). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct UA { pub rom: Vec, pub bank: usize, } impl UA { pub fn new(image: &[u8], start_bank: usize) -> Result { if image.len() != 0x2000 { return Err(format!("UA needs 8192 bytes, not {}", image.len())); } Ok(UA { rom: image.to_vec(), bank: start_bank << 12, }) } } impl Restore for UA {} impl Cartridge for UA { fn name(&self) -> &str { "UA" } fn access(&mut self, address: u16, _value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 != 0 { return Some(self.rom[self.bank | (address & 0xFFF) as usize]); } if address & 0x1380 == 0x0200 && address & 0x60 != 0 { self.bank = ((address & 0x40) as usize) << 6; } None } cartridge_state!(); } /// Dynacom Megaboy F0: sixteen 4K banks; each access to $1FF0 steps to the /// next (DOC Horton: "accessing 1FF0 causes the bank number to increment"). /// /// MAME (rom.cpp:617-637) also returns the bank number at $1FEC; Horton says /// the program finds its bank from a ROM byte there ("Each bank is numbered by /// means of one of the ROM locations"), so the ROM is returned (DOC Horton /// over MAME). Power-on: bank 0 (MAME rom.cpp:612-615). #[derive(Clone, Debug, Serialize, Deserialize)] pub struct MegaboyF0 { pub rom: Vec, pub bank: usize, } impl MegaboyF0 { pub fn new(image: &[u8], start_bank: usize) -> Result { if image.len() != 0x10000 { return Err(format!("F0 needs 65536 bytes, not {}", image.len())); } Ok(MegaboyF0 { rom: image.to_vec(), bank: start_bank, }) } } impl Restore for MegaboyF0 {} impl Cartridge for MegaboyF0 { fn name(&self) -> &str { "F0" } fn access(&mut self, address: u16, _value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 == 0 { return None; } if address == 0x1FF0 { self.bank = (self.bank + 1) & 15; } Some(self.rom[(self.bank << 12) | (address & 0xFFF) as usize]) } cartridge_state!(); } /// 32 in 1: thirty-two 2K games, one per power cycle (DOC MAME rom.cpp:798-832: /// "the 2K banks change at each reset", the next game on every reset of the /// console; Horton does not cover it). HYPOTHESIS: the cartridge counts the /// console's power-ups; the first game at the first. Each 2K game appears /// twice in the 4K window, like a plain 2K cartridge. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThirtyTwoInOne { pub rom: Vec, pub game: usize, } impl ThirtyTwoInOne { pub fn new(image: &[u8], game: usize) -> Result { if image.len() != 0x10000 { return Err(format!("32IN1 needs 65536 bytes, not {}", image.len())); } Ok(ThirtyTwoInOne { rom: image.to_vec(), game, }) } } impl Restore for ThirtyTwoInOne {} impl Cartridge for ThirtyTwoInOne { fn name(&self) -> &str { "32IN1" } fn reset(&mut self) { self.game = (self.game + 1) & 31; } fn access(&mut self, address: u16, _value: u8, _t: u64, _write: bool) -> Option { if address & 0x1000 != 0 { return Some(self.rom[(self.game << 11) | (address & 0x7FF) as usize]); } None } cartridge_state!(); }