-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmesi.cpp
64 lines (57 loc) · 2.2 KB
/
mesi.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
#include "mesi.hpp"
#include "request.hpp"
#include "state.hpp"
#include <stdexcept>
using namespace std;
bool mesi_debug = false;
CacheResultType MESIProtocol::onLoad(int pid, unsigned int address, shared_ptr<Bus> bus,
shared_ptr<Cache> cache) {
State state = cache->getCacheLineState(address);
// handle counters
if (state == M || state == E) {
numPrivate++;
} else if (state == S) {
numShared++;
}
// actual processing
if (state == M || state == E || state == S) {
if (mesi_debug) cout << "M/E/S: load hit" << endl;
cache->updateCacheLine(address, state);
return CACHEHIT;
} else if (state == I) {
if (mesi_debug) cout << "I: load miss, pushing BusRd" << endl;
shared_ptr<Request> busRdRequest = make_shared<Request>(pid, BusRd, address);
bus->pushRequestToBus(busRdRequest);
return CACHEMISS;
} else {
throw runtime_error("invalid state");
}
}
CacheResultType MESIProtocol::onStore(int pid, unsigned int address, shared_ptr<Bus> bus,
shared_ptr<Cache> cache) {
State state = cache->getCacheLineState(address);
if (state == M) {
// cache hit
if (mesi_debug) cout << "M: store hit" << endl;
numPrivate++;
cache->updateCacheLine(address, M); // no change in state, but set most recently used
return CACHEHIT;
} else if (state == E) {
// change cachestate to M and cache hit
if (mesi_debug) cout << "E: store hit, change to M" << endl;
numPrivate++;
// change the state of the cache here
cache->updateCacheLine(address, M);
return CACHEHIT;
} else if (state == I || state == S) {
if (mesi_debug) cout << "S/I: store miss, pushing BusRdX" << endl;
shared_ptr<Request> busRdXRequest = make_shared<Request>(pid, BusRdX, address);
bus->pushRequestToBus(busRdXRequest);
return CACHEMISS;
} else {
// invalid current state
throw runtime_error("invalid state");
}
}
unsigned int MESIProtocol::getNumShared() { return numShared; }
unsigned int MESIProtocol::getNumPrivate() { return numPrivate; }