Move cart to separate file

This commit is contained in:
Simon Gellis 2025-08-06 23:31:32 -04:00
parent 89dfeeeb2d
commit af2df78809
2 changed files with 69 additions and 56 deletions

View File

@ -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<u8>,
sram_file: File,
sram: Vec<u8>,
}
impl Cart {
fn load(rom_path: &Path, sim_id: SimId) -> Result<Self> {
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<PathBuf>,
commands: mpsc::Receiver<EmulatorCommand>,
@ -231,12 +182,12 @@ impl Emulator {
}
pub fn start_second_sim(&mut self, rom: Option<PathBuf>) -> 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(())
}

63
src/emulator/cart.rs Normal file
View File

@ -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<u8>,
sram_file: File,
pub sram: Vec<u8>,
}
impl Cart {
pub fn load(file_path: &Path, sim_id: SimId) -> Result<Self> {
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"),
}
}