// SPDX-License-Identifier: MIT //! The cartridge port: an 8-bit address shift register, an 11-bit counter, two strobes //! (`cart/port.py`). //! //! Every rule here is DOC: Spec §9 ("ROM Cart") and Appx 2 (`$FD87` SYSCTL1, //! `$FD8B` IODAT, `$FCB2/3` RCART0/1). //! //! - The shift register takes CartAddressData (IODAT bit 1, as Mikey drives it) on a //! rising edge of CartAddressStrobe (SYSCTL1 bit 0), most significant bit first. //! - The counter is held at 0 while the strobe is high. //! - A read of RCART0 or RCART1 enables CART0/ or CART1/; its trailing edge advances //! the one shared counter. Address = page * block + (counter mod block). //! //! The port is the channel between Mikey (who drives the address lines) and Suzy //! (who strobes the chip enables and reads the data on her switch lines): both //! chips touch it, neither touches the other. //! //! Differences forced by Rust: Mikey and Suzy share the one port; in Python both hold a //! reference to it, here the board owns it and passes it to each chip's call. //! The cart images (`banks`) are not part of a save state; they are kept from the console //! a state is loaded into. use serde::{Deserialize, Serialize}; use crate::cart::eeprom::Eeprom; #[derive(Clone, Serialize, Deserialize)] pub struct CartPort { #[serde(skip)] pub banks: [Vec; 2], pub blocks: [usize; 2], pub shift: u32, pub counter: u32, pub strobe: u8, pub data: u8, /// AUDIN driven as an output (IODAT bit 4) selects the upper half on AUDIN carts (M7). pub audin: u8, pub writes: u64, /// A serial EEPROM on the cart (crate::cart::eeprom): CS = counter bit 7 (A7), /// CLK = counter bit 1 (A1), DI/DO = AUDIN (DOC: BLL eeprom.inc, cc65 eeprom.s). pub eeprom: Option, } impl CartPort { pub fn new(bank0: &[u8], block0: usize, bank1: &[u8], block1: usize) -> CartPort { CartPort { banks: [bank0.to_vec(), bank1.to_vec()], blocks: [ if block0 != 0 { block0 } else { 1024 }, if block1 != 0 { block1 } else { 1024 }, ], shift: 0, counter: 0, strobe: 0, data: 0, audin: 1, writes: 0, eeprom: None, } } fn eeprom_lines(&mut self) { let counter = self.counter; let audin = self.audin; if let Some(eeprom) = self.eeprom.as_mut() { eeprom.pins( ((counter >> 7) & 1) as u8, ((counter >> 1) & 1) as u8, audin, ); } } /// AUDIN as Mikey drives it (1, the pull-up, when it is an input). pub fn set_audin(&mut self, level: u8) { self.audin = level; if self.eeprom.is_some() { self.eeprom_lines(); } } /// What IODAT bit 4 reads with AUDIN as an input: the EEPROM's DO while it /// drives it, else the pull-up (HYPOTHESIS U17: 1). pub fn audin_pin(&self) -> u8 { if let Some(eeprom) = &self.eeprom { if eeprom.cs != 0 { return eeprom.do_; } } 1 } pub fn set_strobe(&mut self, level: u8) { if level != 0 && self.strobe == 0 { // DOC Spec §9: data accepted on the 0 -> 1 transition, MSB first. self.shift = ((self.shift << 1) | self.data as u32) & 0xFF; } self.strobe = level; if level != 0 { self.counter = 0; // DOC: the counter is reset whenever the strobe is high if self.eeprom.is_some() { self.eeprom_lines(); } } } pub fn set_data(&mut self, level: u8) { self.data = level; } pub fn address(&self, bank: usize) -> usize { let block = self.blocks[bank]; self.shift as usize * block + (self.counter as usize & (block - 1)) } pub fn read(&mut self, bank: usize) -> u8 { let address = self.address(bank); let data = &self.banks[bank]; // HYPOTHESIS: an address past the image reads $FF (no chip answers; the data lines // are Suzy's switch lines behind resistors). Settle with a probe ROM on a console. let value = if address < data.len() { data[address] } else { 0xFF }; if self.strobe == 0 { self.counter = (self.counter + 1) & 0x7FF; // DOC: trailing edge advances it if self.eeprom.is_some() { self.eeprom_lines(); } } value } pub fn write(&mut self, _bank: usize, _value: u8) { // DOC Spec §9: writes use the same addressing; a ROM ignores them. Counted only. self.writes += 1; if self.strobe == 0 { self.counter = (self.counter + 1) & 0x7FF; if self.eeprom.is_some() { self.eeprom_lines(); } } } }