Actually disable sprite and background rendering based on mask

This commit is contained in:
2026-03-16 03:01:37 -05:00
parent b9a30c286a
commit fa88c825a6

View File

@@ -824,7 +824,9 @@ impl PPU {
self.background.second_pattern = val & 0b0001_0000 != 0; self.background.second_pattern = val & 0b0001_0000 != 0;
self.oam.large_sprites = val & 0b0010_0000 != 0; self.oam.large_sprites = val & 0b0010_0000 != 0;
if val & 0b0100_0000 != 0 { if val & 0b0100_0000 != 0 {
println!("WARNING: Bit 6 set in PPUCTRL - may cause damage on physical hardware"); println!(
"WARNING: Bit 6 set in PPUCTRL - may cause damage on physical hardware"
);
} }
self.nmi_enabled = val & 0b1000_0000 != 0; self.nmi_enabled = val & 0b1000_0000 != 0;
} }
@@ -954,33 +956,40 @@ impl PPU {
} else if self.pixel < 257 || self.pixel > 320 { } else if self.pixel < 257 || self.pixel > 320 {
// self.dbg_int = true; // self.dbg_int = true;
// const POS: u32 = 1 << 15; // const POS: u32 = 1 << 15;
let bit_pos = 15 - self.background.x; let bg_color = if self.mask.enable_background {
// Determine background color let bit_pos = 15 - self.background.x;
let a = (self.background.cur_shift_high >> bit_pos) & 1; // Determine background color
let b = (self.background.cur_shift_low >> bit_pos) & 1; let a = (self.background.cur_shift_high >> bit_pos) & 1;
let val = (a << 1) | b; let b = (self.background.cur_shift_low >> bit_pos) & 1;
debug_assert!(val < 4); let val = (a << 1) | b;
debug_assert!(val < 4);
let a = (self.background.cur_attr_shift_high >> bit_pos) & 1; let a = (self.background.cur_attr_shift_high >> bit_pos) & 1;
let b = (self.background.cur_attr_shift_low >> bit_pos) & 1; let b = (self.background.cur_attr_shift_low >> bit_pos) & 1;
let palette = ((a << 1) | b) as u8; let palette = ((a << 1) | b) as u8;
debug_assert!(palette < 4); debug_assert!(palette < 4);
let color = val as u8 + if val != 0 { palette * 4 } else { 0 }; val as u8 + if val != 0 { palette * 4 } else { 0 }
} else {
0
};
if self.scanline < 240 && self.pixel < 257 { if self.scanline < 240 && self.pixel < 257 {
let color = if let Some((sp_color, above, is_sprite_zero)) = let color = if let Some((sp_color, above, is_sprite_zero)) = self
self.oam.pixel(self.pixel, self.scanline) .mask
.enable_sprites
.then_some(())
.and(self.oam.pixel(self.pixel, self.scanline))
{ {
if is_sprite_zero && sp_color != 0 && color != 0 { if is_sprite_zero && sp_color != 0 && bg_color != 0 {
self.sprite_zero_hit = true; self.sprite_zero_hit = true;
} }
if sp_color == 0 || (color != 0 && !above) { if sp_color == 0 || (bg_color != 0 && !above) {
color bg_color
} else { } else {
sp_color sp_color
} }
} else { } else {
color bg_color
}; };
// Write to screen // Write to screen
self.render_buffer.write( self.render_buffer.write(
@@ -1014,7 +1023,9 @@ impl PPU {
| ((self.background.v >> 2) & 0x07); | ((self.background.v >> 2) & 0x07);
// println!("Cur: {:04X}, comp: {:04X}", addr, addr_2); // println!("Cur: {:04X}, comp: {:04X}", addr, addr_2);
// assert_eq!(addr, addr_2); // assert_eq!(addr, addr_2);
let val = mem.read(addr).reg_map(|_, offset| self.palette.ram(offset as u8)); let val = mem
.read(addr)
.reg_map(|_, offset| self.palette.ram(offset as u8));
self.background.next_attr = val; self.background.next_attr = val;
} else if self.pixel % 8 == 6 { } else if self.pixel % 8 == 6 {
// BG pattern low // BG pattern low