Compare commits

..

4 Commits

5 changed files with 185 additions and 623 deletions

684
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@ description = "An emulator for the Virtual Boy."
repository = "https://git.virtual-boy.com/PVB/lemur" repository = "https://git.virtual-boy.com/PVB/lemur"
publish = false publish = false
license = "MIT" license = "MIT"
version = "0.7.1" version = "0.7.2"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
@ -36,14 +36,12 @@ rtrb = "0.3"
rubato = "0.16" rubato = "0.16"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
sha2 = "0.10"
thread-priority = "2" thread-priority = "2"
tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync", "time"] } tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync", "time"] }
tracing = { version = "0.1", features = ["release_max_level_info"] } tracing = { version = "0.1", features = ["release_max_level_info"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
wgpu = "25" wgpu = "25"
winit = { version = "0.30", features = ["serde"] } winit = { version = "0.30", features = ["serde"] }
zip = "4"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
windows = { version = "0.61", features = ["Win32_System_Threading"] } windows = { version = "0.61", features = ["Win32_System_Threading"] }

@ -1 +1 @@
Subproject commit ecbd103917315e3aa24fd2a682208f5548ec5d1b Subproject commit fe9c5c47815c28a4618dad57337a58f0b216af0f

View File

@ -1,10 +1,7 @@
use anyhow::{Context, Result, bail}; use anyhow::Result;
use rand::Rng; use rand::Rng;
use serde::Deserialize;
use sha2::Digest;
use std::{ use std::{
ffi::OsStr, fs::{self, File},
fs,
io::{Read, Seek as _, SeekFrom, Write as _}, io::{Read, Seek as _, SeekFrom, Write as _},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -14,52 +11,38 @@ use crate::emulator::SimId;
pub struct Cart { pub struct Cart {
pub file_path: PathBuf, pub file_path: PathBuf,
pub rom: Vec<u8>, pub rom: Vec<u8>,
sram_file: fs::File, sram_file: File,
pub sram: Vec<u8>, pub sram: Vec<u8>,
} }
impl Cart { impl Cart {
pub fn load(file_path: &Path, sim_id: SimId) -> Result<Self> { pub fn load(file_path: &Path, sim_id: SimId) -> Result<Self> {
let vbx_extension = OsStr::new("vbx"); let rom = fs::read(file_path)?;
let contents = if file_path.extension() == Some(vbx_extension) {
read_bundle(file_path)
} else {
read_rom(file_path)
}?;
let mut sram_file = fs::File::options() let mut sram_file = File::options()
.read(true) .read(true)
.write(true) .write(true)
.create(true) .create(true)
.truncate(false) .truncate(false)
.open(sram_path(file_path, sim_id))?; .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 { let sram = if sram_file.metadata()?.len() == 0 {
// new SRAM file, randomize the contents // new SRAM file, randomize the contents
let mut sram = vec![0; sram_file_size]; let mut sram = vec![0; 16 * 1024];
let mut rng = rand::rng(); let mut rng = rand::rng();
for dst in sram for dst in sram.iter_mut().step_by(2) {
.iter_mut()
.step_by(if contents.sram_small_bus { 2 } else { 1 })
{
*dst = rng.random(); *dst = rng.random();
} }
sram sram
} else { } else {
let mut sram = Vec::with_capacity(sram_file_size); let mut sram = Vec::with_capacity(16 * 1024);
sram_file.read_to_end(&mut sram)?; sram_file.read_to_end(&mut sram)?;
sram.resize(sram_file_size, 0);
sram sram
}; };
Ok(Cart { Ok(Cart {
file_path: file_path.to_path_buf(), file_path: file_path.to_path_buf(),
rom: contents.rom, rom,
sram_file, sram_file,
sram, sram,
}) })
@ -72,81 +55,6 @@ 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 { fn sram_path(file_path: &Path, sim_id: SimId) -> PathBuf {
match sim_id { match sim_id {
SimId::Player1 => file_path.with_extension("p1.sram"), SimId::Player1 => file_path.with_extension("p1.sram"),

View File

@ -301,11 +301,11 @@ impl<T: Number> Widget for NumberEdit<'_, T> {
let mut delta = None; let mut delta = None;
if self.arrows { if self.arrows {
let arrow_left = res.rect.max.x + 4.0; let arrow_left = res.rect.max.x - 16.0;
let arrow_right = res.rect.max.x + 20.0; let arrow_right = res.rect.max.x;
let arrow_top = res.rect.min.y - 2.0; let arrow_top = res.rect.min.y;
let arrow_middle = (res.rect.min.y + res.rect.max.y) / 2.0; let arrow_middle = (res.rect.min.y + res.rect.max.y) / 2.0;
let arrow_bottom = res.rect.max.y + 2.0; let arrow_bottom = res.rect.max.y;
let top_arrow_rect = Rect { let top_arrow_rect = Rect {
min: (arrow_left, arrow_top).into(), min: (arrow_left, arrow_top).into(),