diff --git a/src/bus.rs b/src/bus.rs index e257fd0..20283da 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -7,6 +7,7 @@ use super::serial::Serial; use super::sound::Sound; use super::timer::Timer; use super::work_ram::{VariableWorkRAM, WorkRAM}; +use std::{convert::TryInto, fs::File, io::Read}; #[derive(Debug, Clone)] pub struct Bus { @@ -40,9 +41,15 @@ impl Default for Bus { } impl Bus { - pub fn with_boot() -> Self { + pub fn with_boot(path: &str) -> Self { + let mut file = File::open(path).unwrap(); + let mut buf = Vec::with_capacity(256); + file.read_to_end(&mut buf).unwrap(); + + let boot_rom: [u8; 256] = buf.try_into().unwrap(); + Self { - boot: Some(include_bytes!("../bin/DMG_ROM.bin").to_owned()), + boot: Some(boot_rom), ..Default::default() } } diff --git a/src/cpu.rs b/src/cpu.rs index 1fa7cb3..cbd05ed 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -34,9 +34,9 @@ impl Cpu { } } - pub fn boot_new() -> Self { + pub fn boot_new(path: &str) -> Self { Self { - bus: Bus::with_boot(), + bus: Bus::with_boot(path), ..Default::default() } } diff --git a/src/main.rs b/src/main.rs index 1f2d22a..c03874f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Result}; use gb::cpu::Cpu as LR35902; use pixels::{Pixels, SurfaceTexture}; +use std::env::args; use winit::{ dpi::LogicalSize, event::{Event, VirtualKeyCode}, @@ -19,7 +20,12 @@ fn main() -> Result<()> { let mut input = WinitInputHelper::new(); let window = create_window(&event_loop)?; let mut pixels = create_pixels(&window)?; - let mut game_boy = LR35902::new(); + + let mut game_boy = match args().nth(1) { + Some(boot_path) => LR35902::boot_new(&boot_path), + None => LR35902::new(), + }; + game_boy.load_cartridge("bin/cpu_instrs.gb"); event_loop.run(move |event, _, control_flow| {