Skip to content

Commit

Permalink
Fix PPU LY on VBlank
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 14, 2023
1 parent 366c31b commit 18e6a2f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl Memory for MMU {
0xFF10..=0xFF3F => 0x00,
0xFF0F => self.intf.bits(),
0xFF70 => self.wram_bank as u8,
0xFEA0..=0xFEFF => 0xFF,
0xFFFF => self.inte.bits(),
_ => panic!("Read to unsupported address ({:#06x})!", a),
}
Expand Down Expand Up @@ -117,6 +118,8 @@ impl Memory for MMU {
0xFF0F => self.intf = Interrupts::from_bits(v).unwrap(),
0xFF50 => {},
0xFF70 => self.wram_bank = match v & 0x07 { 0 => 1, n => n as usize },
0xFEA0..=0xFEFF => {},
0xFF7F => {},
0xFFFF => self.inte = Interrupts::from_bits_truncate(v),
_ => panic!("Write to unsupported address ({:#06x})!", a),
}
Expand Down
24 changes: 17 additions & 7 deletions src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct PPU {
mode: GBMode,
ppu_mode: PPUMode,
cycle_count: u32,
vblanked_lines: u32,
sy: u8,
sx: u8,
ly: u8,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl PPU {
mode,
ppu_mode: PPUMode::OAMScan,
cycle_count: 0,
vblanked_lines: 0,
sy: 0x00,
sx: 0x00,
ly: 0x00,
Expand Down Expand Up @@ -185,14 +187,21 @@ impl PPU {
false
},
PPUMode::VBlank => {
if self.cycle_count > 4560 {
self.cycle_count -= 4560;
self.ly = 0;
self.ppu_mode = PPUMode::OAMScan;
if self.lcds.contains(LCDS::MODE_2_SELECT) {
self.interrupts |= Interrupts::LCD;
if self.cycle_count > 456 {
self.cycle_count -= 456;
self.vblanked_lines += 1;

if self.vblanked_lines >= 10 {
self.vblanked_lines = 0;
self.ly = 0;
self.ppu_mode = PPUMode::OAMScan;
if self.lcds.contains(LCDS::MODE_2_SELECT) {
self.interrupts |= Interrupts::LCD;
}
// println!("[PPU] Switching to OAMScan!");
} else {
self.ly += 1;
}
// println!("[PPU] Switching to OAMScan!");
}
false
}
Expand Down Expand Up @@ -462,6 +471,7 @@ impl Memory for PPU {
0xFF43 => self.sx = v,
0xFF44 => print!("Attempted to write to LY!"),
0xFF45 => self.lc = v,
0xFF46 => {},
0xFF47 => self.bgp = v,
0xFF48 => self.op0 = v,
0xFF49 => self.op1 = v,
Expand Down

0 comments on commit 18e6a2f

Please sign in to comment.