diff --git a/backend-simulators/pim/pim-simulator/src/bin/pim-simulator/main.rs b/backend-simulators/pim/pim-simulator/src/bin/pim-simulator/main.rs index bf50c95..04504e2 100644 --- a/backend-simulators/pim/pim-simulator/src/bin/pim-simulator/main.rs +++ b/backend-simulators/pim/pim-simulator/src/bin/pim-simulator/main.rs @@ -43,7 +43,7 @@ struct Args { /// Comma separated list of (address,size) for memory output dump #[arg(short, long, value_delimiter = ',', num_args = 1.., value_name = "ADDR,SIZE")] - dump: Vec, + dump: Vec, } fn main() -> Result<()> { @@ -168,7 +168,7 @@ fn get_crossbars(config: &Value, args: &Args) -> anyhow::Result Result<()> { - let dumps: Vec<(i32, i32)> = args + let dumps: Vec<(usize, usize)> = args .dump .chunks_exact(2) .map(|chunk| (chunk[0], chunk[1])) diff --git a/backend-simulators/pim/pim-simulator/src/lib/cpu/mod.rs b/backend-simulators/pim/pim-simulator/src/lib/cpu/mod.rs index f2c6674..30cdd09 100644 --- a/backend-simulators/pim/pim-simulator/src/lib/cpu/mod.rs +++ b/backend-simulators/pim/pim-simulator/src/lib/cpu/mod.rs @@ -91,30 +91,26 @@ impl<'a> Core<'a> { self.memory.execute_load() } - pub fn execute_store(&mut self, address: impl TryToUsize, element: &[T]) -> Result<()> + pub fn execute_store(&mut self, address: impl AddressArg, element: &[T]) -> Result<()> where T: MemoryStorable, { - let address = address.try_into().context("address can not be negative")?; + let address = address.to_address_usize()?; self.memory.execute_store(address, element) } pub fn reserve_load( &mut self, - address: impl TryToUsize, + address: impl AddressArg, size: impl TryToUsize, ) -> Result<&mut CoreMemory> { - let address = address.try_into().context("address can not be negative")?; + let address = address.to_address_usize()?; let size = size.try_into().context("size can not be negative")?; self.memory.reserve_load(address, size) } pub fn set_register(&mut self, index: impl TryToUsize, value: i32) { let index = index.try_into().expect("index can not be negative"); - assert!( - value >= 0, - "Register cannot be negative if happens remove this and go check where it's used as usize" - ); self.registers[index] = value; } @@ -123,11 +119,11 @@ impl<'a> Core<'a> { self.registers[index] } - pub fn load(&mut self, address: impl TryToUsize, size: impl TryToUsize) -> Result> + pub fn load(&mut self, address: impl AddressArg, size: impl TryToUsize) -> Result> where T: MemoryStorable, { - let address = address.try_into().context("address can not be negative")?; + let address = address.to_address_usize()?; let size = size.try_into().context("size can not be negative")?; self.memory.load(address, size) } @@ -141,8 +137,8 @@ impl<'a> Core<'a> { (memory, crossbars) } - pub fn memset(&mut self, address: impl TryToUsize, size: impl TryToUsize, val: u8) -> Result<()> { - let address = address.try_into().context("address can not be negative")?; + pub fn memset(&mut self, address: impl AddressArg, size: impl TryToUsize, val: u8) -> Result<()> { + let address = address.to_address_usize()?; let size = size.try_into().context("size can not be negative")?; self.memory.memset(address, size, val) } diff --git a/backend-simulators/pim/pim-simulator/src/lib/utility.rs b/backend-simulators/pim/pim-simulator/src/lib/utility.rs index c12d4e9..96e354d 100644 --- a/backend-simulators/pim/pim-simulator/src/lib/utility.rs +++ b/backend-simulators/pim/pim-simulator/src/lib/utility.rs @@ -1,7 +1,44 @@ use std::{fmt::Debug, mem::transmute}; -use crate::memory_manager::type_traits::TryToUsize; +pub trait AddressArg { + fn to_address_usize(self) -> Result; +} + +impl AddressArg for usize { + fn to_address_usize(self) -> Result { + Ok(self) + } +} + +impl AddressArg for u32 { + fn to_address_usize(self) -> Result { + Ok(self as usize) + } +} + +impl AddressArg for u64 { + fn to_address_usize(self) -> Result { + usize::try_from(self).context("address does not fit in usize") + } +} + +impl AddressArg for i32 { + fn to_address_usize(self) -> Result { + Ok(self as u32 as usize) + } +} + +impl AddressArg for i64 { + fn to_address_usize(self) -> Result { + usize::try_from(self).context("address can not be negative") + } +} + + +fn address_to_usize(address: i32) -> usize { + address as u32 as usize +} fn add_offset_impl(address: usize, offset_select : i32, offset_value : i32, id:i32) -> usize{ assert!(offset_select == 1 || offset_select == 2 || offset_select == 4 || offset_value == 0, "offset_select not a bit field"); @@ -14,21 +51,21 @@ fn add_offset_impl(address: usize, offset_select : i32, offset_value : i32, id:i } -pub fn add_offset_rd(address: impl TryToUsize, offset_select : i32, offset_value : i32) -> usize +pub fn add_offset_rd(address: i32, offset_select : i32, offset_value : i32) -> usize { - let address = address.try_into().expect("address can not be negative"); + let address = address_to_usize(address); add_offset_impl(address, offset_select, offset_value, 4) } -pub fn add_offset_r1(address: impl TryToUsize, offset_select : i32, offset_value : i32) -> usize +pub fn add_offset_r1(address: i32, offset_select : i32, offset_value : i32) -> usize { - let address = address.try_into().expect("address can not be negative"); + let address = address_to_usize(address); add_offset_impl(address, offset_select, offset_value, 1) } -pub fn add_offset_r2(address: impl TryToUsize, offset_select : i32, offset_value : i32) -> usize +pub fn add_offset_r2(address: i32, offset_select : i32, offset_value : i32) -> usize { - let address = address.try_into().expect("address can not be negative"); + let address = address_to_usize(address); add_offset_impl(address, offset_select, offset_value, 2) }