Support reading VBX files in cart

This commit is contained in:
2025-08-07 21:15:30 -04:00
parent 6687b1f12f
commit b62046045d
3 changed files with 447 additions and 9 deletions
+101 -9
View File
@@ -1,7 +1,10 @@
use anyhow::Result;
use anyhow::{Context, Result, bail};
use rand::Rng;
use serde::Deserialize;
use sha2::Digest;
use std::{
fs::{self, File},
ffi::OsStr,
fs,
io::{Read, Seek as _, SeekFrom, Write as _},
path::{Path, PathBuf},
};
@@ -11,38 +14,52 @@ use crate::emulator::SimId;
pub struct Cart {
pub file_path: PathBuf,
pub rom: Vec<u8>,
sram_file: File,
sram_file: fs::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 vbx_extension = OsStr::new("vbx");
let contents = if file_path.extension() == Some(vbx_extension) {
read_bundle(file_path)
} else {
read_rom(file_path)
}?;
let mut sram_file = File::options()
let mut sram_file = fs::File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(sram_path(file_path, sim_id))?;
let sram_file_size = if contents.sram_small_bus {
contents.sram_size * 2
} else {
contents.sram_size
};
let sram = if sram_file.metadata()?.len() == 0 {
// new SRAM file, randomize the contents
let mut sram = vec![0; 16 * 1024];
let mut sram = vec![0; sram_file_size];
let mut rng = rand::rng();
for dst in sram.iter_mut().step_by(2) {
for dst in sram
.iter_mut()
.step_by(if contents.sram_small_bus { 2 } else { 1 })
{
*dst = rng.random();
}
sram
} else {
let mut sram = Vec::with_capacity(16 * 1024);
let mut sram = Vec::with_capacity(sram_file_size);
sram_file.read_to_end(&mut sram)?;
sram.resize(sram_file_size, 0);
sram
};
Ok(Cart {
file_path: file_path.to_path_buf(),
rom,
rom: contents.rom,
sram_file,
sram,
})
@@ -55,6 +72,81 @@ impl Cart {
}
}
struct CartContents {
rom: Vec<u8>,
sram_size: usize,
sram_small_bus: bool,
}
fn read_bundle(file_path: &Path) -> Result<CartContents> {
let file = fs::File::open(file_path)?;
let mut archive = zip::ZipArchive::new(file).context("invalid VBX")?;
let manifest_reader = archive
.by_name("manifest.json")
.context("manifest.json not found")?;
let manifest: Manifest =
serde_json::from_reader(manifest_reader).context("malformed manifest")?;
let rom = {
let mut rom_file = archive
.by_name(&manifest.rom.file)
.context("ROM file not found")?;
let mut buffer = Vec::with_capacity(rom_file.size() as usize);
rom_file
.read_to_end(&mut buffer)
.context("could not read ROM")?;
buffer
};
if let Some(hash) = manifest.rom.sha256 {
let expected_hash = hex::decode(hash)?;
let actual_hash = sha2::Sha256::digest(&rom);
if expected_hash[..] != actual_hash[..] {
bail!("Incorrect ROM hash");
}
}
let sram_size = manifest.sram.as_ref().map(|s| s.size).unwrap_or(8192);
if !sram_size.is_power_of_two() {
bail!("Invalid SRAM size, must be power of two")
}
let sram_small_bus = manifest.sram.and_then(|s| s.bus_width).unwrap_or_default() < 16;
Ok(CartContents {
rom,
sram_size,
sram_small_bus,
})
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Manifest {
rom: ManifestRom,
sram: Option<ManifestSram>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestRom {
file: String,
sha256: Option<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestSram {
size: usize,
bus_width: Option<usize>,
}
fn read_rom(rom_path: &Path) -> Result<CartContents> {
let rom = fs::read(rom_path)?;
Ok(CartContents {
rom,
sram_size: 8192,
sram_small_bus: true,
})
}
fn sram_path(file_path: &Path, sim_id: SimId) -> PathBuf {
match sim_id {
SimId::Player1 => file_path.with_extension("p1.sram"),