diff --git a/src/cpu.rs b/src/cpu.rs index 3940709a55a2d363f854c05e434efc687585c0c8..dab920c8e61e1b2350531f2ee093ac72358c3627 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -80,6 +80,8 @@ impl Cpu { let pc = self.pc; + //@todo maybe remove this option as it may + // spend valuable resources if pc >= 0x8000 && pc < 0x9fff { panic!("Invalid PC area at 0x{:04x}", pc); } diff --git a/src/ppu.rs b/src/ppu.rs index 2a87d7cf5088b6d2b31576033409fc668ce52f51..06598cb7002d4ce38fd210596ea3887cac0d358b 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -25,7 +25,7 @@ pub const FRAME_BUFFER_SIZE: usize = DISPLAY_WIDTH * DISPLAY_HEIGHT * RGB_SIZE; /// # Basic usage /// ```rust /// let ppu = Ppu::new(); -/// ppu.tick(); +/// ppu.clock(); /// ``` pub struct Ppu { /// The 8 bit based RGB frame buffer with the @@ -82,11 +82,12 @@ pub struct Ppu { stat_lyc: bool, } +#[derive(Clone, Copy)] pub enum PpuMode { - OamRead, - VramRead, - Hblank, - VBlank, + Hblank = 0, + VBlank = 1, + OamRead = 2, + VramRead = 3, } impl Ppu { @@ -117,7 +118,14 @@ impl Ppu { } pub fn clock(&mut self, cycles: u8) { + if !self.switch_lcd { + return; + } + + // increments the current mode clock by the provided amount + // of CPU cycles (probably coming from a previous CPU clock) self.mode_clock += cycles as u16; + match self.mode { PpuMode::OamRead => { if self.mode_clock >= 80 { @@ -127,7 +135,9 @@ impl Ppu { } PpuMode::VramRead => { if self.mode_clock >= 172 { - self.render_line(); + if self.switch_bg { + self.render_line(); + } self.mode_clock = 0; self.mode = PpuMode::Hblank; @@ -182,7 +192,8 @@ impl Ppu { let value = if self.stat_hblank { 0x04 } else { 0x00 } | if self.stat_vblank { 0x08 } else { 0x00 } | if self.stat_oam { 0x10 } else { 0x00 } - | if self.stat_lyc { 0x20 } else { 0x00 }; + | if self.stat_lyc { 0x20 } else { 0x00 } + | self.mode as u8; value } 0x0042 => self.scy, @@ -342,6 +353,14 @@ impl Ppu { } } + pub fn fill_frame_buffer(&mut self, color: [u8; RGB_SIZE]) { + for index in (0..self.frame_buffer.len()).step_by(RGB_SIZE) { + self.frame_buffer[index] = color[0]; + self.frame_buffer[index + 1] = color[1]; + self.frame_buffer[index + 2] = color[2]; + } + } + /// Prints the tile data information to the stdout, this is /// useful for debugging purposes. pub fn draw_tile_stdout(&self, tile_index: usize) {