diff options
author | David Li <li.davidm96@gmail.com> | 2016-01-03 10:40:01 -0700 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2016-01-03 10:40:01 -0700 |
commit | 7ffec2b2ffe4246a6fe63f5b59ca399ae236236f (patch) | |
tree | 1f1e85fe55d9bc46921afeac8cf2aed20b524ef1 | |
parent | c845bc3877ce7722e4b161951af1e5c236cfc099 (diff) |
Fix write_byte, add basic tests
-rw-r--r-- | src/lib.rs | 23 | ||||
-rw-r--r-- | src/memory.rs | 7 |
2 files changed, 25 insertions, 5 deletions
@@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with rustv. If not, see <http://www.gnu.org/licenses/>. -#![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), } |