From 885bfea43409a3d5438bbde59f8d40014fd7cbbf Mon Sep 17 00:00:00 2001 From: Franco Colmenarez Date: Wed, 12 Oct 2022 08:07:25 -0500 Subject: [PATCH] Small PPU refactor for HDMA addresses --- src/ppu.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ppu.rs b/src/ppu.rs index 8677b33..d8b5a32 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -390,17 +390,14 @@ impl PPU { } pub fn get_register(&self, address: u16) -> u8 { - if address >= 0xFF51 && address <= 0xFF55 { - if address == HDMA1_ADDRESS { - return self.hdma_source.to_be_bytes()[0]; - } else if address == HDMA2_ADDRESS { - return self.hdma_source.to_be_bytes()[1]; - } else if address == HDMA3_ADDRESS { - return self.hdma_destination.to_be_bytes()[0]; - } else if address == HDMA4_ADDRESS { - return self.hdma_destination.to_be_bytes()[1]; - } else if address == HDMA5_ADDRESS { - return self.hdma_start; + if address >= HDMA1_ADDRESS && address <= HDMA5_ADDRESS { + return match address { + HDMA1_ADDRESS => self.hdma_source.to_be_bytes()[0], + HDMA2_ADDRESS => self.hdma_source.to_be_bytes()[1], + HDMA3_ADDRESS => self.hdma_destination.to_be_bytes()[0], + HDMA4_ADDRESS => self.hdma_destination.to_be_bytes()[1], + HDMA5_ADDRESS => self.hdma_start, + _ => 0x00, } } else if address >= 0xFF68 && address <= 0xFF6B { return self.cram_registers[(address as usize) - 0xFF68]; @@ -415,7 +412,15 @@ impl PPU { } pub fn set_register(&mut self, address: u16, data: u8) { - if address >= 0xFF51 && address <= 0xFF55 { + if address >= HDMA1_ADDRESS && address <= HDMA5_ADDRESS { + match address { + HDMA1_ADDRESS => self.hdma_source = (self.hdma_source & 0xFF) | ((data as u16) << 8), + HDMA2_ADDRESS => self.hdma_source = (self.hdma_source & 0xFF00) | (data as u16), + HDMA3_ADDRESS => self.hdma_destination = (self.hdma_destination & 0xFF) | ((data as u16) << 8), + HDMA4_ADDRESS => self.hdma_destination = (self.hdma_destination & 0xFF00) | (data as u16), + HDMA5_ADDRESS => self.hdma_start = data, + _ => (), + }; if address == HDMA1_ADDRESS { self.hdma_source = (self.hdma_source & 0xFF) | ((data as u16) << 8); } else if address == HDMA2_ADDRESS {