diff --git a/src/memory/banked.rs b/src/memory/banked.rs index a29614b..8239433 100644 --- a/src/memory/banked.rs +++ b/src/memory/banked.rs @@ -91,6 +91,12 @@ impl UpperBanked usize { self.selected } + + /// Translates an address into a (bank, address) pair + pub fn translate(&self, addr: usize) -> Option<(usize, usize)> { + let addr = addr.checked_sub(BASE).filter(|v| *v < LEN * 2)?; + Some((addr / LEN, addr % LEN)) + } } impl Default @@ -107,8 +113,7 @@ impl BusIO { fn read(&self, addr: usize) -> Option { // Pray this gets optimized - let addr = addr.checked_sub(BASE).filter(|v| *v < LEN * 2)?; - let (bank, addr) = (addr / LEN, addr % LEN); + let (bank, addr) = self.translate(addr)?; match bank { 0 => self.mem[0].read(addr), _ => self.mem[self.selected].read(addr), @@ -117,8 +122,7 @@ impl BusIO fn write(&mut self, addr: usize, data: u8) -> Option<()> { // Pray this gets optimized - let addr = addr.checked_sub(BASE).filter(|v| *v < LEN * 2)?; - let (bank, addr) = (addr / LEN, addr % LEN); + let (bank, addr) = self.translate(addr)?; match bank { 0 => self.mem[0].write(addr, data), _ => self.mem[self.selected].write(addr, data),