h/v counters

This commit is contained in:
2023-07-02 10:29:10 -05:00
parent 4ae49442ec
commit 300456a2ef
3 changed files with 45 additions and 4 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ impl CPU {
} }
fn push_emulation_interrupt(&mut self, bus: &mut Bus) { fn push_emulation_interrupt(&mut self, bus: &mut Bus) {
if ! self.registers.emulation_mode { if !self.registers.emulation_mode {
self.phk(bus); self.phk(bus);
} }
self.do_push(bus, &[ self.do_push(bus, &[
+42 -3
View File
@@ -2,6 +2,8 @@ use super::registers::PPURegisters;
pub struct PPU { pub struct PPU {
framebuffer: Vec<u8>, framebuffer: Vec<u8>,
h_count: u16,
v_count: u16,
pub registers: PPURegisters, pub registers: PPURegisters,
} }
@@ -9,21 +11,58 @@ impl PPU {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
framebuffer: vec![], framebuffer: vec![],
h_count: 0,
v_count: 0,
registers: PPURegisters::new(), registers: PPURegisters::new(),
} }
} }
pub fn tick(&mut self, cpu_cycles: usize) { pub fn tick(&mut self, cpu_cycles: usize) {
for _ in 0..cpu_cycles { for _ in 0..(cpu_cycles * 2) {
self.do_cycle(); self.dot_cycle();
} }
} }
pub fn do_cycle(&mut self) { pub fn dot_cycle(&mut self) {
self.increment_hv_count();
}
fn increment_hv_count(&mut self) {
self.h_count += 1;
if self.h_count > 339 {
self.h_count = 0;
self.v_count += 1;
if self.v_count > 261 {
self.v_count = 0;
}
}
} }
pub fn framebuffer(&self) -> &Vec<u8> { pub fn framebuffer(&self) -> &Vec<u8> {
&self.framebuffer &self.framebuffer
} }
}
#[cfg(test)]
mod ppu_general_test {
use super::*;
#[test]
fn test_increment_hv_count() {
let mut ppu = PPU::new();
ppu.increment_hv_count();
assert_eq!(ppu.h_count, 1);
ppu.h_count = 339;
ppu.increment_hv_count();
assert_eq!(ppu.h_count, 0);
assert_eq!(ppu.v_count, 1);
ppu.h_count = 339;
ppu.v_count = 261;
ppu.increment_hv_count();
assert_eq!(ppu.h_count, 0);
assert_eq!(ppu.v_count, 0);
}
} }
+2
View File
@@ -60,6 +60,8 @@ pub const WOBJLOG: u16 = 0x212B; // Window 2 Mask Logic (W)
pub const TMW: u16 = 0x212E; // Window Area Main Screen Disable (W) pub const TMW: u16 = 0x212E; // Window Area Main Screen Disable (W)
pub const TSW: u16 = 0x212F; // Window Area Sub Screen Disable (W) pub const TSW: u16 = 0x212F; // Window Area Sub Screen Disable (W)
// PPU Interrupts
pub const RDNMI: u16 = 0x4210; // V-Blank NMI Flag
pub const MAX_BG_WIDTH: usize = 16 * 64; pub const MAX_BG_WIDTH: usize = 16 * 64;
pub const MAX_BG_HEIGHT: usize = 16 * 64; pub const MAX_BG_HEIGHT: usize = 16 * 64;