-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.java
More file actions
69 lines (67 loc) · 2.37 KB
/
Memory.java
File metadata and controls
69 lines (67 loc) · 2.37 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
public class Memory {
// address/value created for memory to utilize
public Word32 address= new Word32();
public Word32 value = new Word32();
// dram created as registers for memory.
private final Word32[] dram= new Word32[1000];
// Functions similar to testAdder.toInt, but returns -1 if less than 0
// or greater than 999. Start new int at 0, add 1 to it every time it is not equal to address
public int addressAsInt(){
Word32 wrd = new Word32();
Word32 oneVal = new Word32();
oneVal.setBitN(31, new Bit(true));
int fin = 0;
while(!wrd.equals(address)){
Word32 temp = new Word32();
wrd.copy(temp);
Adder.add(temp,oneVal, wrd );
fin++;
}
if(fin < 0 || fin > 999)
return -1;
return fin;
}
// Initializes memory as an empty set of word32.
public Memory() {
for(int i = 0; i < 1000; i++)
dram[i] = new Word32();
}
// Derives address from addressAsInt, Gets value of word32 from dram[addr],
// Iterates through each bit i, and set the bitval to value at index i
public void read() {
int addr = addressAsInt();
dram[addr].copy(value);
}
// Write gets addressAsInt, iterates through dram as int i, gets bit at
// value[i] and sets it to dram[addr][i]
public void write() {
int addr = addressAsInt();
value.copy(dram[addr]);
}
// Creates oneVal word set to 1. Iterates through data, and
// sets each character to value. Then adds 1 to address and
// calls write to set edits to dram after address and value have
// been set up.
public void load(String[] data) {
Word32 oneVal = new Word32();
oneVal.setBitN(31, new Bit(true));
for(int j = 0; j < data.length; j++){
for(int i = 0; i < data[j].length(); i++){
Bit b = new Bit(false);
if(data[j].charAt(i) == 't')
b.assign(Bit.boolValues.TRUE);
value.setBitN(i, b);
}
Word32 cp = new Word32();
address.copy(cp);
if(j != 0)
Adder.add(cp, oneVal, address);
write();
}
}
public void printAddrs(){
for(int i = 0; i < 32; i++){
System.out.println(i + ": " + dram[i].toString());
}
}
}