Skip to content

Commit 9a5ebbc

Browse files
committed
chore: update palette table addr mirroring. other nits
1 parent ac5d3db commit 9a5ebbc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/ppu.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct Ppu {
4949
vram: [u8; 2048],
5050

5151
/// Palette Table
52+
/// Backgrounds and sprites each have 4 palettes of 4 colors located at $3F00-$3F1F in VRAM
5253
palette_table: [u8; 32],
5354
oam_data: [u8; 256],
5455

@@ -74,7 +75,7 @@ pub struct PaletteIdx(usize);
7475
pub struct Sprite {
7576
pub x: u8,
7677
pub y: u8,
77-
pub use_tile_bank_1: bool,
78+
pub use_pattern_table_1: bool,
7879
pub tile_idx: u8,
7980
pub is_8_by_16: bool,
8081
pub palette_idx: PaletteIdx,
@@ -226,7 +227,7 @@ impl Ppu {
226227
Sprite {
227228
x,
228229
y,
229-
use_tile_bank_1: pattern_table_idx == 1,
230+
use_pattern_table_1: pattern_table_idx == 1,
230231
tile_idx,
231232
is_8_by_16,
232233
palette_idx,
@@ -401,7 +402,13 @@ impl Ppu {
401402
}
402403

403404
fn mirror_palettes_addr(&mut self, addr: u16) -> u16 {
404-
(addr - 0x3F00) % (self.palette_table.len() as u16)
405+
match addr {
406+
// Note that entry 0 of each palette is also unique in that its color value is shared between the background and sprite palettes, so writing to either one updates the same internal storage.
407+
// This means that the backdrop color can be written through both $3F00 and $3F10.
408+
0x3f10 | 0x3f14 | 0x3f18 | 0x3f1c => addr - 0x3F00 - 0x0010,
409+
0x3f00..=0x3fff => addr - 0x3F00,
410+
_ => panic!("invald palettes addr: {:04X}", addr),
411+
}
405412
}
406413

407414
pub fn write_to_oam_data(&mut self, data: u8) {
@@ -723,7 +730,7 @@ mod tests {
723730
flip_horizontal: true,
724731
flip_vertical: true,
725732
behind_background: true,
726-
use_tile_bank_1: true,
733+
use_pattern_table_1: true,
727734
..Default::default()
728735
};
729736
assert_eq!(

src/render.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ impl Frame {
8686
}
8787

8888
// Lookup the tile
89-
let bank = if sprite.use_tile_bank_1 { 1 } else { 0 };
9089
let tile_n = sprite.tile_idx as usize;
91-
let pattern_table = &chr_rom[bank * PATTERN_TABLE_SIZE..(bank + 1) * PATTERN_TABLE_SIZE];
90+
let pt_idx = if sprite.use_pattern_table_1 { 1 } else { 0 };
91+
let pattern_table =
92+
&chr_rom[pt_idx * PATTERN_TABLE_SIZE..(pt_idx + 1) * PATTERN_TABLE_SIZE];
9293
let tile = get_tile(&pattern_table, tile_n);
9394

9495
// Draw the tile

0 commit comments

Comments
 (0)