diff --git a/csim.c b/csim.c index fdbfdb7..8ecc779 100644 --- a/csim.c +++ b/csim.c @@ -52,6 +52,7 @@ void free_cache(linked_line** cache, long num_sets); void free_set(linked_line* line); void print_cache(linked_line** cache, long num_sets); void print_set(linked_line* line); +char cache_access(linked_line** cache, long address, int set_bits, int block_bits, long num_lines); linked_line* find_line(linked_line* set_head, long tag); void increment_result_counters(char result, results* score); @@ -131,6 +132,12 @@ int main(int argc, char* argv[]) printf("%c %ld\n", op, address); print_cache(cache, set_indices); + char r = cache_access(cache, address, set_index_bits, block_bits, num_lines); + increment_result_counters(r, &score); + if (op == 'M') { //Access again if modifying + r = cache_access(cache, address, set_index_bits, block_bits, num_lines); + increment_result_counters(r, &score); + } } printSummary(score.hits, score.misses, score.evictions); @@ -215,6 +222,38 @@ void print_set(linked_line* line) } } +char cache_access(linked_line** cache, long address, int set_bits, int block_bits, long num_lines) +{ + char out; + long tag = address >> set_bits >> block_bits; + long set = (address >> block_bits) & ((1 << set_bits) - 1); + linked_line* set_head = cache[set]; + linked_line* line = find_line(set_head, tag); + + if (!line->validity) out = 'M'; + else if (line->tag == tag) out = 'H'; + else out = 'E'; + + if (line != set_head) { + line->newer = NULL; + set_head->newer = line; + line->older = set_head; + if (line->older) { + line->older->newer = line->newer; + if (line->older->older == line) + line->older->older = NULL; + } + if (line->newer) + line->newer->older = line->older; + cache[set] = line; + } + + line->tag = tag; + line->validity = 1; + + return out; +} + /** @returns the first line with a matching tag, the first invalid line, or the first line without an older (i.e. the end of the set