-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.java
More file actions
60 lines (55 loc) · 1.88 KB
/
Copy pathMemory.java
File metadata and controls
60 lines (55 loc) · 1.88 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
public class Memory {
int memorySize = 16777216;
String[] data;
public Memory() {
data = new String[memorySize];
}
//returns a full block of data from the read
public String readBlock(int address, int blockSize) {
StringBuilder sb = new StringBuilder();
int mod = address % blockSize;
int blockStart = address - mod; //makes sure we are starting at correct block
for(int i = blockStart; i < blockStart + blockSize; i ++) { //memory hasn't been updated either
if(data[i] != null){
sb.append(data[i]);
}
else {
sb.append("00000000");
}
}
//System.out.println(sb.toString());
return sb.toString();
}
//TODO update read function, think about access size
public String read(int address, int access, int blockSize) {
StringBuilder sb = new StringBuilder();
int mod = address % blockSize;
int blockStart = address - mod; //makes sure we are starting at correct block
for(int i = blockStart; i < blockStart + blockSize; i ++) { //memory hasn't been updated either
if(data[i] == null){
sb.append("00000000");
} sb.append(data[i]);
}
return sb.toString();
}
// use for smaller writes to data where there is a write through hit or miss
public void write(int address, String value, int access) {
//System.out.println(value);
// System.out.println(address);
// System.out.println(access);
for(int i = 0; i < access; i++) {
String sub = value.substring(i*8, (i+1)*8);
data[address + i] = sub;
}
}
// used for evictions when whole block must be written to memory
public void blockWrite(int address, String[] val, int blockSize) {
int mod = address % blockSize;
int blockStart = address - mod; //makes sure we are starting at correct block
// System.out.println("BLOCK BEING WRITTEN IN MEMORY");
// System.out.println(blockStart);
for(int i = blockStart; i < blockSize + blockStart; i++) {
data[i] = val[i-blockStart];
}
}
}