diff --git a/app/src/main/java/Programs/Factorial.java b/app/src/main/java/Programs/Factorial.java index bc5fb38..46830bf 100644 --- a/app/src/main/java/Programs/Factorial.java +++ b/app/src/main/java/Programs/Factorial.java @@ -2,6 +2,7 @@ package Programs; import hatelace.*; import hatelace.instructions.*; +import hatelace.memtypes.IntWord; public class Factorial extends Program { public Factorial() { diff --git a/app/src/main/java/Programs/LongFactorial.java b/app/src/main/java/Programs/LongFactorial.java new file mode 100644 index 0000000..7990354 --- /dev/null +++ b/app/src/main/java/Programs/LongFactorial.java @@ -0,0 +1,21 @@ +package Programs; + +import hatelace.*; +import hatelace.instructions.*; +import hatelace.memtypes.LongWord; + +public class LongFactorial extends Program { + public LongFactorial() { + Address n = new Address(0), + fac = new Address(1); + + add(new Copy(new LongWord(5L), n)); + add(new Copy(new LongWord(1L), fac)); + add(new JumpEq(6, n, new LongWord(1L))); + add(new Mul(fac, n, fac)); + add(new Add(n, new LongWord(-1L), n)); + add(new Jump(2)); + add(new Print(fac)); + add(new Halt()); + } +} \ No newline at end of file diff --git a/app/src/main/java/hatelace/Main.java b/app/src/main/java/hatelace/Main.java index 9dcb7f1..d380b1d 100644 --- a/app/src/main/java/hatelace/Main.java +++ b/app/src/main/java/hatelace/Main.java @@ -1,14 +1,15 @@ package hatelace; -import Programs.Factorial; +import Programs.*; // LongFactorial, Factorial +import hatelace.memtypes.*; // LongWord, IntWord, LongMemory, IntMemory public class Main { public static void main(String[] args) { System.out.println("Starting..."); - Memory memory = new IntMemory(64); // 64 words of memory + Memory memory = new LongMemory(64); // 64 words of memory Computer computer = new Computer(memory); - Program program = new Factorial(); + Program program = new LongFactorial(); computer.load(program); computer.run(); diff --git a/app/src/main/java/hatelace/Word.java b/app/src/main/java/hatelace/Word.java index 6ee37e8..db74dbc 100644 --- a/app/src/main/java/hatelace/Word.java +++ b/app/src/main/java/hatelace/Word.java @@ -1,7 +1,7 @@ package hatelace; public abstract class Word { - public abstract Integer getValue(); + public abstract T getValue(); public abstract Word add(Word other); public abstract Word subtract(Word other); public abstract Word multiply(Word other); diff --git a/app/src/main/java/hatelace/IntMemory.java b/app/src/main/java/hatelace/memtypes/IntMemory.java similarity index 91% rename from app/src/main/java/hatelace/IntMemory.java rename to app/src/main/java/hatelace/memtypes/IntMemory.java index 07f46c0..3b83b72 100644 --- a/app/src/main/java/hatelace/IntMemory.java +++ b/app/src/main/java/hatelace/memtypes/IntMemory.java @@ -1,4 +1,8 @@ -package hatelace; +package hatelace.memtypes; + +import hatelace.Address; +import hatelace.Memory; +import hatelace.Word; public class IntMemory extends Memory { protected int[] memory; diff --git a/app/src/main/java/hatelace/IntWord.java b/app/src/main/java/hatelace/memtypes/IntWord.java similarity index 88% rename from app/src/main/java/hatelace/IntWord.java rename to app/src/main/java/hatelace/memtypes/IntWord.java index 4db7f87..a3265a4 100644 --- a/app/src/main/java/hatelace/IntWord.java +++ b/app/src/main/java/hatelace/memtypes/IntWord.java @@ -1,4 +1,6 @@ -package hatelace; +package hatelace.memtypes; + +import hatelace.Word; public class IntWord extends Word { private Integer value; @@ -8,6 +10,7 @@ public class IntWord extends Word { } @Override + @SuppressWarnings("unchecked") // Yes, this is a hack. public Integer getValue() { return value; } diff --git a/app/src/main/java/hatelace/memtypes/LongMemory.java b/app/src/main/java/hatelace/memtypes/LongMemory.java new file mode 100644 index 0000000..cdb0222 --- /dev/null +++ b/app/src/main/java/hatelace/memtypes/LongMemory.java @@ -0,0 +1,47 @@ +package hatelace.memtypes; + +import hatelace.Address; +import hatelace.Memory; +import hatelace.Word; + +public class LongMemory extends Memory { + protected long[] memory; + + public LongMemory() { + this.memory = new long[256]; + } + + public LongMemory(int size) { + this.memory = new long[size]; + } + + public Word read(Address address) { + return new LongWord(this.memory[address.getAddress()]); + } + + public int size() { + return this.memory.length; + } + + public void write(Address address, Word data) { + this.memory[address.getAddress()] = data.getValue(); + } + + /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Longel_HEX) */ + public void dump() { + for(int i = 0; i < this.memory.length; i+= 16) { + System.out.printf(":%02X%04X00 ", 16, i); + for(int j = 0; j < 16; j++) { + long content = this.memory[i + j]; + + // Print it as faded if it's zero + if (content == 0) + System.out.print("\u001B[2m"); + + System.out.printf("%02X ", this.memory[i + j]); + System.out.print("\u001B[0m"); + } + System.out.println(); + } + } +} diff --git a/app/src/main/java/hatelace/memtypes/LongWord.java b/app/src/main/java/hatelace/memtypes/LongWord.java new file mode 100644 index 0000000..a7bf783 --- /dev/null +++ b/app/src/main/java/hatelace/memtypes/LongWord.java @@ -0,0 +1,41 @@ +package hatelace.memtypes; + +import hatelace.Word; + +public class LongWord extends Word { + private Long value; + + public LongWord(Long value) { + this.value = value; + } + + @Override + @SuppressWarnings("unchecked") // Not a good look + public Long getValue() { + return value; + } + + public Word add(Word other) { + return new LongWord(value + (Long) other.getValue()); + } + + public Word subtract(Word other) { + return new LongWord(value - (Long) other.getValue()); + } + + public Word multiply(Word value) { + return new LongWord(this.value * (Long) value.getValue()); + } + + public Word divide(Word other) { + return new LongWord(value / (Long) other.getValue()); + } + + public String toString() { + return value.toString(); + } + + public boolean equals(Object other) { + return value.equals(((LongWord) other).getValue()); + } +}