take either loaded rom or path to rom

This commit is contained in:
Alex Janka 2023-03-06 17:23:46 +11:00
parent b13dc13d1f
commit 4a2c15e5ec
3 changed files with 28 additions and 20 deletions

View file

@ -9,7 +9,7 @@ use cpal::{
};
use futures::executor;
use gb_emu_lib::{
connect::{AudioOutput, EmulatorMessage, JoypadState, Renderer},
connect::{AudioOutput, EmulatorMessage, JoypadState, Renderer, RomFile},
util::scale_buffer,
};
use gilrs::{
@ -75,7 +75,7 @@ fn main() {
};
let options = gb_emu_lib::Options {
rom_path: args.rom,
rom: RomFile::Path(args.rom),
save_path: args.save,
no_save: args.no_save,
bootrom_path: args.bootrom,

View file

@ -6,6 +6,11 @@ pub enum EmulatorMessage {
Stop,
}
pub enum RomFile {
Path(String),
Raw(Vec<u8>),
}
pub trait Renderer {
fn prepare(&mut self, width: usize, height: usize);

View file

@ -10,7 +10,7 @@ use crate::{
processor::memory::Memory,
util::{pause, print_cycles},
};
use connect::{AudioOutput, EmulatorMessage, Renderer};
use connect::{AudioOutput, EmulatorMessage, Renderer, RomFile};
use once_cell::sync::OnceCell;
use processor::{memory::Rom, Cpu};
use std::{
@ -29,7 +29,7 @@ mod processor;
pub mod util;
pub struct Options {
pub rom_path: String,
pub rom: RomFile,
pub save_path: Option<String>,
pub no_save: bool,
pub bootrom_path: Option<String>,
@ -56,24 +56,27 @@ pub fn init(
) {
VERBOSE.set(options.verbose).unwrap();
let maybe_save = if options.no_save {
None
} else {
Some(if let Some(path) = options.save_path {
PathBuf::from_str(&path).unwrap()
} else {
PathBuf::from_str(&options.rom_path)
.unwrap()
.with_extension("sav")
})
};
let rom = match options.rom {
RomFile::Path(path) => {
let maybe_save = if options.no_save {
None
} else {
Some(if let Some(path) = options.save_path {
PathBuf::from_str(&path).unwrap()
} else {
PathBuf::from_str(&path).unwrap().with_extension("sav")
})
};
let rom: Rom = match fs::read(options.rom_path) {
Ok(data) => Rom::load(data, maybe_save),
Err(e) => {
println!("Error reading ROM: {e}");
return;
match fs::read(path) {
Ok(data) => Rom::load(data, maybe_save),
Err(e) => {
println!("Error reading ROM: {e}");
return;
}
}
}
RomFile::Raw(data) => Rom::load(data, None),
};
window.prepare(WIDTH, HEIGHT);