get_bg_modes function

This commit is contained in:
2023-07-02 20:24:58 -05:00
parent 9d99d2d194
commit c8db7d6e68
2 changed files with 114 additions and 12 deletions

View File

@@ -111,21 +111,13 @@ mod bus_tests {
assert_eq!(Bus::map_address(0xBF21FF), MemoryMap::PPU);
assert_eq!(Bus::map_address(0x004200), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x0042FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x00420F), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x3F4200), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x3F42FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x3F420F), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x804200), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x8042FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x80420F), MemoryMap::CPU);
assert_eq!(Bus::map_address(0xBF4200), MemoryMap::CPU);
assert_eq!(Bus::map_address(0xBF42FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x004300), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x0043FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x3F4300), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x3F43FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x804300), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x8043FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0xBF4300), MemoryMap::CPU);
assert_eq!(Bus::map_address(0xBF43FF), MemoryMap::CPU);
assert_eq!(Bus::map_address(0xBF420F), MemoryMap::CPU);
assert_eq!(Bus::map_address(0x004016), MemoryMap::Joypad);
assert_eq!(Bus::map_address(0x004017), MemoryMap::Joypad);

View File

@@ -103,6 +103,16 @@ impl BgSize {
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum BgMode{
Color2BPP,
Color4BPP,
Color8BPP,
OffsetPerTile,
ExtBg,
}
#[derive(Debug, Copy, Clone)]
pub enum Background {
Bg1,
@@ -172,6 +182,61 @@ impl PPURegisters {
}
}
pub fn get_bg_modes(&self) -> (Option<BgMode>, Option<BgMode>, Option<BgMode>, Option<BgMode>) {
let byte = self.read(BGMODE);
match byte & 0b111 {
0 => (
Some(BgMode::Color2BPP),
Some(BgMode::Color2BPP),
Some(BgMode::Color2BPP),
Some(BgMode::Color2BPP),
),
1 => (
Some(BgMode::Color4BPP),
Some(BgMode::Color4BPP),
Some(BgMode::Color2BPP),
None,
),
2 => (
Some(BgMode::Color4BPP),
Some(BgMode::Color4BPP),
Some(BgMode::OffsetPerTile),
None,
),
3 => (
Some(BgMode::Color8BPP),
Some(BgMode::Color4BPP),
None,
None,
),
4 => (
Some(BgMode::Color8BPP),
Some(BgMode::Color2BPP),
Some(BgMode::OffsetPerTile),
None,
),
5 => (
Some(BgMode::Color4BPP),
Some(BgMode::Color2BPP),
None,
None,
),
6 => (
Some(BgMode::Color4BPP),
None,
Some(BgMode::OffsetPerTile),
None,
),
7 => (
Some(BgMode::Color8BPP),
Some(BgMode::ExtBg),
None,
None,
),
_ => unreachable!(),
}
}
pub fn is_vblanking(&self) -> bool {
if self.h_count >= 1 && self.h_count <= 224 {
return false
@@ -201,6 +266,51 @@ mod ppu_registers_test {
assert_eq!(registers.get_bg_size(Background::Bg1), BgSize::T32x64);
}
#[test]
fn test_get_bg_modes() {
let mut registers = PPURegisters::new();
registers.write(BGMODE, 0);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color2BPP), Some(BgMode::Color2BPP), Some(BgMode::Color2BPP), Some(BgMode::Color2BPP)),
);
registers.write(BGMODE, 1);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color4BPP), Some(BgMode::Color4BPP), Some(BgMode::Color2BPP), None),
);
registers.write(BGMODE, 2);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color4BPP), Some(BgMode::Color4BPP), Some(BgMode::OffsetPerTile), None),
);
registers.write(BGMODE, 3);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color8BPP), Some(BgMode::Color4BPP), None, None),
);
registers.write(BGMODE, 4);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color8BPP), Some(BgMode::Color2BPP), Some(BgMode::OffsetPerTile), None),
);
registers.write(BGMODE, 5);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color4BPP), Some(BgMode::Color2BPP), None, None),
);
registers.write(BGMODE, 6);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color4BPP), None, Some(BgMode::OffsetPerTile), None),
);
registers.write(BGMODE, 7);
assert_eq!(
registers.get_bg_modes(),
(Some(BgMode::Color8BPP), Some(BgMode::ExtBg), None, None),
);
}
#[test]
fn test_is_vblanking() {
let mut registers = PPURegisters::new();