Skip to content

Commit

Permalink
Make boot rom optional
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 14, 2023
1 parent 7656cf8 commit 3dce172
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub struct CPU {
}

impl CPU {
pub fn new(mode: GBMode, rom: Vec<u8>) -> Self {
pub fn new(mode: GBMode, rom: Vec<u8>, booting: bool) -> Self {
Self {
reg: Registers::new(mode),
reg: Registers::new(mode, booting),
mem: MMU::new(mode, rom),
halted: false,
ime: false
Expand Down
21 changes: 14 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
}

#[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];
Expand Down Expand Up @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions src/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -93,7 +93,7 @@ impl Registers {
e: 0xD8,
h: 0x01,
l: 0x4D,
pc: 0x0000,
pc: if booting { 0x0000 } else { 0x0100 },
sp: 0xFFFE
}
},
Expand All @@ -107,7 +107,7 @@ impl Registers {
e: 0x56,
h: 0x00,
l: 0x0D,
pc: 0x0100,
pc: if booting { 0x0000 } else { 0x0100 },
sp: 0xFFFE
}
}
Expand Down

0 comments on commit 3dce172

Please sign in to comment.