diff --git a/src/bus.rs b/src/bus.rs index 3d11a99..9ed354c 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -13,8 +13,8 @@ pub(crate) const BOOT_SIZE: usize = 0x100; #[derive(Debug)] pub struct Bus { boot: Option<[u8; BOOT_SIZE]>, // Boot ROM is 256b long - cart: Option, - ppu: Ppu, + pub(crate) cart: Option, + pub(crate) ppu: Ppu, work_ram: WorkRam, var_ram: VariableWorkRam, timer: Timer, @@ -22,7 +22,7 @@ pub struct Bus { apu: Apu, high_ram: HighRam, serial: Serial, - joypad: Joypad, + pub(crate) joyp: Joypad, } impl Default for Bus { @@ -38,7 +38,7 @@ impl Default for Bus { apu: Default::default(), high_ram: Default::default(), serial: Default::default(), - joypad: Default::default(), + joyp: Default::default(), } } } @@ -86,30 +86,10 @@ impl Bus { } } - #[inline] - pub(crate) fn joyp_mut(&mut self) -> &mut Joypad { - &mut self.joypad - } - - #[inline] - pub(crate) fn cart(&self) -> Option<&Cartridge> { - self.cart.as_ref() - } - - #[inline] - pub(crate) fn cart_mut(&mut self) -> Option<&mut Cartridge> { - self.cart.as_mut() - } - #[inline] pub(crate) fn apu_mut(&mut self) -> &mut Apu { &mut self.apu } - - #[inline] - pub fn ppu(&self) -> &Ppu { - &self.ppu - } } impl Bus { @@ -239,7 +219,7 @@ impl BusIo for Bus { // Every address here starts with 0xFF so we can just check the // low byte to figure out which register it is match addr & 0x00FF { - 0x00 => self.joypad.p1, + 0x00 => self.joyp.p1, 0x01 => self.serial.next, 0x02 => self.serial.ctrl.into(), 0x04 => (self.timer.divider >> 8) as u8, @@ -337,7 +317,7 @@ impl BusIo for Bus { // Every address here starts with 0xFF so we can just check the // low byte to figure out which register it is match addr & 0x00FF { - 0x00 => self.joypad.update(byte), + 0x00 => self.joyp.update(byte), 0x01 => self.serial.next = byte, 0x02 => self.serial.ctrl = byte.into(), 0x04 => self.timer.divider = 0x0000, @@ -401,7 +381,7 @@ impl Bus { let lcd_stat = self.ppu.int.lcd_stat(); // Read the current interrupt information from the Joypad - let joypad = self.joypad.interrupt(); + let joypad = self.joyp.interrupt(); // Read the current interrupt information from the Timer let timer = self.timer.interrupt(); @@ -431,7 +411,7 @@ impl Bus { self.ppu.int.set_lcd_stat(lcd_stat); // Update the Joypad's instance of the following interrupts - self.joypad.set_interrupt(joypad); + self.joyp.set_interrupt(joypad); // Update the Timer's instance of the following interrupts self.timer.set_interrupt(timer); diff --git a/src/emu.rs b/src/emu.rs index 923d0c0..1bcde89 100644 --- a/src/emu.rs +++ b/src/emu.rs @@ -1,7 +1,6 @@ use crate::apu::gen::SampleProducer; use crate::bus::BOOT_SIZE; use crate::cpu::Cpu; -use crate::joypad::{self, Joypad}; use crate::{Cycle, GB_HEIGHT, GB_WIDTH}; use clap::crate_name; use gilrs::Gilrs; @@ -20,9 +19,9 @@ pub fn run_frame(emu: &mut Emulator, gamepad: &mut Gilrs, key: &WinitInputHelper let mut elapsed = 0; if let Some(event) = gamepad.next_event() { - joypad::handle_gamepad_input(emu.joyp_mut(), event); + crate::joypad::handle_gamepad_input(&mut emu.cpu.bus.joyp, event); } - joypad::handle_keyboard_input(emu.joyp_mut(), key); + crate::joypad::handle_keyboard_input(&mut emu.cpu.bus.joyp, key); while elapsed < CYCLES_IN_FRAME { elapsed += emu.step(); @@ -32,7 +31,7 @@ pub fn run_frame(emu: &mut Emulator, gamepad: &mut Gilrs, key: &WinitInputHelper } pub fn draw_frame(emu: &Emulator, buf: &mut [u8; GB_HEIGHT * GB_WIDTH * 4]) { - buf.copy_from_slice(emu.cpu.bus.ppu().frame_buf()); + buf.copy_from_slice(emu.cpu.bus.ppu.frame_buf.as_ref()); } pub struct Emulator { @@ -84,11 +83,6 @@ impl Emulator { self.cpu.bus.load_cart(rom); } - #[inline] - fn joyp_mut(&mut self) -> &mut Joypad { - self.cpu.bus.joyp_mut() - } - pub fn set_prod(&mut self, prod: SampleProducer) { self.cpu.bus.apu_mut().attach_producer(prod) } @@ -98,7 +92,7 @@ impl Emulator { } pub fn try_write_sav(&self) -> std::io::Result<()> { - if let Some(ext_ram) = self.cpu.bus.cart().map(|c| c.ext_ram()).flatten() { + if let Some(ext_ram) = self.cpu.bus.cart.as_ref().map(|c| c.ext_ram()).flatten() { if let Some(title) = self.cpu.bus.cart_title() { let mut save_path = Self::data_path().unwrap_or_else(|| PathBuf::from(".")); save_path.push(title); @@ -113,7 +107,7 @@ impl Emulator { } pub fn try_load_sav(&mut self) -> std::io::Result<()> { - if let Some(cart) = self.cpu.bus.cart_mut() { + if let Some(cart) = &mut self.cpu.bus.cart { if let Some(title) = cart.title() { let mut save_path = Self::data_path().unwrap_or_else(|| PathBuf::from(".")); save_path.push(title); diff --git a/src/ppu.rs b/src/ppu.rs index 0911bc7..065d954 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -48,7 +48,7 @@ pub struct Ppu { fetch: PixelFetcher, fifo: PixelFifo, obj_buffer: ObjectBuffer, - frame_buf: Box<[u8; GB_WIDTH * GB_HEIGHT * 4]>, + pub(crate) frame_buf: Box<[u8; GB_WIDTH * GB_HEIGHT * 4]>, win_stat: WindowStatus, scanline_start: bool, @@ -423,11 +423,6 @@ impl Ppu { self.frame_buf.swap_with_slice(&mut blank); } - #[inline] - pub(crate) fn frame_buf(&self) -> &[u8; GB_HEIGHT * GB_WIDTH * 4] { - &self.frame_buf - } - fn clock_fifo(&mut self) -> Option { use RenderPriority::*;