-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.cpp
125 lines (109 loc) · 5.59 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <math.h>
#include <vector>
#include <iostream>
#include "Cache.h"
void Cache::setParameters(
int addressLength,
int cacheBlockSize,
int cacheSize,
int setAssociativity){
numCacheBlocks_ = cacheSize/cacheBlockSize;
numCacheSets_ = numCacheBlocks_/setAssociativity;
offsetWidth_ = (int) log2(cacheBlockSize);
indexWidth_ = (int) log2(numCacheSets_);
blockStructArray_.resize(numCacheSets_);
for (int i = 0; i < numCacheSets_; ++i){
blockStructArray_[i].resize(setAssociativity);
}
}
void Cache::printCfg(){
std::cout << " numCacheBlocks_: " << numCacheBlocks_ << std::endl;
std::cout << " numCacheSets_ : " << numCacheSets_ << std::endl;
std::cout << std::endl;
}
int Cache::write(int decodedTag, int decodedIndex, int decodedOffset, int setAssociativity, int addressCounter){
writeTotal++;
bool writeHit = false;
int rep_candidate=0; // Default
for(int j = 0; j < setAssociativity; j++){
//if(blockStructArray_[decodedIndex][j].tagField == decodedTag && blockStructArray_[decodedIndex][j].validBit == 0){
if(blockStructArray_[decodedIndex][j].tagField == decodedTag){
std::cout << "(Cache::write) WRITE HIT" << std::endl;
writeHits++;
writeHit = true;
blockStructArray_[decodedIndex][j].lruField=addressCounter;
blockStructArray_[decodedIndex][j].validBit=1;
blockStructArray_[decodedIndex][j].dirtyBit=1;
return 1; // Hit
}
}
if(writeHit == false){
std:: cout << "(Cache::write) WRITE MISS" << std::endl;
writeMisses++;
// Find eviction candidate
int lruVal = blockStructArray_[decodedIndex][0].lruField;
for(int j=0; j<setAssociativity; j++){
if(blockStructArray_[decodedIndex][j].lruField < lruVal){
rep_candidate = j;
}
}
// Check dirty bit, if dirty writeback++, if not, simple overwrite
for(int j=0; j<=setAssociativity; j++){
if(blockStructArray_[rep_candidate][j].dirtyBit == 1){
writeBacks++;
break;
}
// for a read miss, the cpu tag field, and transaction counts are updated, block is considererd not dirty and yes valid
blockStructArray_[decodedIndex][rep_candidate].tagField=decodedTag;
blockStructArray_[decodedIndex][rep_candidate].validBit=1;
blockStructArray_[decodedIndex][rep_candidate].dirtyBit=0;
blockStructArray_[decodedIndex][rep_candidate].lruField=addressCounter;
}
}
return 0; // Miss
}
int Cache::read(int decodedTag, int decodedIndex, int decodedOffset, int setAssociativity, int addressCounter, int numCacheSets){
readTotal++;
bool readHit;
int rep_candidate=0;
// loop across ways/blocks, for index
for(int j = 0; j < setAssociativity; j++){
// if there is a index + tag match, in way j, increment readHits counter, set readHit to true, ubdate lru field
if(blockStructArray_[decodedIndex][j].tagField == decodedTag){
std::cout << "(Cache::read) READ HIT" << std::endl;
readHits++;
readHit=true;
blockStructArray_[decodedIndex][j].lruField = addressCounter;
return 1; // Hit
}
}
// If no read hit, find replacement victim
if(readHit == false){
std:: cout << "(Cache::read) READ MISS" << std::endl;
readMisses++;
int lruTag = 0;
// Find eviction candidate
for(int j=0; j<setAssociativity; j++){
for(int i=0; i < numCacheSets; i++){
//std::cout << "readCacheModify[i][j].cacheTagField: " << readCacheModify[i][j].lruField << std::endl;
if(blockStructArray_[decodedIndex][j].lruField > lruTag){
lruTag = blockStructArray_[decodedIndex][j].lruField;
rep_candidate = i;
}
}
}
// Check dirty bit, if dirty writeback, if not OK to overwrite
for(int j=0; j<=setAssociativity; j++){
if(blockStructArray_[rep_candidate][j].dirtyBit == 1){
writeBacks++;
break;
}
// for a read miss, the cpu tag field, and transaction counts are updated, block is considererd not dirty and yes valid
blockStructArray_[decodedIndex][rep_candidate].tagField=decodedTag;
blockStructArray_[decodedIndex][rep_candidate].validBit=1;
blockStructArray_[decodedIndex][rep_candidate].dirtyBit=0;
blockStructArray_[decodedIndex][rep_candidate].lruField=addressCounter;
}
}
return 0; // Miss
}