Skip to content

Commit b5b6d6a

Browse files
committed
added MSI implementation
1 parent e9aa8df commit b5b6d6a

File tree

7 files changed

+99
-18
lines changed

7 files changed

+99
-18
lines changed

MSI.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdlib.h>
2+
#include <iostream>
3+
#include "MSI.h"
4+
5+
MSI::MSI (int processorsNum, Processor **p) {
6+
processors = p;
7+
procNum = processorsNum;
8+
}
9+
10+
void MSI::readRequest(int processorId, int address) {
11+
std::cout<<"1 " << processorId << " " << address <<"\n";
12+
int cacheLine = processors[processorId]->cache->findCacheLine(address);
13+
std::cout<<"2\n";
14+
int tag = processors[processorId]->cache->findTag(address);
15+
std::cout<<"3 " <<cacheLine<< " "<<address<< " " <<tag<<"\n";
16+
int word = processors[processorId]->cache->retWord(cacheLine, tag);
17+
std::cout<<"4\n";
18+
19+
20+
if (word != address || processors[processorId]->cache->retStateOfLine(cacheLine) == 0) {
21+
// set this block as Shared (state == 1)
22+
processors[processorId]->cache->setStateOfLine(cacheLine, 1);
23+
processors[processorId]->cache->cacheWord(cacheLine, tag, address);
24+
}
25+
else {
26+
std::cout<< "Hit\n";
27+
}
28+
29+
}
30+
31+
void MSI::writeRequest(int processorId, int address) {
32+
int cacheLine = processors[processorId]->cache->findCacheLine(address);
33+
int tag = processors[processorId]->cache->findTag(address);
34+
int word = processors[processorId]->cache->retWord(cacheLine, tag);
35+
36+
// set this block as Modified
37+
processors[processorId]->cache->setStateOfLine(cacheLine, 2);
38+
processors[processorId]->cache->cacheWord(cacheLine, tag, address);
39+
for (int i = 0; i < procNum; i++){
40+
if (i != processorId) {
41+
int line = processors[i]->cache->findCacheLine(address);
42+
int tag = processors[i]->cache->findTag(address);
43+
if (processors[i]->cache->retWord(line, tag) == address){
44+
processors[i]->cache->setStateOfLine(cacheLine, 0);
45+
}
46+
}
47+
}
48+
// invalidate other blocks in other caches that may have this word
49+
// cached.
50+
51+
}

MSI.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
class MSI {
1+
#pragma once
2+
#include "processor.h"
23

4+
class MSI {
5+
public:
6+
MSI (int processorsNum, Processor **p);
7+
~MSI (){};
8+
void readRequest(int processorId, int address);
9+
void writeRequest(int processorId, int address);
10+
Processor **processors;
311

12+
private:
13+
int procNum;
414

5-
}
15+
};

Main.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include "processor.h"
33
#include "parser.h"
4+
#include "MSI.h"
45
#include <stdlib.h> /* atoi */
56
int main (int argc, char* argv[]) {
67
int lineSize = 1024;
@@ -9,35 +10,45 @@ int main (int argc, char* argv[]) {
910

1011
if (argc < 4) {
1112
std::cout << "Not enough arguments\n";
13+
std::cout << "Provide cache line size, lines' number and processors' number\n";
1214
exit(1);
1315
}
1416

1517
lineSize = atoi(argv[1]);
1618
linesNumber = atoi(argv[2]);
1719
procNumber = atoi(argv[3]);
18-
20+
1921
Processor **procTable = new Processor*[procNumber];
22+
Cache **cacheTable = new Cache*[procNumber];
2023
for (int i = 0; i < procNumber; i++) {
21-
Cache cache(lineSize, linesNumber);
22-
procTable[i] = new Processor(i, &cache);
23-
std::cout << "Cache line size: " << cache.retLineSize() << std::endl;
24-
std::cout << "Cache lines number: " << cache.retLinesNumber() << std::endl;
25-
std::cout << "cache Line of 17: " << cache.findCacheLine(17) << std::endl;
26-
std::cout << "cache tag of 17: " << cache.findTag(17) << std::endl;
27-
cache.cacheWord(cache.findCacheLine(17), cache.findTag(17), 17);
28-
std::cout << "address: " << cache.retWord(cache.findCacheLine(17), cache.findTag(17)) << std::endl;
24+
cacheTable[i] = new Cache(lineSize, linesNumber);
25+
procTable[i] = new Processor(i, cacheTable[i]);
2926
}
3027

28+
MSI coherence_protocol(procNumber, procTable);
29+
30+
std::cout << procTable[0]->cache->retLineSize() << "---\n";
31+
std::cout << coherence_protocol.processors[0]->cache->retLineSize() << "+++\n";
3132
Parser p;
3233
int ret;
3334
ret = p.parse();
3435
int pId;
3536
int adr;
37+
std::string opr;
3638
while (ret) {
3739
// here cache coherence should be implemented
3840
pId = p.getProcessor();
3941
adr = p.getAddress();
40-
std::cout << pId << " " << adr << std::endl;
42+
opr = p.getOper();
43+
if (opr.compare("R") == 0) {
44+
std::cout<<"READ\n";
45+
// std::cout << "Processor: " << pId << " " << opr << " " << adr << std::endl;
46+
coherence_protocol.readRequest(pId, adr);
47+
}
48+
else {
49+
std::cout<<"WRITE\n";
50+
coherence_protocol.writeRequest(pId, adr);
51+
}
4152
ret = p.parse();
4253
}
4354
}

cache.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ Cache::Cache(int size, int number) {
77
linesNumber = number;
88
statePerLine = new int[linesNumber];
99
addrPerLine = new int*[linesNumber];
10-
for (int i = 0; i < lineSize; i ++)
10+
for (int i = 0; i < linesNumber; i ++)
1111
addrPerLine[i] = new int[lineSize];
12+
for (int i = 0; i < linesNumber; i ++)
13+
statePerLine[i] = 0;
14+
int i;
15+
for (i = 0; i < linesNumber; i ++)
16+
for (int j = 0; j < lineSize; j++)
17+
addrPerLine[i][j] = -1;
1218
}
1319

1420
Cache::~Cache(){
@@ -35,11 +41,12 @@ int Cache::findCacheLine(int wordAddress) {
3541
if (wordAddress >= linesNumber) {
3642
return findCacheLine(wordAddress/linesNumber);
3743
}
38-
return wordAddress-1;
44+
45+
return wordAddress;
3946
}
4047

4148
int Cache::findTag(int wordAddress) {
42-
return wordAddress%linesNumber;
49+
return wordAddress%lineSize;
4350
}
4451

4552
int Cache::retWord(int cacheLine, int tag) {

cache.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
class Cache {
24
public:
35
Cache(int size, int number);

parser.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class Parser{
1515

1616
int parse() {
1717
std::string line;
18-
1918
if (std::getline(infile, line)) {
20-
// std::cout << line << std::endl;
19+
std::cout << line << std::endl;
2120
processLine(line);
2221
return 1;
2322
}

processor.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
#pragma once
12
#include "cache.h"
23

34
class Processor {
45
public:
56
Processor(int processorId, Cache *privateCache);
67
void request();
8+
Cache *cache;
79

810
private:
911
int id;
10-
Cache *cache;
1112
};

0 commit comments

Comments
 (0)