-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.cpp
106 lines (87 loc) · 3.47 KB
/
cache.cpp
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
#include "cache.hpp"
#include <cmath>
Cache::Cache(unsigned int cacheSize, unsigned int associativity, unsigned int blockSize) {
this->associativity = associativity;
this->blockSize = blockSize;
this->cacheSize = cacheSize;
// Calculate the number of bits for offset, set index, and tag
this->offsetBits = std::log2(blockSize);
int numSets = cacheSize / (blockSize * associativity);
this->setIndexBits = std::log2(numSets);
this->tagBits = 32 - (offsetBits + setIndexBits);
// Create masks for extracting set index and offset
unsigned int offsetMask = (1 << offsetBits) - 1;
unsigned int setIndexMask = ((1 << setIndexBits) - 1) << offsetBits;
this->cache = std::vector<CacheSet>(numSets, CacheSet(associativity));
}
unsigned int Cache::getIndex(unsigned int address) {
return (address & setIndexMask) >> offsetBits;
}
unsigned int Cache::getTag(unsigned int address) { return address >> (offsetBits + setIndexBits); }
unsigned int Cache::getIndexWithTag(unsigned int address) {
// this returns the index of the memory
return address / (blockSize);
}
unsigned int Cache::reverseGeneralAddress(unsigned int tag, unsigned int index) {
return ((tag * (blockSize * cacheSize)) + (index)) * blockSize;
}
bool Cache::checkCacheLine(unsigned int address) {
// get the respective address
// loop inside the cacheset list to find cacheline
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
return cache[index].checkCacheLine(tag);
}
bool Cache::invalidateCacheLine(unsigned int address) {
// performs a checkCacheLine
// performs a invalidateCacheLine
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
return cache[index].invalidateCacheLine(tag);
}
void Cache::addCacheLine(unsigned int address, State state) {
// performs a addCacheLine
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
// this will only give the tag, need to add in the address and then pad with the block size
cache[index].addCacheLine(tag, state);
}
bool Cache::readCacheLine(unsigned int address) {
// checkCacheLine
// readCacheLine
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
return cache[index].readCacheLine(tag);
}
bool Cache::updateCacheLine(unsigned int address, State state) {
// checkCacheLine
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
return cache[index].updateCacheLine(tag, state);
}
State Cache::getCacheLineState(unsigned int address) {
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
return cache[index].checkCacheLineState(tag);
}
bool Cache::setCacheLineState(unsigned int address, State state) {
unsigned int index = getIndex(address);
unsigned int tag = getTag(address);
return cache[index].setCacheLineState(tag, state);
}
State Cache::getLRUCacheLineState(unsigned int address) {
unsigned int index = getIndex(address);
if (!cache[index].isEmpty()) {
return cache[index].getFirst().getState();
}
return I;
}
bool Cache::checkCacheLineFull(unsigned int address) {
unsigned int index = getIndex(address);
return associativity == cache[index].size();
}
unsigned int Cache::getLRUCacheLineAddress(unsigned int address) {
unsigned int index = getIndex(address);
unsigned int tag = cache[index].getFirst().getTag();
return reverseGeneralAddress(tag, index);
}