From 7ffec2b2ffe4246a6fe63f5b59ca399ae236236f Mon Sep 17 00:00:00 2001 From: David Li Date: Sun, 3 Jan 2016 10:40:01 -0700 Subject: Fix write_byte, add basic tests --- src/lib.rs | 23 ++++++++++++++++++++++- src/memory.rs | 7 +++---- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index f5bf110..ebbc92f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with rustv. If not, see . -#![feature(associated_consts)] +#![feature(step_by)] pub mod isa; pub mod binary; pub mod memory; @@ -57,4 +57,25 @@ mod tests { assert_eq!(dm_cache_doubleword.parse_address(0xFFFFFFFD), (0x7FFFFFF, 3, 5)); } + + #[test] + fn memory_rw() { + use memory::*; + let size = 0xFF; + let mut memory = Memory::new(size); + + assert_eq!(memory.write_word(0, 0xF0), + Err(MemoryError::InvalidAddress)); + assert_eq!(memory.write_byte(0, 0xF0), + Err(MemoryError::InvalidAddress)); + assert_eq!(memory.write_byte(1, 0xF0), + Err(MemoryError::InvalidAddress)); + assert_eq!(memory.write_byte(2, 0xF0), + Err(MemoryError::InvalidAddress)); + + for address in (4..size).step_by(4) { + assert_eq!(memory.write_word(address, 0xF0), Ok(())); + assert_eq!(memory.read_word(address), Ok(0xF0)); + } + } } diff --git a/src/memory.rs b/src/memory.rs index 22d9bce..9b4ab0a 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -20,7 +20,7 @@ use std::cell::RefCell; use isa::{self, Instruction}; use binary::{Binary}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub enum MemoryError { InvalidAddress, CacheMiss { @@ -65,7 +65,6 @@ pub trait MemoryInterface { } fn write_byte(&mut self, address: isa::Address, value: isa::Byte) -> Result<()> { - let address = address / 4; let offset = address % 4; let result = self.read_word(address); @@ -73,14 +72,14 @@ pub trait MemoryInterface { match result { Ok(word) => { - match offset { + let value = match offset { 0 => (word & !(0xFF)) | value, 1 => (word & !(0xFF00)) | (value << 8), 2 => (word & !(0xFF0000)) | (value << 16), 3 => (word & !(0xFF000000)) | (value << 24), _ => panic!(""), }; - Ok(()) + self.write_word(address, value) }, Err(e) => Err(e), } -- cgit v1.2.3