-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.cpp
58 lines (47 loc) · 1.27 KB
/
cache.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
#include "cache.h"
#include <stdlib.h>
#include <iostream>
Cache::Cache(int size, int number) {
lineSize = size;
linesNumber = number;
statePerLine = new int[linesNumber];
addrPerLine = new int*[linesNumber];
for (int i = 0; i < linesNumber; i ++)
addrPerLine[i] = new int[lineSize];
for (int i = 0; i < linesNumber; i ++)
statePerLine[i] = 0;
int i;
for (i = 0; i < linesNumber; i ++)
for (int j = 0; j < lineSize; j++)
addrPerLine[i][j] = -1;
}
Cache::~Cache(){
delete[] statePerLine;
}
int Cache::retLineSize() {
return lineSize;
}
int Cache::retLinesNumber() {
return linesNumber;
}
int Cache::retStateOfLine(int lineIndex) {
return statePerLine[lineIndex];
}
void Cache::setStateOfLine(int lineIndex, int state) {
statePerLine[lineIndex] = state;
}
int Cache::findCacheLine(int wordAddress) {
if (wordAddress >= linesNumber) {
return findCacheLine(wordAddress/linesNumber);
}
return wordAddress;
}
int Cache::findTag(int wordAddress) {
return wordAddress%lineSize;
}
int Cache::retWord(int cacheLine, int tag) {
return addrPerLine[cacheLine][tag];
}
void Cache::cacheWord(int cacheLine, int tag, int address) {
addrPerLine[cacheLine][tag] = address;
}