Skip to content
Snippets Groups Projects
Verified Commit 354fe6f0 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

feat: better PPU implementation

parent 89fe647f
No related branches found
No related tags found
No related merge requests found
Pipeline #853 passed
...@@ -80,6 +80,8 @@ impl Cpu { ...@@ -80,6 +80,8 @@ impl Cpu {
let pc = self.pc; let pc = self.pc;
//@todo maybe remove this option as it may
// spend valuable resources
if pc >= 0x8000 && pc < 0x9fff { if pc >= 0x8000 && pc < 0x9fff {
panic!("Invalid PC area at 0x{:04x}", pc); panic!("Invalid PC area at 0x{:04x}", pc);
} }
......
...@@ -25,7 +25,7 @@ pub const FRAME_BUFFER_SIZE: usize = DISPLAY_WIDTH * DISPLAY_HEIGHT * RGB_SIZE; ...@@ -25,7 +25,7 @@ pub const FRAME_BUFFER_SIZE: usize = DISPLAY_WIDTH * DISPLAY_HEIGHT * RGB_SIZE;
/// # Basic usage /// # Basic usage
/// ```rust /// ```rust
/// let ppu = Ppu::new(); /// let ppu = Ppu::new();
/// ppu.tick(); /// ppu.clock();
/// ``` /// ```
pub struct Ppu { pub struct Ppu {
/// The 8 bit based RGB frame buffer with the /// The 8 bit based RGB frame buffer with the
...@@ -82,11 +82,12 @@ pub struct Ppu { ...@@ -82,11 +82,12 @@ pub struct Ppu {
stat_lyc: bool, stat_lyc: bool,
} }
#[derive(Clone, Copy)]
pub enum PpuMode { pub enum PpuMode {
OamRead, Hblank = 0,
VramRead, VBlank = 1,
Hblank, OamRead = 2,
VBlank, VramRead = 3,
} }
impl Ppu { impl Ppu {
...@@ -117,7 +118,14 @@ impl Ppu { ...@@ -117,7 +118,14 @@ impl Ppu {
} }
pub fn clock(&mut self, cycles: u8) { 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; self.mode_clock += cycles as u16;
match self.mode { match self.mode {
PpuMode::OamRead => { PpuMode::OamRead => {
if self.mode_clock >= 80 { if self.mode_clock >= 80 {
...@@ -127,7 +135,9 @@ impl Ppu { ...@@ -127,7 +135,9 @@ impl Ppu {
} }
PpuMode::VramRead => { PpuMode::VramRead => {
if self.mode_clock >= 172 { if self.mode_clock >= 172 {
self.render_line(); if self.switch_bg {
self.render_line();
}
self.mode_clock = 0; self.mode_clock = 0;
self.mode = PpuMode::Hblank; self.mode = PpuMode::Hblank;
...@@ -182,7 +192,8 @@ impl Ppu { ...@@ -182,7 +192,8 @@ impl Ppu {
let value = if self.stat_hblank { 0x04 } else { 0x00 } let value = if self.stat_hblank { 0x04 } else { 0x00 }
| if self.stat_vblank { 0x08 } else { 0x00 } | if self.stat_vblank { 0x08 } else { 0x00 }
| if self.stat_oam { 0x10 } 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 value
} }
0x0042 => self.scy, 0x0042 => self.scy,
...@@ -342,6 +353,14 @@ impl Ppu { ...@@ -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 /// Prints the tile data information to the stdout, this is
/// useful for debugging purposes. /// useful for debugging purposes.
pub fn draw_tile_stdout(&self, tile_index: usize) { pub fn draw_tile_stdout(&self, tile_index: usize) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment