Compare commits
3 commits
04840349a9
...
e0845968a1
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e0845968a1 | ||
![]() |
7d7509e852 | ||
![]() |
b301ba7669 |
8 changed files with 34 additions and 35 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,23 +7,23 @@ public class IntWord extends Word {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public <T> T getValue() {
|
||||
return (T) value;
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public <T> T add(Word other) {
|
||||
return (T) new IntWord(value + (Integer) other.getValue());
|
||||
public Word add(Word other) {
|
||||
return new IntWord(value + (Integer) other.getValue());
|
||||
}
|
||||
|
||||
public <T> T subtract(Word other) {
|
||||
return (T) new IntWord(value - (Integer) other.getValue());
|
||||
public Word subtract(Word other) {
|
||||
return new IntWord(value - (Integer) other.getValue());
|
||||
}
|
||||
|
||||
public <T> T multiply(Word other) {
|
||||
return (T) new IntWord(value * (Integer) other.getValue());
|
||||
public Word multiply(Word value) {
|
||||
return new IntWord(this.value * (Integer) value.getValue());
|
||||
}
|
||||
|
||||
public <T> T divide(Word other) {
|
||||
return (T) new IntWord(value / (Integer) other.getValue());
|
||||
public Word divide(Word other) {
|
||||
return 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();
|
||||
|
|
|
@ -2,8 +2,8 @@ package hatelace;
|
|||
|
||||
public abstract class Word {
|
||||
public abstract <T> T getValue();
|
||||
public abstract <T> T add(Word other);
|
||||
public abstract <T> T subtract(Word other);
|
||||
public abstract <T> T multiply(Word other);
|
||||
public abstract <T> T divide(Word other);
|
||||
public abstract Word add(Word other);
|
||||
public abstract Word subtract(Word other);
|
||||
public abstract Word multiply(Word other);
|
||||
public abstract Word divide(Word other);
|
||||
}
|
||||
|
|
|
@ -3,21 +3,22 @@ package hatelace.instructions;
|
|||
import hatelace.Instruction;
|
||||
import hatelace.Memory;
|
||||
import hatelace.ProgramCounter;
|
||||
import hatelace.IntWord;
|
||||
import hatelace.Word;
|
||||
import hatelace.Address;
|
||||
|
||||
public class Add extends Instruction {
|
||||
private int op1;
|
||||
private int op2;
|
||||
private int dest;
|
||||
private Word op1;
|
||||
private Word op2;
|
||||
private Address dest;
|
||||
|
||||
public Add(int op1, int op2, int dest) {
|
||||
public Add(Word op1, Word op2, Address dest) {
|
||||
this.op1 = op1;
|
||||
this.op2 = op2;
|
||||
this.dest = dest;
|
||||
}
|
||||
|
||||
public void execute(Memory memory, ProgramCounter PC) {
|
||||
memory.write(this.dest, new IntWord(this.op1 + this.op2));
|
||||
memory.write(this.dest, op1.add(op2));
|
||||
PC.incPC();
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ public class Copy extends Instruction {
|
|||
}
|
||||
|
||||
public void execute(Memory memory, ProgramCounter PC) {
|
||||
memory.write(this.address.getAddress(), this.word);
|
||||
memory.write(this.address, this.word);
|
||||
PC.incPC();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,23 +1,24 @@
|
|||
package hatelace.instructions;
|
||||
|
||||
import hatelace.Address;
|
||||
import hatelace.Word;
|
||||
import hatelace.Instruction;
|
||||
import hatelace.Memory;
|
||||
import hatelace.ProgramCounter;
|
||||
import hatelace.IntWord;
|
||||
|
||||
public class Mul extends Instruction {
|
||||
private int op1;
|
||||
private int op2;
|
||||
private int dest;
|
||||
private Word op1;
|
||||
private Word op2;
|
||||
private Address dest;
|
||||
|
||||
public Mul(int op1, int op2, int dest) {
|
||||
public Mul(Word op1, Word op2, Address dest) {
|
||||
this.op1 = op1;
|
||||
this.op2 = op2;
|
||||
this.dest = dest;
|
||||
}
|
||||
|
||||
public void execute(Memory memory, ProgramCounter PC) {
|
||||
memory.write(this.dest, new IntWord(this.op1 * this.op2));
|
||||
memory.write(this.dest, op1.multiply(op2));
|
||||
PC.incPC();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue