-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.java
More file actions
82 lines (58 loc) · 1.84 KB
/
Copy pathCache.java
File metadata and controls
82 lines (58 loc) · 1.84 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
package MaquinaVirtual;
public class Cache extends RAM {
private RAM ram;
private int[] cache;
private int startAddress;
private boolean modified;
public Cache(int memorySize, RAM ram) {
super(memorySize);
this.ram = ram;
this.cache = new int[memorySize];
this.modified = false;
this.startAddress = 0;
pullCache(startAddress);
}
@Override
public int read(int address) {
if(address <= (startAddress+cache.length-1) && address >= startAddress) {
return cache[address-startAddress];
}
else{
pullCache(address);
return read(address);
}
}
@Override
public int write(int address, int value) {
if(address <= (startAddress+cache.length-1) && address >= startAddress) {
cache[address-startAddress] = value;
modified = true;
}
else{
pullCache(address);
write(address, value);
}
return 1;
}
public void pushCache() {
for(int i = 0; i < cache.length; i++) {
//System.out.println("Writing " + cache[i] + " to " + (startAddress+i));
ram.write(startAddress+i, cache[i]);
}
}
public void pullCache(int startAddress) {
if(modified){
pushCache();
}
//for last memory address
this.startAddress = startAddress;
if(startAddress+cache.length > ram.getMemorySize()){
startAddress = ram.getMemorySize()-cache.length;
this.startAddress = startAddress;
}
for(int i = 0; i < cache.length; i++) {
//System.out.println("Pulling cache from address " + (startAddress+i));
cache[i] = ram.read(startAddress+i);
}
}
}