Checkpoint
This commit is contained in:
parent
b301ba7669
commit
7d7509e852
4 changed files with 18 additions and 13 deletions
|
@ -19,11 +19,8 @@ public class IntMemory extends Memory {
|
|||
return this.memory.length;
|
||||
}
|
||||
|
||||
public void write(int address, Word data) {
|
||||
if (address < 0 || address >= this.memory.length) {
|
||||
throw new IllegalArgumentException("Invalid memory address");
|
||||
}
|
||||
this.memory[address] = data.getValue();
|
||||
public void write(Address address, Word data) {
|
||||
this.memory[address.getAddress()] = data.getValue();
|
||||
}
|
||||
|
||||
/** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */
|
||||
|
|
|
@ -7,8 +7,16 @@ public class IntWord extends Word {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public <T> T getValue() {
|
||||
return (T) value;
|
||||
// public <T> T getValue() {
|
||||
// return value;
|
||||
// }
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public IntWord multiply(Word value) {
|
||||
return new IntWord(this.value * (Integer) value.getValue());
|
||||
}
|
||||
|
||||
public <T> T add(Word other) {
|
||||
|
@ -19,9 +27,9 @@ public class IntWord extends Word {
|
|||
return (T) new IntWord(value - (Integer) other.getValue());
|
||||
}
|
||||
|
||||
public <T> T multiply(Word other) {
|
||||
return (T) new IntWord(value * (Integer) other.getValue());
|
||||
}
|
||||
// public <T> T multiply(Word other) {
|
||||
// return (T) new IntWord(value * (Integer) other.getValue());
|
||||
// }
|
||||
|
||||
public <T> T divide(Word other) {
|
||||
return (T) new IntWord(value / (Integer) other.getValue());
|
||||
|
|
|
@ -9,8 +9,8 @@ public class Main {
|
|||
Memory memory = new IntMemory(64); // 64 words of memory
|
||||
Computer computer = new Computer(memory);
|
||||
Program program = new Program(new Instruction[] {
|
||||
new Add(1, 1, 0), // Store 1 + 1 in address 0
|
||||
new Mul(2, 2, 1), // Store 2 * 2 in address 1
|
||||
new Add(new IntWord(1), new IntWord(1), new Address(0)), // Store 1 + 1 in address 0
|
||||
new Mul(new IntWord(2), new IntWord(2), new Address(1)), // Store 2 * 2 in address 1
|
||||
new Copy(new IntWord(3), new Address(2)) // Store 3 in address 2
|
||||
});
|
||||
computer.load(program);
|
||||
|
|
|
@ -3,7 +3,7 @@ package hatelace;
|
|||
public abstract class Memory {
|
||||
public abstract int read(int address);
|
||||
public abstract int size();
|
||||
public abstract void write(int address, Word data);
|
||||
public abstract void write(Address address, Word data);
|
||||
|
||||
/** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */
|
||||
public abstract void dump();
|
||||
|
|
Loading…
Reference in a new issue