-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.java
More file actions
26 lines (21 loc) · 724 Bytes
/
Copy pathMemory.java
File metadata and controls
26 lines (21 loc) · 724 Bytes
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
public class Memory {
private final byte[] memory;
public Memory() {
memory = new byte[65536]; // 64 KB
}
public byte readByte(int address) {
return memory[address & 0xFFFF]; // prevent wrap around
}
public void writeByte(int address, byte value) {
memory[address & 0xFFFF] = value;
}
public void writeWord(int address, int value) {
writeByte(address, (byte)(value & 0xFF)); // low byte
writeByte(address + 1, (byte)((value >> 8) & 0xFF)); // high byte
}
public int readWord(int address) {
int low = readByte(address) & 0xFF;
int high = readByte(address + 1) & 0xFF;
return (high << 8) | low;
}
}