From 4a2c15e5ecba28d9e7f8c5a76d7f4500a411ce44 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 6 Mar 2023 17:23:46 +1100 Subject: [PATCH] take either loaded rom or path to rom --- gb-emu/src/main.rs | 4 ++-- lib/src/connect/mod.rs | 5 +++++ lib/src/lib.rs | 39 +++++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/gb-emu/src/main.rs b/gb-emu/src/main.rs index fc095af..8a440fa 100644 --- a/gb-emu/src/main.rs +++ b/gb-emu/src/main.rs @@ -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, diff --git a/lib/src/connect/mod.rs b/lib/src/connect/mod.rs index 14c4ad9..d94027f 100644 --- a/lib/src/connect/mod.rs +++ b/lib/src/connect/mod.rs @@ -6,6 +6,11 @@ pub enum EmulatorMessage { Stop, } +pub enum RomFile { + Path(String), + Raw(Vec), +} + pub trait Renderer { fn prepare(&mut self, width: usize, height: usize); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index ec64863..8d4b6d0 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -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, pub no_save: bool, pub bootrom_path: Option, @@ -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);