-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAM.java
48 lines (31 loc) · 990 Bytes
/
RAM.java
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
package MaquinaVirtual;
public class RAM {
private int memory_size;
private int[] memory;
public RAM(int memory_size) {
this.memory_size = memory_size;
this.memory = new int[memory_size];
}
public int write(int position, int value) {
try{
this.memory[position] = value;
return 1;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Position " + position + " for value"+value+" is out of bounds");
System.out.println("Error: Address out of bounds");
System.exit(1);
return -1;
}
}
public int read(int position) {
if(position < 0 || position >= this.memory_size-1 || this.memory[position] == 0) {
// Invalid position
return -1;
}
return this.memory[position];
}
public int getMemorySize() {
return this.memory_size;
}
}