From 3dce1726a8c9723516c44e35cc463d9b8cd210dc Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 14 Dec 2023 00:38:43 -0500 Subject: [PATCH] Make boot rom optional --- src/cpu.rs | 4 ++-- src/main.rs | 21 ++++++++++++++------- src/registers.rs | 6 +++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 3d5a9a6..94a6db7 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -11,9 +11,9 @@ pub struct CPU { } impl CPU { - pub fn new(mode: GBMode, rom: Vec) -> Self { + pub fn new(mode: GBMode, rom: Vec, booting: bool) -> Self { Self { - reg: Registers::new(mode), + reg: Registers::new(mode, booting), mem: MMU::new(mode, rom), halted: false, ime: false diff --git a/src/main.rs b/src/main.rs index a12ef32..967f005 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,22 +29,29 @@ pub const STEP_CYCLES: u32 = (STEP_TIME as f64 / (1000_f64 / CLOCK_FREQUENCY as #[derive(Parser)] struct Args { rom_path: String, - boot_rom: String + boot_rom: Option } #[tokio::main] async fn main() -> Result<(), impl std::error::Error> { let args = Args::parse(); let mut file = File::open(args.rom_path).expect("No ROM found!"); - let mut boot = File::open(args.boot_rom).expect("No Boot ROM found!"); let mut buffer = Vec::new(); - let mut boot_rom = Vec::new(); + let mut booting = true; file.read_to_end(&mut buffer).expect("Failed to read ROM!"); - boot.read_to_end(&mut boot_rom).expect("Failed to read Boot ROM!"); - // Display Nintendo Logo - buffer[0..=0x00FF].copy_from_slice(boot_rom.as_slice()); + match args.boot_rom { + Some(path) => { + let mut boot_rom = Vec::new(); + let mut boot = File::open(path).expect("No Boot ROM found!"); + boot.read_to_end(&mut boot_rom).expect("Failed to read Boot ROM!"); + + // Display Nintendo Logo + buffer[0..=0x00FF].copy_from_slice(boot_rom.as_slice()); + }, + None => booting = false + } // Get game name let name_data = &buffer[0x0134..=0x0143]; @@ -73,7 +80,7 @@ async fn main() -> Result<(), impl std::error::Error> { let context = Arc::clone(&context); // Start CPU tokio::spawn(async move { - let mut cpu = CPU::new(GBMode::Classic, buffer); + let mut cpu = CPU::new(GBMode::Classic, buffer, booting); let mut step_cycles = 0; let mut step_zero = Instant::now(); diff --git a/src/registers.rs b/src/registers.rs index 1f128ab..78a0304 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -81,7 +81,7 @@ impl Registers { } } - pub fn new(mode: GBMode) -> Registers { + pub fn new(mode: GBMode, booting: bool) -> Registers { match mode { GBMode::Classic => { Registers { @@ -93,7 +93,7 @@ impl Registers { e: 0xD8, h: 0x01, l: 0x4D, - pc: 0x0000, + pc: if booting { 0x0000 } else { 0x0100 }, sp: 0xFFFE } }, @@ -107,7 +107,7 @@ impl Registers { e: 0x56, h: 0x00, l: 0x0D, - pc: 0x0100, + pc: if booting { 0x0000 } else { 0x0100 }, sp: 0xFFFE } }