From af2df788097eb7a6480f99c33b9ae69158f9c2f6 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Wed, 6 Aug 2025 23:31:32 -0400 Subject: [PATCH] Move cart to separate file --- src/emulator.rs | 62 +++++-------------------------------------- src/emulator/cart.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 56 deletions(-) create mode 100644 src/emulator/cart.rs diff --git a/src/emulator.rs b/src/emulator.rs index 55019ec..d73b276 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -1,8 +1,6 @@ use std::{ collections::HashMap, fmt::Display, - fs::{self, File}, - io::{Read, Seek, SeekFrom, Write}, path::{Path, PathBuf}, sync::{ Arc, Weak, @@ -16,11 +14,11 @@ use anyhow::Result; use atomic::Atomic; use bytemuck::NoUninit; use egui_notify::Toast; -use rand::Rng; use tracing::{error, warn}; use crate::{ audio::Audio, + emulator::cart::Cart, graphics::TextureSink, memory::{MemoryRange, MemoryRegion}, }; @@ -28,6 +26,7 @@ use shrooms_vb_core::{EXPECTED_FRAME_SIZE, Sim, StopReason}; pub use shrooms_vb_core::{VBKey, VBRegister, VBWatchpointType}; mod address_set; +mod cart; mod shrooms_vb_core; #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] @@ -62,54 +61,6 @@ impl Display for SimId { } } -struct Cart { - rom_path: PathBuf, - rom: Vec, - sram_file: File, - sram: Vec, -} - -impl Cart { - fn load(rom_path: &Path, sim_id: SimId) -> Result { - let rom = fs::read(rom_path)?; - - let mut sram_file = File::options() - .read(true) - .write(true) - .create(true) - .truncate(false) - .open(sram_path(rom_path, sim_id))?; - - let sram = if sram_file.metadata()?.len() == 0 { - // new SRAM file, randomize the contents - let mut sram = vec![0; 8 * 1024]; - let mut rng = rand::rng(); - for dst in sram.iter_mut().step_by(2) { - *dst = rng.random(); - } - sram - } else { - let mut sram = Vec::with_capacity(8 * 1024); - sram_file.read_to_end(&mut sram)?; - sram - }; - - Ok(Cart { - rom_path: rom_path.to_path_buf(), - rom, - sram_file, - sram, - }) - } -} - -fn sram_path(rom_path: &Path, sim_id: SimId) -> PathBuf { - match sim_id { - SimId::Player1 => rom_path.with_extension("p1.sram"), - SimId::Player2 => rom_path.with_extension("p2.sram"), - } -} - pub struct EmulatorBuilder { rom: Option, commands: mpsc::Receiver, @@ -231,12 +182,12 @@ impl Emulator { } pub fn start_second_sim(&mut self, rom: Option) -> Result<()> { - let rom_path = if let Some(path) = rom { + let file_path = if let Some(path) = rom { Some(path) } else { - self.carts[0].as_ref().map(|c| c.rom_path.clone()) + self.carts[0].as_ref().map(|c| c.file_path.clone()) }; - let cart = match rom_path { + let cart = match file_path { Some(rom_path) => Some(Cart::load(&rom_path, SimId::Player2)?), None => None, }; @@ -349,8 +300,7 @@ impl Emulator { let cart = self.carts[sim_id.to_index()].as_mut(); if let (Some(sim), Some(cart)) = (sim, cart) { sim.read_sram(&mut cart.sram); - cart.sram_file.seek(SeekFrom::Start(0))?; - cart.sram_file.write_all(&cart.sram)?; + cart.save_sram()?; } Ok(()) } diff --git a/src/emulator/cart.rs b/src/emulator/cart.rs new file mode 100644 index 0000000..55cc134 --- /dev/null +++ b/src/emulator/cart.rs @@ -0,0 +1,63 @@ +use anyhow::Result; +use rand::Rng; +use std::{ + fs::{self, File}, + io::{Read, Seek as _, SeekFrom, Write as _}, + path::{Path, PathBuf}, +}; + +use crate::emulator::SimId; + +pub struct Cart { + pub file_path: PathBuf, + pub rom: Vec, + sram_file: File, + pub sram: Vec, +} + +impl Cart { + pub fn load(file_path: &Path, sim_id: SimId) -> Result { + let rom = fs::read(file_path)?; + + let mut sram_file = File::options() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(sram_path(file_path, sim_id))?; + + let sram = if sram_file.metadata()?.len() == 0 { + // new SRAM file, randomize the contents + let mut sram = vec![0; 8 * 1024]; + let mut rng = rand::rng(); + for dst in sram.iter_mut().step_by(2) { + *dst = rng.random(); + } + sram + } else { + let mut sram = Vec::with_capacity(8 * 1024); + sram_file.read_to_end(&mut sram)?; + sram + }; + + Ok(Cart { + file_path: file_path.to_path_buf(), + rom, + sram_file, + sram, + }) + } + + pub fn save_sram(&mut self) -> Result<()> { + self.sram_file.seek(SeekFrom::Start(0))?; + self.sram_file.write_all(&self.sram)?; + Ok(()) + } +} + +fn sram_path(file_path: &Path, sim_id: SimId) -> PathBuf { + match sim_id { + SimId::Player1 => file_path.with_extension("p1.sram"), + SimId::Player2 => file_path.with_extension("p2.sram"), + } +}