-
Notifications
You must be signed in to change notification settings - Fork 1
/
L1Cache.cpp
97 lines (84 loc) · 1.97 KB
/
L1Cache.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
#include "constants.h"
#include "L1Cache.h"
#include "L2Cache.h"
L1Cache::L1Cache(int num_sets, int associativity, int type, int id, replacement_policy policy){
this->associativity = associativity;
this->num_sets = num_sets;
this->type = type;
this->id = id;
this->clock=1;
this->policy = policy;
this->data.resize(num_sets, vector<ll>(associativity, INVALID));
}
void
L1Cache::set_child(L2Cache* child){
this->child = child;
}
void
L1Cache::find_in_cache(ull addr, int category, ull pc){
this->clock++;
ull idx = addr%num_sets;
for(int j=0;j<this->associativity;++j){
if(this->data[idx][j]==addr){ //Hit
switch(this->policy){
case BELADY:
break;
case LRU:
this->last_use[addr]=this->clock;
// cout << "L1 Hit " << addr << endl;
break;
}
return;
}
}
//Miss
this->child->find_in_cache(addr, category, pc);
// If cache has space left
for(int j=0;j<this->associativity;++j){
if(this->data[idx][j]==INVALID){
this->data[idx][j]=addr;
switch(this->policy){
case BELADY:
break;
case LRU:
this->last_use[addr]=this->clock;
// cout << "L1 Miss " << addr << endl;
break;
}
return;
}
}
// Need to invoke replacement policy
switch(this->policy){
case BELADY:
break;
case LRU:
int min_way;
long long min_use = LLONG_MAX;
for(int j=0;j<associativity;++j){
if(this->last_use[this->data[idx][j]] < min_use){
min_use = this->last_use[this->data[idx][j]];
min_way = j;
}
}
this->last_use.erase(this->data[idx][min_way]);
this->last_use[addr]=clock;
// cout << "L1 Miss " << addr << " Replacing " << this->data[idx][min_way] << endl;
this->data[idx][min_way]=addr;
break;
}
}
void
L1Cache::invalidate(ull addr){
ull idx = addr%num_sets;
// cout << "L1 invalidate " << addr << endl;
for(int j=0;j<this->associativity;++j){
if(this->data[idx][j]==addr){
last_use.erase(addr);
data[idx][j] = INVALID;
return;
}
}
}
L1Cache::~L1Cache(){
}