-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2Cache.java
More file actions
165 lines (162 loc) · 5.68 KB
/
L2Cache.java
File metadata and controls
165 lines (162 loc) · 5.68 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.LinkedList;
public class L2Cache {
// Condtant values for l2.
private final Bit T = new Bit(true);
private final Word32[] L2 = new Word32[32];
private final Word32[] bases = new Word32[4];
private final Word32 BLANK = new Word32();
public final Word32 NEGONE = new Word32(new Bit[]{T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T});
// Values for address, baseAddress, value, and FULL_CACHE and IndexesUsed for processor to check.
public Word32 address = new Word32();
public Word32 baseAddress = new Word32();
public Word32 value = new Word32();
public Bit FULL_CACHE = new Bit(false);
public LinkedList<Integer> IndexesUsed = new LinkedList<Integer>();
// Initializes basea and cache in constructor to -1 in binary.
public L2Cache(){
address = new Word32();
value = new Word32();
for(int i = 0; i < L2.length; i++)
L2[i] = new Word32();
for(int i = 0; i < bases.length; i++){
bases[i] = new Word32();
NEGONE.copy(bases[i]);
}
}
// Iterate thru bases, if bases[i] equals baseAddress, subtract address and baseAddress and then writes to cache.
public void write(){
int section = 0;
int addr = 0;
for(int i = 0; i < bases.length; i++){
if(bases[i].equals(baseAddress)){
Word32 RealIndex = new Word32();
Adder.subtract(address, baseAddress, RealIndex);
addr = addressAsInt(RealIndex);
section = 8 * i;
remove(i);
IndexesUsed.add(i);
break;
}
}
value.copy(L2[section + addr]);
}
// Finds baseAddress, then read the memory from the block in which the address-baseAddress is from.
public void read(){
for(int i = 0; i < bases.length; i++){
if(bases[i].equals(baseAddress)){
remove(i);
IndexesUsed.add(i);
ReadMemFromBlock(i);
}
}
}
// determines the address of the block and returns the value.
private void ReadMemFromBlock(int i){
Word32 result = new Word32();
Adder.subtract(address, baseAddress, result);
int addrInd = addressAsInt(result);
L2[(8*i) + addrInd].copy(value);
}
// Returns an int based on the input.
public int addressAsInt(Word32 inp){
Word32 wrd = new Word32();
Word32 oneVal = new Word32();
oneVal.setBitN(31, new Bit(true));
int fin = 0;
while(!wrd.equals(inp)){
Word32 temp = new Word32();
wrd.copy(temp);
Adder.add(temp,oneVal, wrd );
fin++;
}
if(fin < 0 || fin > 999)
return -1;
return fin;
}
// method checks if the input is a base.
public boolean checkIfInAddr(Word32 inp){
for(int i = 0; i < bases.length; i++){
if(inp.equals(bases[i]))
return true;
}
return false;
}
// addAddress adds address to bases if a base is -1, if it's already in bases then do nothing. If not added then
// evist the least used cache and then assign it to the new quarter.
public void addAddr(Word32 inp){
boolean added = false;
for(int i = 0; i < bases.length; i++){
if(bases[i].equals(inp)){
remove(i);
IndexesUsed.add(i);
added = true;
break;
}
else if(bases[i].equals(NEGONE)){
inp.copy(bases[i]);
IndexesUsed.add(i);
added = true;
break;
}
}
if(!added){
int indToRemove = IndexesUsed.remove(0);
inp.copy(bases[indToRemove]);
FULL_CACHE.assign(Bit.boolValues.TRUE);
}
}
// getAddr from returns an addess directly from l2
public Word32[] GetAddrFrom(int i){
Word32[] fin = new Word32[8];
for(int n = 0; n < 8; n++)
fin[n] = new Word32();
for(int n = 0; n < 8; n++)
L2[(8*i)+n].copy(fin[n]);
return fin;
}
// getvalues returns a page of values based on a baseaddress
public Word32[] getValuesBy(Word32 inp){
Word32[] fin = new Word32[8];
for(int i = 0; i < bases.length; i++){
if(inp.equals(bases[i])){
fin = GetAddrFrom(i);
break;
}
}
return fin;
}
// configureL2 configures their cache using a base and an array of words.
public void configureL2(Word32 base, Word32[] arr){
int i = 0;
while(i < bases.length){
if(bases[i].equals(base)){
int baseInt = 8 * i;
for(int n = 0; n < 8; n++)
arr[n].copy(L2[baseInt+n]);
break;
}
i++;
};
}
// remove gets rid of indexesUsed based on input i
private void remove(int i){
for(int n = 0; n < IndexesUsed.size(); n++){
if(IndexesUsed.get(n) == i){
IndexesUsed.remove(n);
break;
}
}
}
// Returns base on inpit i from IndexesUsed.
public Word32 getBaseBy(int i){
return bases[i];
}
// isFull checks if any of the bases are negative 1.
public boolean isFull(){
for(int i = 0; i < bases.length; i++){
if(bases[i].equals(NEGONE))
return false;
}
return true;
}
}