-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullAss.h
124 lines (111 loc) · 4.21 KB
/
fullAss.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <vector>
#include <bitset>
#include <sstream>
#include <algorithm>
using namespace std;
// Define a struct to represent a cache block
struct Cache {
string tag; // The tag bits for the cache block
bool valid; // Indicates if the cache block contains valid data
int accessTime; // The time at which the block was last accessed
};
class FullyAssociative {
private:
int cacheSize; // The size of the cache in bytes
int blockSize; // The size of each cache block in bytes
int numBlocks; // The number of cache blocks
vector<Cache> cache; // The cache data structure
int numHits;
int numMisses;
bool fifo; // Flag to indicate if FIFO or LRU replacement policy should be used
public:
FullyAssociative(int cacheSize, int blockSize, bool fifo) {
// Initialize the cache size and block size
this->cacheSize = cacheSize;
this->blockSize = blockSize;
this -> numHits = 0;
this-> numMisses = 0;
this->fifo = fifo;
// Calculate the number of cache blocks based on the cache size and block size
this->numBlocks = cacheSize / blockSize;
// Initialize the cache with empty blocks
for (int i = 0; i < numBlocks; i++) {
Cache block;
block.valid = false;
cache.push_back(block);
}
}
// Accesses the cache at the given memory address
void accessCache(string address) {
// Convert the hexadecimal address to a binary string
stringstream ss;
ss << hex << address;
bitset<64> bits(stoull(ss.str(), nullptr, 16));
string binaryAddress = bits.to_string();
// Calculate the tag bits for the given memory address
string tagBits = binaryAddress.substr(0, binaryAddress.length() - log2(blockSize));
// Find the first cache block with invalid data
int invalidIndex = -1;
for (int i = 0; i < numBlocks; i++) {
if (!cache[i].valid) {
invalidIndex = i;
break;
}
}
for (int i = 0; i < numBlocks; i++) {
if (cache[i].valid && cache[i].tag == tagBits) {
// Cache hit
numHits++;
cache[i].accessTime = numMisses + numHits - 1;
// cout << "Cache hit!" << endl;
return;
}
}
// If there is an invalid block, use it to store the data
if (invalidIndex != -1) {
cache[invalidIndex].tag = tagBits;
cache[invalidIndex].valid = true;
cache[invalidIndex].accessTime = numMisses + numHits;
numMisses++;
// cout << "Cache miss, inserted data into block " << invalidIndex << endl;
return;
}
// If there is no invalid block, evict a block and replace it with the new data
if (fifo) {
// Find the block with the smallest access time (FIFO replacement policy)
int minIndex = 0;
for (int i = 1; i < numBlocks; i++) {
if (cache[i].accessTime < cache[minIndex].accessTime) {
minIndex = i;
}
}
cache[minIndex].tag = tagBits;
cache[minIndex].accessTime = numMisses + numHits;
numMisses++;
// cout << "Cache miss, evicted block " << minIndex << " and inserted data" << endl;
}
else{
int minIndex = 0;
for (int i = 1; i < numBlocks; i++) {
if (cache[i].accessTime < cache[minIndex].accessTime) {
minIndex = i;
}
}
cache[minIndex].tag = tagBits;
cache[minIndex].accessTime = numMisses + numHits;
numMisses++;
// cout << "Cache miss, evicted block " << minIndex << " and inserted data" << endl;
}
}
int getNumHits() {
return numHits;
}
// Returns the number of cache misses
int getNumMisses() {
return numMisses;
}
float getHitRate() {
return (float) numHits / (numHits + numMisses);
}
};