// SPDX-License-Identifier: MIT //! A Mega Drive with a 32X in its slot, built the one way every caller should build it. //! //! `build(machine, cartridge, boot_roms)` makes the slot device, plugs it in at power-on //! (`System::with_sockets(..., slot)`, socket 1) and gives it the VDP's line records //! (`System::attach_video_timing`, socket 6), which its VDP status bits and the SH-2s' //! VINT/HINT need, makes it the picture's last stage (`System::attach_picture`, //! socket 7: the composite, `composite.rs`) and adds its PWM to the sound //! (`System::audio_sources`, socket 8: `pwm.rs`). The user's boot ROMs are required //! (Aubrey's decision 3) and checked by SHA-1 (`BootROMs`). //! //! The reference's `console.py`. What Rust forced: the reference hands the one device //! object to all four sockets; here it is shared ([`Shared`]) by four small socket //! objects, each locking it for its call. `AcclaimEEPROMCartridge` is megadrive-rust's //! `Cartridge` with its `sda_line` flag set (a subclass in the reference). use std::sync::{Arc, Mutex}; use megadrive_rust::board::Devices; use megadrive_rust::cartridge::Cartridge; use megadrive_rust::io::Device; use megadrive_rust::sockets::{ AudioSource, BusDevice, Clocked, Picture, PictureLine, PictureRow, Reset, SlotDevice, VideoLine, VideoTiming, }; use megadrive_rust::System; use crate::bootrom::BootROMs; use crate::machine::machine as machine_32x; use crate::slot::{Sega32X, Stop}; /// The device, as its four socket objects share it. pub type Shared = Arc>; fn stopped(board: &mut Devices, out: Result) -> T { match out { Ok(value) => value, Err(Stop::Lockup(message)) => { board.lock_up(message); T::default() } Err(Stop::BootROM(message)) => panic!("{message}"), } } /// Socket 1 (and 4, 5, 9): the slot device. pub struct Slot { pub device: Shared, pub cartridge: Option, } impl Slot { fn lock(&self) -> std::sync::MutexGuard<'_, Sega32X> { self.device.lock().expect("the 32X") } /// A call into the device, then the board's claims read again if the device changed /// them (outside the device's lock: the board asks the slot object for them). One lock /// for the call and the question (sega32x-rust-fast: upstream locks twice; nothing /// runs between the two). fn call(&self, board: &mut Devices, f: impl FnOnce(&mut Sega32X, &mut Devices) -> T) -> T { let (out, changed) = { let mut device = self.lock(); let out = f(&mut device, board); (out, device.take_claims_changed()) }; if changed { board.update_claims(); } out } } impl Clocked for Slot { fn catch_up(&mut self, board: &mut Devices, mclk: i64) { self.call(board, |device, board| device.catch_up(board, mclk)); } fn video_first(&mut self, _board: &mut Devices, mclk: i64, reset: Option) -> bool { self.lock().video_first(mclk, reset) } } impl BusDevice for Slot { fn attach(&mut self, board: &mut Devices) { self.lock().attach(board); } fn claims(&self) -> Vec<(u32, u32)> { self.lock().claims() } fn read_word(&mut self, board: &mut Devices, address: u32, mclk: i64) -> u16 { let out = self.call(board, |device, board| device.read_word(board, address, mclk)); stopped(board, out) } fn write_word(&mut self, board: &mut Devices, address: u32, value: u16, mclk: i64) { let out = self.call(board, |device, board| device.write_word(board, address, value, mclk)); stopped(board, out) } fn read_byte(&mut self, board: &mut Devices, address: u32, mclk: i64, master: u8) -> u8 { let out = self.call(board, |device, board| device.read_byte(board, address, mclk, master)); stopped(board, out) } fn write_byte(&mut self, board: &mut Devices, address: u32, value: u8, mclk: i64, master: u8) { let out = self.call(board, |device, board| device.write_byte(board, address, value, mclk, master)); stopped(board, out) } fn peek_byte(&mut self, board: &mut Devices, address: u32) -> u8 { self.lock().peek_byte(board, address) } /// Nothing latches at a DMA's start on the 32X side (the one-word-late rule is /// the Sega CD's word RAM, not this). fn dma_start(&mut self, _board: &mut Devices, _mclk: i64, _bus: u16) {} fn dma_read_word(&mut self, board: &mut Devices, address: u32, mclk: i64) -> u16 { let out = self.call(board, |device, board| device.dma_read_word(board, address, mclk)); stopped(board, out) } fn reset(&mut self, board: &mut Devices, kind: Reset, mclk: i64) { self.call(board, |device, board| device.reset(board, kind, mclk)); } fn name(&self) -> &'static str { "sega32x_python.slot.Sega32X" } } impl SlotDevice for Slot { fn take_cartridge(&mut self) -> Option { self.cartridge.take() } } /// Socket 6. pub struct Video(pub Shared); impl VideoTiming for Video { fn video_line(&mut self, line: VideoLine) { self.0.lock().expect("the 32X").video_line(&line); } } /// Socket 7. pub struct Screen(pub Shared); impl Picture for Screen { fn picture_line(&mut self, line: PictureLine) -> Option { self.0.lock().expect("the 32X").picture_line(line) } } /// Socket 8: the PWM. pub struct Sound(pub Shared); impl AudioSource for Sound { fn rate(&self) -> f64 { self.0.lock().expect("the 32X").pwm.rate() } fn gain(&self) -> f64 { self.0.lock().expect("the 32X").pwm.gain } fn take_samples(&mut self, mclk: i64) -> Vec<(f64, f64)> { let mut device = self.0.lock().expect("the 32X"); let mclk_hz = device.mclk_hz; let (pwm, mut links) = device.pwm_links(); pwm.take_samples(mclk, mclk_hz, &mut links) } } /// The Cartridge of a 32X image: megadrive-rust's own choice of board, unless its /// serial is one of `Quirks.cartridge_boards` (`AcclaimEEPROMCartridge`: an Acclaim /// cartridge with a serial EEPROM, as a 32X title has it. The board's wiring (/SDA at /// $200001 bit 0, /SCL at $200000 bit 0, a 24C02) is megadrive-rust's `rom_nbajamte` /// board at the pin; this adds one rule: /SDA reads the line, not only what the chip /// drives. DOC: the I2C bus's SDA is open-drain, a wired AND of every device driving it /// (NXP UM10204, the I2C-bus specification, section 3.1.1; Microchip's 24LC02B data /// sheet, "SDA ... open-drain terminal"). So a line the 68000 holds low reads low whatever /// the EEPROM drives. HYPOTHESIS: that the cartridge feeds the 68000's /SDA latch onto /// the line through an open-drain stage (no schematic of the board is to hand).) pub fn cartridge_for(image: Vec, model: &str) -> Cartridge { let serial: String = String::from_utf8_lossy(&image[0x183..0x18B]).trim().to_string(); let boards = machine_32x(model).expect("a 32X model").quirks.cartridge_boards.value; match boards.iter().find(|(s, _)| *s == serial) { None => Cartridge::from_image(image), Some((_, slot)) => { let mut cartridge = Cartridge::new(image, slot, 0, ""); cartridge.sda_line = true; cartridge } } } /// A System of megadrive-rust's `machine` with a 32X (`model`) holding `cartridge`. pub fn build( machine: &str, cartridge: Option, boot_roms: &BootROMs, model: &str, ) -> Result<(System, Shared), String> { let has_cartridge = cartridge.is_some(); let device = Sega32X::new(has_cartridge, machine_32x(model), None, Some(boot_roms))?; let shared: Shared = Arc::new(Mutex::new(device)); let md = megadrive_rust::machine(machine).ok_or(format!("unknown machine {machine}"))?; let pads: Option<[Option; 2]> = None; let slot = Slot { device: shared.clone(), cartridge }; let mut system = System::with_sockets(md, None, pads, None, Some(Box::new(slot)), None); system.attach_video_timing(Box::new(Video(shared.clone()))); system.attach_picture(Box::new(Screen(shared.clone()))); system.audio_sources.push(Box::new(Sound(shared.clone()))); Ok((system, shared)) } /// The harness's passive 32X: `Sega32X(cartridge)`, no boot ROMs, in the slot, with /// sockets 7 and 8 attached as the reference's `harness.insert_32x` does (not 6). pub fn build_passive( md: megadrive_rust::Machine, cartridge: Option, pads: Option<[Option; 2]>, tmss_rom: Option>, ) -> (System, Shared) { let device = Sega32X::new(cartridge.is_some(), None, None, None).expect("a passive 32X"); let shared: Shared = Arc::new(Mutex::new(device)); let slot = Slot { device: shared.clone(), cartridge }; let mut system = System::with_sockets(md, None, pads, tmss_rom, Some(Box::new(slot)), None); system.attach_picture(Box::new(Screen(shared.clone()))); system.audio_sources.push(Box::new(Sound(shared.clone()))); (system, shared) }