|
1 | 1 | use bitflags::bitflags;
|
2 | 2 |
|
3 |
| -use crate::{ |
4 |
| - addr_register::AddrRegister, |
5 |
| - palette::{SYSTEM_PALETTE}, |
6 |
| - rom::Mirroring, |
7 |
| -}; |
| 3 | +use crate::{addr_register::AddrRegister, palette::SYSTEM_PALETTE, rom::Mirroring}; |
8 | 4 |
|
9 | 5 | // TODO: move UI rendering stuff that ties to SDL2 out of PPU
|
10 | 6 | pub struct Frame {
|
@@ -32,7 +28,6 @@ impl Frame {
|
32 | 28 |
|
33 | 29 | // tile_n can be thought of as the offset in the pattern table (CHR ROM)
|
34 | 30 | // pos (*8) relates to the value in the name table.
|
35 |
| - /// VRAM (also called "name tables") |
36 | 31 | pub fn draw_bg_tile(
|
37 | 32 | &mut self,
|
38 | 33 | chr_rom: &[u8],
|
@@ -145,8 +140,8 @@ pub struct PpuRegisters {
|
145 | 140 | pub struct Ppu {
|
146 | 141 | /// CHR ROM (also called "pattern tables")
|
147 | 142 | chr_rom: Vec<u8>,
|
148 |
| - /// VRAM (also called "name tables") |
149 | 143 |
|
| 144 | + /// VRAM (also called "name tables") |
150 | 145 | /// 1024 bytes make up a single "Frame"
|
151 | 146 | /// The PPU has 2048 bytes here, representing 2 frames.
|
152 | 147 | /// NOTE: the PPU is addressable via 4096 bytes, meaning there's some remapping of those additional bytes.
|
@@ -220,7 +215,7 @@ impl Ppu {
|
220 | 215 | let p_idx = palette_idx.0;
|
221 | 216 | assert!(p_idx < 8);
|
222 | 217 |
|
223 |
| - let start = (p_idx * 4); |
| 218 | + let start = p_idx * 4; |
224 | 219 | [
|
225 | 220 | // The first entry is a special case
|
226 | 221 | if is_background { self.palettes[0] } else { 0 },
|
@@ -275,7 +270,6 @@ impl Ppu {
|
275 | 270 |
|
276 | 271 | // tile_n can be thought of as the offset in the pattern table (CHR ROM)
|
277 | 272 | // pos (*8) relates to the value in the name table.
|
278 |
| - /// VRAM (also called "name tables") |
279 | 273 | pub fn draw_background(&self, frame: &mut Frame) {
|
280 | 274 | // Determine which nametable is being used for the current screen (by reading bit 0 and bit 1 from Control register)
|
281 | 275 | let which_nametable = self
|
@@ -506,7 +500,7 @@ impl Ppu {
|
506 | 500 | let name_table_idx = base / 0x0400;
|
507 | 501 |
|
508 | 502 | // and ROM-configured mirroring
|
509 |
| - |
| 503 | + |
510 | 504 | match (self.mirroring, name_table_idx) {
|
511 | 505 | (Mirroring::Horizontal, 1) | (Mirroring::Horizontal, 2) => base - 0x0400,
|
512 | 506 | (Mirroring::Vertical, 2) | (Mirroring::Vertical, 3) | (Mirroring::Horizontal, 3) => {
|
@@ -692,9 +686,7 @@ mod tests {
|
692 | 686 |
|
693 | 687 | assert_eq!(ppu.get_tick_status(), (0, 0));
|
694 | 688 | assert_eq!(ppu.nmi_interrupt, None);
|
695 |
| - assert!( |
696 |
| - !ppu.registers.status.contains(StatusRegister::VBLANK_FLAG), |
697 |
| - ); |
| 689 | + assert!(!ppu.registers.status.contains(StatusRegister::VBLANK_FLAG),); |
698 | 690 | assert!(!ppu.is_vblank_nmi_enabled());
|
699 | 691 |
|
700 | 692 | ppu.registers
|
@@ -858,36 +850,42 @@ mod tests {
|
858 | 850 | fn test_parse_sprite_from_oam_data() {
|
859 | 851 | let mut ppu = new_test_ppu();
|
860 | 852 |
|
861 |
| - let mut expected = Sprite::default(); |
862 |
| - expected.palette_idx = PaletteIdx(4); |
| 853 | + let expected = Sprite { |
| 854 | + palette_idx: PaletteIdx(4), |
| 855 | + ..Default::default() |
| 856 | + }; |
863 | 857 | assert_eq!(ppu.parse_sprite_from_oam_data(&[0, 0, 0, 0]), expected);
|
864 | 858 |
|
865 | 859 | ppu.registers
|
866 | 860 | .control
|
867 | 861 | .insert(ControlRegister::SPRITE_PATTERN_ADDR);
|
868 |
| - let mut expected = Sprite::default(); |
869 |
| - expected.x = 5; |
870 |
| - expected.y = 10; |
871 |
| - expected.tile_idx = 123; |
872 |
| - expected.palette_idx = PaletteIdx(6); |
873 |
| - expected.flip_horizontal = true; |
874 |
| - expected.flip_vertical = true; |
875 |
| - expected.behind_background = true; |
876 |
| - expected.use_tile_bank_1 = true; |
| 862 | + let expected = Sprite { |
| 863 | + x: 5, |
| 864 | + y: 10, |
| 865 | + tile_idx: 123, |
| 866 | + palette_idx: PaletteIdx(6), |
| 867 | + flip_horizontal: true, |
| 868 | + flip_vertical: true, |
| 869 | + behind_background: true, |
| 870 | + use_tile_bank_1: true, |
| 871 | + ..Default::default() |
| 872 | + }; |
877 | 873 | assert_eq!(
|
878 | 874 | ppu.parse_sprite_from_oam_data(&[10, 123, 0b1110_0010, 5]),
|
879 | 875 | expected
|
880 | 876 | );
|
881 | 877 |
|
882 | 878 | ppu.registers.control.insert(ControlRegister::SPRITE_SIZE);
|
883 |
| - let mut expected = Sprite::default(); |
884 |
| - expected.x = 1; |
885 |
| - expected.y = 2; |
886 |
| - expected.tile_idx = 16; |
887 |
| - expected.palette_idx = PaletteIdx(7); |
888 |
| - expected.flip_vertical = true; |
889 |
| - expected.behind_background = true; |
890 |
| - expected.is_8_by_16 = true; |
| 879 | + let expected = Sprite { |
| 880 | + x: 1, |
| 881 | + y: 2, |
| 882 | + tile_idx: 16, |
| 883 | + palette_idx: PaletteIdx(7), |
| 884 | + flip_vertical: true, |
| 885 | + behind_background: true, |
| 886 | + is_8_by_16: true, |
| 887 | + ..Default::default() |
| 888 | + }; |
891 | 889 | assert_eq!(
|
892 | 890 | ppu.parse_sprite_from_oam_data(&[2, 0b0001_0000, 0b1010_0011, 1]),
|
893 | 891 | expected
|
|
0 commit comments