From 8102cfe7d5cdfd0f2001d257af11686809c22bb9 Mon Sep 17 00:00:00 2001 From: David Li Date: Sun, 27 Dec 2015 10:01:43 -0700 Subject: Fix and test cache address parsing --- src/lib.rs | 14 ++++++++++++++ src/memory.rs | 24 ++++++++++++------------ 2 files changed, 26 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 7701aa3..3e2d0e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,3 +15,17 @@ fn it_works() { Err(err) => println!("Error: {:?}", err), } } + +#[test] +fn cache_address_parsing() { + let dm_cache_word = memory::Cache::new(4, 1, 1); + let dm_cache_doubleword = memory::Cache::new(4, 1, 2); + let fa_cache_doubleword = memory::Cache::new(1, 4, 2); + + assert_eq!(dm_cache_word.parse_address(0xFFFFFFFD), + (0xFFFFFFF, 3, 1)); + assert_eq!(dm_cache_doubleword.parse_address(0xFFFFFFFD), + (0x7FFFFFF, 3, 5)); + assert_eq!(fa_cache_doubleword.parse_address(0xFFFFFFFD), + (0x1FFFFFFF, 0, 5)); +} diff --git a/src/memory.rs b/src/memory.rs index e65eb10..76a942f 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -52,9 +52,9 @@ type CacheSet = Vec; // TODO: use hashtable for a way? // TODO: hashtable-based FA cache? pub struct Cache { - num_sets: usize, - num_ways: usize, - block_words: usize, + num_sets: u32, + num_ways: u32, + block_words: u32, cache: Vec, } @@ -102,39 +102,39 @@ impl MemoryInterface for Memory { } impl Cache { - pub fn new(sets: usize, ways: usize, block_words: usize) -> Cache { + pub fn new(sets: u32, ways: u32, block_words: u32) -> Cache { let set = vec![CacheBlock { valid: false, tag: 0, - contents: vec![0; block_words], + contents: vec![0; block_words as usize], fetch_request: None, - }; ways]; + }; ways as usize]; Cache { num_sets: sets, num_ways: ways, block_words: block_words, - cache: vec![set; sets], + cache: vec![set; sets as usize], } } - fn parse_address(&self, address: isa::Address) -> (u32, u32, u32) { + pub fn parse_address(&self, address: isa::Address) -> (u32, u32, u32) { // TODO: use constant in ISA module for word->byte conversion let offset_mask = (self.block_words * 4 - 1) as u32; let offset = address & offset_mask; let index_mask = (self.num_sets - 1) as u32; - let index_shift = 32 - (self.block_words * 4).leading_zeros(); + let index_shift = 32 - (self.block_words * 4).leading_zeros() - 1; let index = (address >> index_shift) & index_mask; - let tag_shift = index_shift + (32 - self.num_sets.leading_zeros()); + let tag_shift = index_shift + (32 - self.num_sets.leading_zeros()) - 1; let tag = address >> tag_shift; (tag, index, offset) } - fn prefetch(&mut self, address: isa::Address) { + pub fn prefetch(&mut self, address: isa::Address) { } - fn invalidate(&mut self, address: isa::Address) { + pub fn invalidate(&mut self, address: isa::Address) { } } -- cgit v1.2.3