From 1c85827e94cec071215e758d6a39e4c7733031ef Mon Sep 17 00:00:00 2001 From: Theron Spiegl Date: Thu, 9 Jan 2020 19:49:31 -0600 Subject: [PATCH] fix graphical glitch, mapper idea --- src/main.rs | 1 - src/memory_model.rs | 34 ++++++++++++++++++++++++++++++++++ src/ppu/rendering.rs | 15 ++++++++------- 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 src/memory_model.rs diff --git a/src/main.rs b/src/main.rs index bc072b7..d99c7f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,7 +114,6 @@ fn main() -> Result<(), String> { println!("samples per second: {}", sps); sps = 0; - } } // PROFILER.lock().unwrap().stop().unwrap(); diff --git a/src/memory_model.rs b/src/memory_model.rs new file mode 100644 index 0000000..ca54bd0 --- /dev/null +++ b/src/memory_model.rs @@ -0,0 +1,34 @@ +use std::rc::Rc; +use std::cell::RefCell; + +fn main() { + let mapper = Rc::new(RefCell::new(Mmc1{a:32})); + + let ppu = Ppu{mapper: mapper.clone()}; + let cpu = Cpu{mapper: mapper.clone()}; + println!("{}", cpu.mapper.borrow_mut().doit()); + println!("{}", ppu.mapper.borrow_mut().doit()); +} + +struct Mmc1 { + a: u8, +} + +trait Mapper { + fn doit(&mut self) -> u8; +} + +impl Mapper for Mmc1 { + fn doit(&mut self) -> u8 { + self.a += 1; + self.a + } +} + +struct Ppu { + mapper: Rc>, +} + +struct Cpu { + mapper: Rc>, +} diff --git a/src/ppu/rendering.rs b/src/ppu/rendering.rs index 1774ba0..9824083 100644 --- a/src/ppu/rendering.rs +++ b/src/ppu/rendering.rs @@ -142,12 +142,6 @@ impl super::Ppu { let mut low_bit = 0; let mut high_bit = 0; let mut secondary_index = 0; - // Every cycle, the 8 x-position counters for the sprites are decremented by one. - for i in 0..self.num_sprites { - if self.sprite_counters[i] > 0 { - self.sprite_counters[i] -= 1; - } - } for i in 0..self.num_sprites { // If the counter is zero, the sprite becomes "active", and the respective pair of shift registers for the sprite is shifted once every cycle. // This output accompanies the data in the sprite's latch, to form a pixel. @@ -169,6 +163,12 @@ impl super::Ppu { self.sprite_pattern_table_srs[i].1 <<= 1; } } + // Every cycle, the 8 x-position counters for the sprites are decremented by one. + for i in 0..self.num_sprites { + if self.sprite_counters[i] > 0 { + self.sprite_counters[i] -= 1; + } + } ((high_bit << 1) | low_bit, secondary_index) } else { (0, 0) @@ -193,6 +193,7 @@ impl super::Ppu { } } self.num_sprites = sprite_count; + } pub fn fetch_sprites(&mut self) { @@ -201,7 +202,7 @@ impl super::Ppu { let sprite_y_position = self.secondary_oam[(4*i)+0] as usize; // byte 0 of sprite, sprite's vertical position on screen let sprite_tile_index = self.secondary_oam[(4*i)+1] as usize; // byte 1 of sprite, sprite's location within pattern table let sprite_attributes = self.secondary_oam[(4*i)+2]; // byte 2 of sprite, sprite's palette, priority, and flip attributes - let sprite_x_position = self.secondary_oam[(4*i)+3] + 1; // byte 3 of sprite, sprite's horizontal position on screen + let sprite_x_position = self.secondary_oam[(4*i)+3]; // byte 3 of sprite, sprite's horizontal position on screen // For 8x8 sprites, this is the tile number of this sprite within the pattern table selected in bit 3 of PPUCTRL ($2000). if self.sprite_size == 8 { address = self.sprite_pattern_table_base;