forked from jatindev2016/Cache-Coherence-Protocols
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L2.h
139 lines (130 loc) · 2.56 KB
/
L2.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<iostream>
using namespace std;
#define L2_ID 0
#define L2_SETS 1024
#define L2_WAYS 8
//states
#define PR 'R'
#define PW 'W'
#define I 'I'
#define S 'S'
#define M 'M'
#define E 'E'
#define O 'O'
//Core Counts
//#define CORE_COUNT 4
//Defining Queue Parameters
#define queue_size 60
#define res_queue_size 5
class L2
{
public:
unsigned long long tags[L2_SETS][L2_WAYS];
unsigned long long int timeStamp[L2_SETS][L2_WAYS];
unsigned long long hitCount,missCount;
char dir[L2_SETS][L2_WAYS];
int q_empty;
unsigned long long res_waiting_time,no_requests, Clk_count;
L2(){
hitCount = 0;
missCount = 0;
res_waiting_time=0;
q_empty=res_queue_size;
no_requests=0;
Clk_count=0;
reset();
}
void reset()
{
//clearing L2 cache
for(int i=0;i<L2_SETS;i++)
{
for(int j=0;j<L2_WAYS;j++)
{
tags[i][j]=0;
}
}
//clearing L2 Timestamp
for(int i=0;i<L2_SETS;i++)
{
for(int j=0;j<L2_WAYS;j++)
{
timeStamp[i][j]=0;
}
}
}
void print()
{
printf("--------------L2_DATA-----------\n");
for(int i=0;i<L2_SETS;i++)
{
for(int j=0;j<L2_WAYS;j++)
{
printf("%lld\t|",tags[i][j]);
}
printf("\n");
}
printf("--------------L2_Timestamp-----------\n");
for(int i=0;i<L2_SETS;i++)
{
for(int j=0;j<L2_WAYS;j++)
{
printf("%lld\t|",timeStamp[i][j]);
}
printf("\n");
}
}
char get_state(int set,unsigned long long tag,int *way)
{
for(int i=0;i<L2_WAYS;i++)
{
if((tags[set][i]==tag))
{
*way = i;
return dir[set][i];
}
}
return 'I';
}
int find_victim(int set)
{
int i,min=timeStamp[set][0],way=0;
for(i=1;i<L2_WAYS;i++)
{
if(timeStamp[set][i]<=min)
{
min=timeStamp[set][i];
way=i;
}
}
return way;
}
void update_timestamp(int set,int way,int opr, unsigned long long * timeStampCntr)
{
(*timeStampCntr)++;
//opr 0 mean invalidate
if(opr==0)
{ timeStamp[set][way] = 0;
dir[set][way]='I';}
else
//update timestamp
timeStamp[set][way] = *timeStampCntr;
}
int search(int set, unsigned long long tag)
{
for(int i=0;i<L2_WAYS;i++)
{
if((tags[set][i]==tag) && (timeStamp[set][i]!=0))
return i;
}
return -1;
}
int insert(int set,int tag, unsigned long long * timeStampCntr)
{
//way return the position to be evict, then insert data at way and update LRU table
int way =find_victim(set);
tags[set][way]=tag;
update_timestamp(set, way, 1, timeStampCntr);
return way;
}
};