-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesi.cc
158 lines (128 loc) · 3.55 KB
/
mesi.cc
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// mesi.cc
//
// Created by Adam Alford & Samuel Christie
//
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "mesi.h"
#include "directory.h"
using namespace std;
MesiCache::MesiCache(int s,int a,int b,int n, Directory *newDirectory ) : Cache(s,a,b,n)
{
dir = newDirectory;
}
/**you might add other parameters to Access()
since this function is an entry point
to the memory hierarchy (i.e. caches)**/
void MesiCache::Access(ulong addr,uchar op)
{
currentCycle++;/*per cache global counter to maintain LRU order
among cache ways, updated on every cache access*/
if(op == 'w') writes++;
else reads++;
cacheLine * line = findLine(addr);
if(line == NULL) {/*miss*/
if(op == 'w') writeMisses++;
else readMisses++;
if(op == 'w') {
// Send Upgr request to the directory
line = fillLine(addr);
dir->Read(addr, id);
dir->Upgr(addr, id);
line->setFlags(MODIFIED);
}
else {// read miss
// Send Read request to the directory
dir->Read(addr, id);
}
}
else {
updateLRU(line);
ulong flags = line->getFlags();
switch(flags) {
case EXCLUSIVE:
if(op == 'w')
line->setFlags(MODIFIED);
break;
case MODIFIED:
// 1., 2. M-->M on PrRd and PrWr
break;
case SHARED:
if(op == 'w') {
// post upgr to home
line->setFlags(MODIFIED);
dir->Upgr(addr, id);
}
else
// 4. S->S on PrRd
;
break;
default:
break;
}
}
}
/*allocate a new line*/
cacheLine *MesiCache::fillLine(ulong addr)
{
ulong tag;
cacheLine *victim = findLineToReplace(addr);
assert(victim != 0);
if(victim->getFlags() == MODIFIED) {
writeBack(addr);
}
dir->Disown(victim->addr, id);
tag = calcTag(addr);
victim->setTag(tag);
victim->addr = addr;
/**note that this cache line has been already
upgraded to MRU in the previous function (findLineToReplace)**/
return victim;
}
void MesiCache::Inv(ulong addr)
{
cacheLine *line = findLine(addr);
if(line != NULL) {
line->setFlags(INVALID);
invalidations++;
}
}
void MesiCache::WB_Int(ulong addr, int id)
{
// find if a line contains this block
cacheLine *line = findLine(addr);
if(line == NULL) {
cout << "attempting to have non-owner flush line " <<addr << "\n";
cout.flush();
exit(1);
}
line->setFlags(SHARED);
dir->Flush(addr, id); //flush to requestor
}
void MesiCache::ReplyD(ulong addr, bool shared)
{
// find if a line contains this block, or create it if it's a new line
cacheLine *line = findLine(addr);
if (line==NULL)
line = fillLine(addr);
if(shared) {
line->setFlags(SHARED);
}
else {
line->setFlags(EXCLUSIVE);
}
}
//receiving the data from another processor after requesting it
void MesiCache::Flush(ulong addr)
{
// find if a line contains this block
cacheLine *line = findLine(addr);
if (line == NULL)
line = fillLine(addr);
line->setFlags(SHARED);
//we received the data from another cache, so increment the cache-to-cache transfer counter
linesReceived++;
}