-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstructionCache.java
More file actions
75 lines (72 loc) · 2.81 KB
/
InstructionCache.java
File metadata and controls
75 lines (72 loc) · 2.81 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
public class InstructionCache {
// static final values.
private final Bit F = new Bit(false);
private final Bit T = new Bit(true);
private final Word32 EIGHT = new Word32(new Bit[]{F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,T,T});
private final Word32[] CACHE = new Word32[8];
// address, value, and baseAddress configured for instruction cache
public Word32 address;
public Word32 value;
public Word32 baseAddress = new Word32();
private ALU alu = new ALU();
// Constructor initializes the Word32's in bases and cache arr.
public InstructionCache(){
address = new Word32();
value = new Word32();
for(int i = 0; i < 8; i++)
CACHE[i] = new Word32();
Bit b = new Bit(true);
for(int i = 0; i < 32; i++)
baseAddress.setBitN(i, b);
}
// Converts address to int.
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;
}
// read subtracts address to baseAddress, and then copies address as int from the result to value.
public void read(){
Word32 addrInL1 = new Word32();
Adder.subtract(address, baseAddress,addrInL1);
CACHE[addressAsInt(addrInL1)].copy(value);
}
// checkIfInRange first checks if baseAddress is negative, if so return false
// It then checks if address is less than base address, if so then return false.
// If address is less than base + 8, or if it's greater than base + 8, return false.
// If it passes this test return true.
public boolean checkIfInRange(Word32 address){
Bit b = new Bit(false);
baseAddress.getBitN(0, b);
if(b.getValue() == Bit.boolValues.TRUE)
return false;
alu.COMPARE.copy(alu.instruction);
address.copy(alu.op1);
baseAddress.copy(alu.op2);
alu.doInstruction();
if(alu.less.getValue() == Bit.boolValues.TRUE)
return false;
Word32 max = new Word32();
Adder.add(baseAddress, EIGHT, max);
max.copy(alu.op2);
alu.doInstruction();
if(alu.less.getValue() == Bit.boolValues.FALSE && alu.equal.getValue() == Bit.boolValues.FALSE)
return false;
return true;
}
// configureL1 configures inp into cache.
public void configureL1(Word32[] inp){
for(int i = 0; i < inp.length; i++)
inp[i].copy(CACHE[i]);
}
}