Randomize SRAM when first created

This commit is contained in:
Simon Gellis 2025-08-03 18:42:19 -04:00
parent d4844a303a
commit 89dfeeeb2d
3 changed files with 17 additions and 3 deletions

1
Cargo.lock generated
View File

@ -1842,6 +1842,7 @@ dependencies = [
"num-traits",
"oneshot",
"pollster",
"rand 0.9.2",
"rfd",
"rtrb",
"rubato",

View File

@ -30,6 +30,7 @@ num-derive = "0.4"
num-traits = "0.2"
oneshot = "0.1"
pollster = "0.4"
rand = "0.9"
rfd = { version = "0.15", default-features = false, features = ["xdg-portal", "async-std"]}
rtrb = "0.3"
rubato = "0.16"

View File

@ -16,6 +16,7 @@ use anyhow::Result;
use atomic::Atomic;
use bytemuck::NoUninit;
use egui_notify::Toast;
use rand::Rng;
use tracing::{error, warn};
use crate::{
@ -78,10 +79,21 @@ impl Cart {
.create(true)
.truncate(false)
.open(sram_path(rom_path, sim_id))?;
sram_file.set_len(8 * 1024)?;
let mut sram = vec![];
sram_file.read_to_end(&mut sram)?;
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,