diff options
author | David Li <li.davidm96@gmail.com> | 2015-12-27 10:01:43 -0700 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2015-12-27 10:01:43 -0700 |
commit | 8102cfe7d5cdfd0f2001d257af11686809c22bb9 (patch) | |
tree | 9c4976f9266a14446b4902044057b380f2d604d9 /src | |
parent | 6fbeb4e5ec5f12916853f9a168d6a730992dd63e (diff) |
Fix and test cache address parsing
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 14 | ||||
-rw-r--r-- | src/memory.rs | 24 |
2 files changed, 26 insertions, 12 deletions
@@ -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<CacheBlock>; // 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<CacheSet>, } @@ -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) { } } |