From b43c8ac7c9fce26e63d666c5d735056d0efc84ab Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 14 Apr 2021 01:21:45 -0500 Subject: [PATCH] feat(cartridge): grab title from the ROM --- src/bus.rs | 4 ++++ src/cartridge.rs | 17 +++++++++++++++++ src/cpu.rs | 4 ++++ src/main.rs | 11 +++++++---- src/ppu.rs | 12 +++++++----- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 4d7d676..4181683 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -63,6 +63,10 @@ impl Bus { Ok(()) } + pub fn rom_title(&self) -> Option<&str> { + self.cartridge.as_ref()?.title() + } + pub fn step(&mut self, cycles: Cycle) { self.ppu.step(cycles); self.timer.step(cycles); diff --git a/src/cartridge.rs b/src/cartridge.rs index 4f66dd5..6d78c0e 100644 --- a/src/cartridge.rs +++ b/src/cartridge.rs @@ -9,6 +9,7 @@ const MBC_TYPE_ADDRESS: usize = 0x0147; #[derive(Debug, Clone, Default)] pub struct Cartridge { memory: Vec, + title: Option, mbc: Box, } @@ -20,6 +21,7 @@ impl Cartridge { Ok(Self { mbc: Self::detect_mbc(&memory), + title: Self::find_title(&memory), memory, }) } @@ -46,6 +48,21 @@ impl Cartridge { } } + fn find_title(memory: &[u8]) -> Option { + // FIXME: Get rid of magic values and handle cases + // where 0x134..0x143 reads past the length of the + // string + + let slice = &memory[0x134..0x143]; + + let str_with_nulls = std::str::from_utf8(slice).ok(); + str_with_nulls.map(|s| s.trim_matches('\0').to_string()) + } + + pub fn title(&self) -> Option<&str> { + self.title.as_deref() + } + fn find_ram_size(memory: &[u8]) -> RamSize { let id = memory[RAM_SIZE_ADDRESS]; id.into() diff --git a/src/cpu.rs b/src/cpu.rs index 7dafb42..28281c9 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -68,6 +68,10 @@ impl Cpu { pub fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> { self.bus.load_cartridge(path) } + + pub fn rom_title(&self) -> Option<&str> { + self.bus.rom_title() + } } impl Cpu { diff --git a/src/main.rs b/src/main.rs index f7be0e6..506ed42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ fn main() -> Result<()> { .takes_value(true) .required(true) .index(1) - .help("The ROM that the emulator will run"), + .help("Path to the Game ROM"), ) .arg( Arg::with_name("boot") @@ -58,10 +58,13 @@ fn main() -> Result<()> { .load_cartridge(rom_path) .expect("Failed to load ROM"); + let default_title = "DMG-01 Emulator"; + let cartridge_title = game_boy.rom_title().unwrap_or(&default_title); + // Initialize GUI let event_loop = EventLoop::new(); let mut input = WinitInputHelper::new(); - let window = create_window(&event_loop)?; + let window = create_window(&event_loop, cartridge_title)?; let mut pixels = create_pixels(&window)?; let mut now = Instant::now(); @@ -114,10 +117,10 @@ fn main() -> Result<()> { }); } -fn create_window(event_loop: &EventLoop<()>) -> Result { +fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result { let size = LogicalSize::new((GB_WIDTH as f64) * SCALE, (GB_HEIGHT as f64) * SCALE); Ok(WindowBuilder::new() - .with_title("DMG-1 Emulator") + .with_title(title) .with_inner_size(size) .with_min_inner_size(size) .build(&event_loop)?) diff --git a/src/ppu.rs b/src/ppu.rs index ea369c4..c1be252 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -49,7 +49,7 @@ impl Ppu { let start: u32 = self.cycles.into(); let end: u32 = cycles.into(); - for cycle in start..(start + end).into() { + for _ in start..(start + end) { self.cycles += 1; match self.stat.mode() { @@ -155,10 +155,12 @@ impl Ppu { let attr = self.oam.attribute((cycle / 2) as usize); let line_y = self.pos.line_y + 16; - if attr.x > 0 && line_y >= attr.y && line_y < (attr.y + sprite_height) { - if !self.sprite_buffer.full() { - self.sprite_buffer.add(attr); - } + if attr.x > 0 + && line_y >= attr.y + && line_y < (attr.y + sprite_height) + && !self.sprite_buffer.full() + { + self.sprite_buffer.add(attr); } } }