Reputation: 1
So basically this is a set associative cache simulation. I need to get it working for 2,4,16 and 32 way associative caches. The LRU is a series of deques within a vector, and the cache itself is a vector of longlongs where the least significant bit is the valid bit. I keep getting a few thousand or so less hits than I should be. Testing on 1122102 instructions with target addresses. Just want to know if there's any errors I'm overlooking here.
bool setA(string behavior, int asc, unsigned long long target, vector<unsigned long long>& cache, vector<deque<int>>& lru){
bool hit=false;
int kb=16;
const int line_size_bytes = 32;
const int num_cache_lines = (kb * 1024) / line_size_bytes;
const int num_sets = num_cache_lines / asc;
unsigned long long index = (target / line_size_bytes) % num_sets;
unsigned long long tag = target / (line_size_bytes * num_sets);
int start_index = index * asc;
int end_index = start_index + asc;
auto it = find_if(cache.begin() + start_index, cache.begin() + end_index, [&tag](unsigned long long line) { return line & 1 && (line >> 1) == tag; });
if (it != cache.begin() + end_index) {
// Cache hit
hit = true;
int position = distance(cache.begin()+start_index, it);
// Update LRU: Move the cache line to the end of the deque
auto lru_it = find(lru[index].begin(), lru[index].end(), position);
lru[index].erase(lru_it);
lru[index].push_back(position);
} else {
// Cache miss
// Find the least recently used cache line in the set
int lru_position = lru[index].front();
lru[index].pop_front();
// Replace the least recently used cache line with the new tag and valid bit
cache[start_index + lru_position] = (tag << 1) | 1;
// update newly used line with used line
lru[index].push_back(lru_position);
}
return hit;
}
Upvotes: 0
Views: 154