Compare commits
No commits in common. "e0845968a17e2dc3ff7e7634d9fc2c4a21ca15d4" and "04840349a95255f37e1c94ecd9384b470135ab01" have entirely different histories.
e0845968a1
...
04840349a9
8 changed files with 35 additions and 34 deletions
|
@ -19,8 +19,11 @@ public class IntMemory extends Memory {
|
||||||
return this.memory.length;
|
return this.memory.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(Address address, Word data) {
|
public void write(int address, Word data) {
|
||||||
this.memory[address.getAddress()] = data.getValue();
|
if (address < 0 || address >= this.memory.length) {
|
||||||
|
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) */
|
||||||
|
|
|
@ -7,23 +7,23 @@ public class IntWord extends Word {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getValue() {
|
public <T> T getValue() {
|
||||||
return value;
|
return (T) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Word add(Word other) {
|
public <T> T add(Word other) {
|
||||||
return new IntWord(value + (Integer) other.getValue());
|
return (T) new IntWord(value + (Integer) other.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Word subtract(Word other) {
|
public <T> T subtract(Word other) {
|
||||||
return new IntWord(value - (Integer) other.getValue());
|
return (T) new IntWord(value - (Integer) other.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Word multiply(Word value) {
|
public <T> T multiply(Word other) {
|
||||||
return new IntWord(this.value * (Integer) value.getValue());
|
return (T) new IntWord(value * (Integer) other.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Word divide(Word other) {
|
public <T> T divide(Word other) {
|
||||||
return new IntWord(value / (Integer) other.getValue());
|
return (T) new IntWord(value / (Integer) other.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(new IntWord(1), new IntWord(1), new Address(0)), // Store 1 + 1 in address 0
|
new Add(1, 1, 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 Mul(2, 2, 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);
|
||||||
|
|
|
@ -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(Address address, Word data);
|
public abstract void write(int 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();
|
||||||
|
|
|
@ -2,8 +2,8 @@ package hatelace;
|
||||||
|
|
||||||
public abstract class Word {
|
public abstract class Word {
|
||||||
public abstract <T> T getValue();
|
public abstract <T> T getValue();
|
||||||
public abstract Word add(Word other);
|
public abstract <T> T add(Word other);
|
||||||
public abstract Word subtract(Word other);
|
public abstract <T> T subtract(Word other);
|
||||||
public abstract Word multiply(Word other);
|
public abstract <T> T multiply(Word other);
|
||||||
public abstract Word divide(Word other);
|
public abstract <T> T divide(Word other);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,22 +3,21 @@ package hatelace.instructions;
|
||||||
import hatelace.Instruction;
|
import hatelace.Instruction;
|
||||||
import hatelace.Memory;
|
import hatelace.Memory;
|
||||||
import hatelace.ProgramCounter;
|
import hatelace.ProgramCounter;
|
||||||
import hatelace.Word;
|
import hatelace.IntWord;
|
||||||
import hatelace.Address;
|
|
||||||
|
|
||||||
public class Add extends Instruction {
|
public class Add extends Instruction {
|
||||||
private Word op1;
|
private int op1;
|
||||||
private Word op2;
|
private int op2;
|
||||||
private Address dest;
|
private int dest;
|
||||||
|
|
||||||
public Add(Word op1, Word op2, Address dest) {
|
public Add(int op1, int op2, int dest) {
|
||||||
this.op1 = op1;
|
this.op1 = op1;
|
||||||
this.op2 = op2;
|
this.op2 = op2;
|
||||||
this.dest = dest;
|
this.dest = dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
memory.write(this.dest, op1.add(op2));
|
memory.write(this.dest, new IntWord(this.op1 + this.op2));
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ public class Copy extends Instruction {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
memory.write(this.address, this.word);
|
memory.write(this.address.getAddress(), this.word);
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,23 @@
|
||||||
package hatelace.instructions;
|
package hatelace.instructions;
|
||||||
|
|
||||||
import hatelace.Address;
|
|
||||||
import hatelace.Word;
|
|
||||||
import hatelace.Instruction;
|
import hatelace.Instruction;
|
||||||
import hatelace.Memory;
|
import hatelace.Memory;
|
||||||
import hatelace.ProgramCounter;
|
import hatelace.ProgramCounter;
|
||||||
|
import hatelace.IntWord;
|
||||||
|
|
||||||
public class Mul extends Instruction {
|
public class Mul extends Instruction {
|
||||||
private Word op1;
|
private int op1;
|
||||||
private Word op2;
|
private int op2;
|
||||||
private Address dest;
|
private int dest;
|
||||||
|
|
||||||
public Mul(Word op1, Word op2, Address dest) {
|
public Mul(int op1, int op2, int dest) {
|
||||||
this.op1 = op1;
|
this.op1 = op1;
|
||||||
this.op2 = op2;
|
this.op2 = op2;
|
||||||
this.dest = dest;
|
this.dest = dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
memory.write(this.dest, op1.multiply(op2));
|
memory.write(this.dest, new IntWord(this.op1 * this.op2));
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue