-
Notifications
You must be signed in to change notification settings - Fork 1
/
moesi.cpp
62 lines (56 loc) · 2.19 KB
/
moesi.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
#include "moesi.hpp"
#include "request.hpp"
#include "state.hpp"
#include <stdexcept>
using namespace std;
bool moesi_debug = false;
CacheResultType MOESIProtocol::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 || state == O) {
numShared++;
}
// P1 busRd M -> O P2 PrRd I -> S P2 read from P1 directly
// P1 PrWr O -> M P2-4 BusRdx S -> I
// actual processing
if (state == M || state == E || state == S || state == O) {
if (moesi_debug) cout << "M/E/S: load hit" << endl;
cache->updateCacheLine(address, state);
return CACHEHIT;
} else if (state == I) {
if (moesi_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 MOESIProtocol::onStore(int pid, unsigned int address, shared_ptr<Bus> bus,
shared_ptr<Cache> cache) {
State state = cache->getCacheLineState(address);
if (state == M) {
if (moesi_debug) cout << "M: store hit" << endl;
numPrivate++;
cache->updateCacheLine(address, M);
return CACHEHIT;
} else if (state == E) {
if (moesi_debug) cout << "E: store hit, change to M" << endl;
numPrivate++;
cache->updateCacheLine(address, M);
return CACHEHIT;
} else if (state == I || state == S || state == O) {
if (moesi_debug) cout << "S/O/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 MOESIProtocol::getNumShared() { return numShared; }
unsigned int MOESIProtocol::getNumPrivate() { return numPrivate; }