forked from jzhu2022/cs535-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set.java
91 lines (75 loc) · 1.99 KB
/
Set.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class Set {
private int ways;
enum POLICY = {LRU, FIFO};
POLICY policy;
private int index;
private int lineSize;
private CacheLine[] lines;
public Set(int w, POLICY p, int l) {
ways = w;
policy = p;
lineSize = l;
lines = new CacheLine[ways];
for (int i = 0; i < ways; i++) {
lines[i] = new CacheLine(l, 0);
}
}
public CacheLine read(int tag) {
for (int i = 0; i < ways; i++) {
if (lines.isValid() && lines[i].getTag() == tag) {
return lines[i];
}
}
return null;
}
public int write(int tag, int word, int data) {
int way = getEmpty();
if (way == -1) {
evict(tag, data);
}
else {
lines[way] = new
}
}
public CacheLine getLine(int tag) {
return lines[find(tag)];
}
public int findNext() {
int max = -1;
int l = -1;
for (int i = 0; i < ways; i++) {
if (!lines[i].isValid()) return i;
if (lines[i].priority > max) {
max = lines[i].priority;
l = i;
}
}
return l;
}
private int find(int tag) {
for (int i = 0; i < lineSize; i++) {
if (lines.isValid() && lines[i].getTag() == tag) {
return i;
}
}
return -1;
}
public void put(int tag, CacheLine c) {
int loc = findEmpty();
if (loc == -1) {
loc = evict();
}
lines[loc] = c;
}
public int evict() {
int loc = leaving();
updatePriorities(sets[loc].priority);
if (lines[loc].dirty) {
}
}
private void updatePriorities(int p) {
for (int i = 0; i < ways; i++) {
if (lines[i].priority < p) lines[i].incrementP();
}
}
}