Randomize SRAM when first created

This commit is contained in:
2025-08-03 18:42:19 -04:00
parent d4844a303a
commit 89dfeeeb2d
3 changed files with 17 additions and 3 deletions
+15 -3
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,