Checkpoint

This commit is contained in:
Imbus 2024-04-16 12:56:14 +02:00
parent b301ba7669
commit 7d7509e852
4 changed files with 18 additions and 13 deletions

View file

@ -19,11 +19,8 @@ public class IntMemory extends Memory {
return this.memory.length; return this.memory.length;
} }
public void write(int address, Word data) { public void write(Address address, Word data) {
if (address < 0 || address >= this.memory.length) { this.memory[address.getAddress()] = data.getValue();
throw new IllegalArgumentException("Invalid memory address");
}
this.memory[address] = data.getValue();
} }
/** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */

View file

@ -7,8 +7,16 @@ public class IntWord extends Word {
this.value = value; this.value = value;
} }
public <T> T getValue() { // public <T> T getValue() {
return (T) value; // 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) { public <T> T add(Word other) {
@ -19,9 +27,9 @@ public class IntWord extends Word {
return (T) new IntWord(value - (Integer) other.getValue()); return (T) new IntWord(value - (Integer) other.getValue());
} }
public <T> T multiply(Word other) { // public <T> T multiply(Word other) {
return (T) new IntWord(value * (Integer) other.getValue()); // return (T) new IntWord(value * (Integer) other.getValue());
} // }
public <T> T divide(Word other) { public <T> T divide(Word other) {
return (T) new IntWord(value / (Integer) other.getValue()); return (T) new IntWord(value / (Integer) other.getValue());

View file

@ -9,8 +9,8 @@ public class Main {
Memory memory = new IntMemory(64); // 64 words of memory Memory memory = new IntMemory(64); // 64 words of memory
Computer computer = new Computer(memory); Computer computer = new Computer(memory);
Program program = new Program(new Instruction[] { Program program = new Program(new Instruction[] {
new Add(1, 1, 0), // Store 1 + 1 in address 0 new Add(new IntWord(1), new IntWord(1), new Address(0)), // Store 1 + 1 in address 0
new Mul(2, 2, 1), // Store 2 * 2 in address 1 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 new Copy(new IntWord(3), new Address(2)) // Store 3 in address 2
}); });
computer.load(program); computer.load(program);

View file

@ -3,7 +3,7 @@ package hatelace;
public abstract class Memory { public abstract class Memory {
public abstract int read(int address); public abstract int read(int address);
public abstract int size(); 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) */ /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */
public abstract void dump(); public abstract void dump();