Skip to content

Commit 827d68b

Browse files
committed
Fix circular dependency
1 parent 01b9c3a commit 827d68b

12 files changed

+123
-88
lines changed

bus.cpp

-36
This file was deleted.

bus.hpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#pragma once
22

33
#include "processor.hpp"
4-
#include "request.hpp"
54
#include <memory>
6-
#include <queue>
5+
#include <vector>
76

87
using namespace std;
98

109
class Bus {
1110
private:
1211
vector<shared_ptr<Processor>> processors;
13-
shared_ptr<Request> currReq;
14-
vector<Request> memRequests;
15-
queue<Request> busQueue;
1612

1713
public:
1814
virtual void attachProcessor(shared_ptr<Processor> proc) = 0;
1915
virtual void issueInvalidation() = 0;
2016
virtual void executeCycle() = 0;
21-
virtual ~Bus(){};
2217
};

bus_impl.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "bus_impl.hpp"
2+
#include "processor.hpp"
3+
#include "request.cpp"
4+
#include <memory>
5+
#include <queue>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
void BusImpl::attachProcessor(shared_ptr<Processor> proc) { this->processors.push_back(proc); }
11+
12+
void BusImpl::issueInvalidation() {
13+
for (auto processor : this->processors) {
14+
processor->invalidateCache();
15+
}
16+
}
17+
18+
void BusImpl::executeCycle() {
19+
if (currReq == nullptr) {
20+
currReq = busQueue.front();
21+
busQueue.pop();
22+
}
23+
currReq->decrement();
24+
if (currReq->isDone()) {
25+
/* current bus transaction is done */
26+
}
27+
/* handle memory */
28+
}

bus_impl.hpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "bus.hpp"
4+
#include "processor.hpp"
5+
#include "request.hpp"
6+
#include <memory>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
class BusImpl : public Bus {
12+
private:
13+
vector<shared_ptr<Processor>> processors;
14+
shared_ptr<Request> currReq = nullptr;
15+
vector<Request> memRequests;
16+
queue<shared_ptr<Request>> busQueue;
17+
18+
public:
19+
void attachProcessor(shared_ptr<Processor> proc);
20+
void issueInvalidation();
21+
void executeCycle();
22+
};

main.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include "bus.cpp"
1+
#include "bus.hpp"
2+
#include "bus_impl.hpp"
23
#include "memory.cpp"
3-
#include "processor.cpp"
4+
#include "processor.hpp"
45
#include <iostream>
56
#include <memory>
67
#include <string>
@@ -45,9 +46,10 @@ int main(int argc, char *argv[]) {
4546
// Your logic here for implementing the cache coherence protocols
4647

4748
int numProcessors;
48-
// TODO: Initialize a shared bus
49-
// shared_ptr<Bus> bus = make_shared();
49+
shared_ptr<Bus> bus = make_shared<BusImpl>();
5050
// TODO: Initialize processors with bus
51+
for (int i = 0; i < numProcessors; i++) {
52+
}
5153

5254
unsigned int clock = 0;
5355
/*

mesi.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "mesi.hpp"
22

3+
using namespace std;
4+
35
void MESIProtocol::onLoad(unsigned int address, shared_ptr<Bus> bus, shared_ptr<Cache> cache) {
46
// MESI-specific logic
57
/*

mesi.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "protocol.hpp"
33
#include <memory>
44

5+
using namespace std;
6+
57
class MESIProtocol : public Protocol {
68
public:
79
void onLoad(unsigned int address, shared_ptr<Bus> bus, shared_ptr<Cache> cache) override;

processor.hpp

+3-29
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
11
#pragma once
22

3-
#include "cache.hpp"
4-
#include "protocol.hpp"
5-
#include "request.hpp"
6-
#include <memory>
7-
#include <vector>
8-
9-
using namespace std;
10-
11-
enum ProcessorState { FREE, LOAD, STORE, NON_MEMORY, MEM_ACCESS };
12-
133
class Processor {
14-
private:
15-
shared_ptr<Protocol> protocol;
16-
shared_ptr<Cache> l1Data;
17-
shared_ptr<Bus> bus;
18-
19-
ProcessorState state;
20-
shared_ptr<Request> currRequest;
21-
unsigned int cycles = 0;
22-
23-
unsigned int memCounter = 0;
24-
unsigned int nonMemCounter = 0;
25-
26-
void execute(unsigned int type, unsigned int value);
27-
void issueBusTransaction();
28-
294
public:
30-
Processor(unsigned int cacheSize, unsigned int associativity, unsigned int blockSize,
31-
shared_ptr<Bus> bus, shared_ptr<Protocol> protocol);
32-
void executeCycle();
33-
void invalidateCache();
5+
virtual void executeCycle() = 0;
6+
virtual void invalidateCache() = 0;
7+
virtual bool isDone() = 0;
348
};

processor.cpp renamed to processor_impl.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "processor.hpp"
1+
#include "processor_impl.hpp"
22
#include "protocol.hpp"
33
#include <iostream>
44
#include <memory>
@@ -7,26 +7,27 @@
77

88
using namespace std;
99

10-
Processor::Processor(unsigned int cacheSize, unsigned int associativity, unsigned int blockSize,
11-
shared_ptr<Bus> bus, shared_ptr<Protocol> protocol)
12-
: state(FREE), protocol(protocol), bus(bus) {
10+
ProcessorImpl::ProcessorImpl(unsigned int cacheSize, unsigned int associativity,
11+
unsigned int blockSize, shared_ptr<Bus> bus,
12+
shared_ptr<Protocol> protocol)
13+
: protocol(protocol), bus(bus) {
1314
l1Data = make_shared<Cache>(cacheSize, associativity, blockSize);
1415
}
1516

16-
void Processor::executeCycle() {
17+
void ProcessorImpl::executeCycle() {
1718
// TODO: read instructions line by line here
1819
switch (state) {
1920
case FREE:
2021
// call execute(); with instruction type and value
2122
break;
2223
case STORE:
23-
if (currRequest->isDone) {
24+
if (currRequest->isDone()) {
2425
state = FREE;
2526
}
2627
cycles++;
2728
break;
2829
case LOAD:
29-
if (currRequest->isDone) {
30+
if (currRequest->isDone()) {
3031
state = FREE;
3132
}
3233
cycles++;
@@ -37,10 +38,12 @@ void Processor::executeCycle() {
3738
}
3839
cycles++;
3940
break;
41+
case MEM_ACCESS:
42+
break;
4043
}
4144
}
4245

43-
void Processor::execute(unsigned int type, unsigned int value) {
46+
void ProcessorImpl::execute(unsigned int type, unsigned int value) {
4447
switch (type) {
4548
case 0: // load
4649
protocol->onLoad(value, bus, l1Data);
@@ -57,6 +60,6 @@ void Processor::execute(unsigned int type, unsigned int value) {
5760
}
5861
}
5962

60-
void Processor::issueBusTransaction() {}
63+
void ProcessorImpl::invalidateCache() {}
6164

62-
void Processor::invalidateCache() {}
65+
bool ProcessorImpl::isDone() { return done; }

processor_impl.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include "bus.hpp"
4+
#include "cache.hpp"
5+
#include "processor.hpp"
6+
#include "protocol.hpp"
7+
#include "request.hpp"
8+
#include <memory>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
enum ProcessorState { FREE, LOAD, STORE, NON_MEMORY, MEM_ACCESS };
14+
15+
class ProcessorImpl : public Processor {
16+
private:
17+
shared_ptr<Protocol> protocol;
18+
shared_ptr<Cache> l1Data;
19+
shared_ptr<Bus> bus;
20+
21+
ProcessorState state = FREE;
22+
shared_ptr<Request> currRequest = nullptr;
23+
unsigned int cycles = 0;
24+
25+
unsigned int memCounter = 0;
26+
unsigned int nonMemCounter = 0;
27+
28+
bool done = false;
29+
30+
void execute(unsigned int type, unsigned int value);
31+
32+
public:
33+
ProcessorImpl(unsigned int cacheSize, unsigned int associativity, unsigned int blockSize,
34+
shared_ptr<Bus> bus, shared_ptr<Protocol> protocol);
35+
void executeCycle();
36+
void invalidateCache();
37+
bool isDone();
38+
};

protocol.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#pragma once
2-
32
#include "bus.hpp"
43
#include "cache.hpp"
54
#include <memory>
65

6+
using namespace std;
7+
78
class Protocol {
89
public:
910
virtual void onLoad(unsigned int address, shared_ptr<Bus> bus, shared_ptr<Cache> cache) = 0;

request.hpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#pragma once
22

33
class Request {
4-
public:
5-
bool isDone = false;
4+
private:
5+
bool done = false;
66
unsigned int countdown = 0;
7+
8+
public:
9+
bool isDone() { return done; };
10+
void decrement() { countdown--; }
711
};

0 commit comments

Comments
 (0)