Compare commits
1 Commits
main
...
vbx-suppor
Author | SHA1 | Date |
---|---|---|
|
b62046045d |
File diff suppressed because it is too large
Load Diff
|
@ -4,7 +4,7 @@ description = "An emulator for the Virtual Boy."
|
|||
repository = "https://git.virtual-boy.com/PVB/lemur"
|
||||
publish = false
|
||||
license = "MIT"
|
||||
version = "0.7.2"
|
||||
version = "0.7.1"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
@ -36,12 +36,14 @@ rtrb = "0.3"
|
|||
rubato = "0.16"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sha2 = "0.10"
|
||||
thread-priority = "2"
|
||||
tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync", "time"] }
|
||||
tracing = { version = "0.1", features = ["release_max_level_info"] }
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
wgpu = "25"
|
||||
winit = { version = "0.30", features = ["serde"] }
|
||||
zip = "4"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.61", features = ["Win32_System_Threading"] }
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit fe9c5c47815c28a4618dad57337a58f0b216af0f
|
||||
Subproject commit ecbd103917315e3aa24fd2a682208f5548ec5d1b
|
|
@ -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"),
|
||||
|
|
|
@ -301,11 +301,11 @@ impl<T: Number> Widget for NumberEdit<'_, T> {
|
|||
|
||||
let mut delta = None;
|
||||
if self.arrows {
|
||||
let arrow_left = res.rect.max.x - 16.0;
|
||||
let arrow_right = res.rect.max.x;
|
||||
let arrow_top = res.rect.min.y;
|
||||
let arrow_left = res.rect.max.x + 4.0;
|
||||
let arrow_right = res.rect.max.x + 20.0;
|
||||
let arrow_top = res.rect.min.y - 2.0;
|
||||
let arrow_middle = (res.rect.min.y + res.rect.max.y) / 2.0;
|
||||
let arrow_bottom = res.rect.max.y;
|
||||
let arrow_bottom = res.rect.max.y + 2.0;
|
||||
|
||||
let top_arrow_rect = Rect {
|
||||
min: (arrow_left, arrow_top).into(),
|
||||
|
|
Loading…
Reference in New Issue