From 73e52f6aea27270c78dd0dbc35437de28a42b84b Mon Sep 17 00:00:00 2001 From: John Date: Tue, 9 Jul 2024 01:41:32 -0500 Subject: [PATCH] banked: Break address translation into its own function --- src/memory/banked.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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),