Repository navigation
Expand file tree
/
Copy pathcache_simulator.cpp
More file actions
95 lines (78 loc) · 3.47 KB
/
Copy pathcache_simulator.cpp
File metadata and controls
95 lines (78 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
// Represents a single slot (line) in the cache
struct CacheLine {
bool valid = false; // true if this slot actually contains valid data
unsigned int tag = 0; // The unique identifier for the memory block
};
class CacheSimulator {
private:
unsigned int cacheSize; // Total size in bytes (e.g., 1024)
unsigned int blockSize; // Size of a single block in bytes (e.g., 64)
unsigned int numSlots; // Total rows in the cache
unsigned int indexBits; // Number of bits needed for the index
unsigned int offsetBits; // Number of bits needed for the offset
std::vector<CacheLine> cache;
// Performance metrics
int totalAccesses = 0;
int hits = 0;
int misses = 0;
public:
CacheSimulator(unsigned int cSize, unsigned int bSize) {
cacheSize = cSize;
blockSize = bSize;
numSlots = cacheSize / blockSize;
// Calculate how many bits we need to extract from the address
offsetBits = std::log2(blockSize);
indexBits = std::log2(numSlots);
// Initialize our cache array with empty slots
cache.resize(numSlots);
}
void accessMemory(unsigned int address) {
totalAccesses++;
// Shift away the offset bits, then mask to get the index
unsigned int index = (address >> offsetBits) & (numSlots - 1);
// Shift away both offset and index bits to get the unique tag
unsigned int tag = address >> (offsetBits + indexBits);
// Check if data is present and matches the tag
if (cache[index].valid && cache[index].tag == tag) {
hits++;
} else {
misses++;
// Cache Miss: Bring the data into the cache slot (overwrite what was there)
cache[index].valid = true;
cache[index].tag = tag;
}
}
void printStats() {
double hitRate = (totalAccesses > 0) ? (double)hits / totalAccesses * 100 : 0;
std::cout << "\n====================================\n";
std::cout << " CACHE SIMULATION RESULTS \n";
std::cout << "====================================\n";
std::cout << "Total Memory Accesses : " << totalAccesses << "\n";
std::cout << "Cache Hits : " << hits << "\n";
std::cout << "Cache Misses : " << misses << "\n";
std::cout << "Hit Rate : " << std::fixed << std::setprecision(2) << hitRate << "%\n";
std::cout << "====================================\n";
}
};
int main() {
// Setup an L1 Cache: 1024 Bytes total size, with 64-byte blocks (16 total slots)
CacheSimulator sim(1024, 64);
// Simulated memory trace stream representing a loop reading an array
std::vector<unsigned int> memoryTrace = {
0x1000, 0x1004, 0x1008, // Sequential reads (Should hit same cache block)
0x2000, // Maps to a different index (Miss)
0x1000, // Read original address again (Should be a Hit!)
0x1080, // Same tag, different index (Miss)
0x3000 // Conflict miss (evicts 0x2000 if it shares an index)
};
std::cout << "Running memory traces through the simulator...\n";
for (unsigned int addr : memoryTrace) {
sim.accessMemory(addr);
}
sim.printStats();
return 0;
}