Successfully ported to 64 bit word width

This commit is contained in:
Imbus 2024-04-16 15:22:38 +02:00
parent a323782664
commit b97d09ac5b
8 changed files with 124 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package Programs;
import hatelace.*;
import hatelace.instructions.*;
import hatelace.memtypes.IntWord;
public class Factorial extends Program {
public Factorial() {

View file

@ -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());
}
}

View file

@ -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();

View file

@ -1,7 +1,7 @@
package hatelace;
public abstract class Word {
public abstract Integer getValue();
public abstract <T> T getValue();
public abstract Word add(Word other);
public abstract Word subtract(Word other);
public abstract Word multiply(Word other);

View file

@ -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;

View file

@ -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;
}

View file

@ -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();
}
}
}

View file

@ -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());
}
}