Working generic word width

This commit is contained in:
Imbus 2024-04-16 12:42:57 +02:00
parent 1dab9e69c7
commit 04840349a9
11 changed files with 143 additions and 48 deletions

View file

@ -34,7 +34,7 @@ java {
application {
// Define the main class for the application.
mainClass.set("hatelace.App")
mainClass.set("hatelace.Main")
}
tasks.named<Test>("test") {

View file

@ -0,0 +1,13 @@
package hatelace;
public class Address {
int address;
public Address(int address) {
this.address = address;
}
public int getAddress() {
return address;
}
}

View file

@ -0,0 +1,46 @@
package hatelace;
public class IntMemory extends Memory {
protected int[] memory;
public IntMemory() {
this.memory = new int[256];
}
public IntMemory(int size) {
this.memory = new int[size];
}
public int read(int address) {
return this.memory[address];
}
public int size() {
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();
}
/** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_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++) {
int 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,29 @@
package hatelace;
public class IntWord extends Word {
private Integer value;
public IntWord(Integer value) {
this.value = value;
}
public <T> T getValue() {
return (T) value;
}
public <T> T add(Word other) {
return (T) new IntWord(value + (Integer) other.getValue());
}
public <T> T subtract(Word other) {
return (T) new IntWord(value - (Integer) other.getValue());
}
public <T> T multiply(Word other) {
return (T) new IntWord(value * (Integer) other.getValue());
}
public <T> T divide(Word other) {
return (T) new IntWord(value / (Integer) other.getValue());
}
}

View file

@ -2,15 +2,16 @@ package hatelace;
import hatelace.instructions.*;
public class App {
public class Main {
public static void main(String[] args) {
System.out.println("Starting...");
Memory memory = new Memory(64); // 64 words of memory
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 Mul(2, 2, 1), // Store 2 * 2 in address 1
new Copy(new IntWord(3), new Address(2)) // Store 3 in address 2
});
computer.load(program);
computer.run();

View file

@ -1,46 +1,10 @@
package hatelace;
public class Memory {
protected int[] memory;
public Memory() {
this.memory = new int[256];
}
public Memory(int size) {
this.memory = new int[size];
}
public int read(int address) {
return this.memory[address];
}
public int size() {
return this.memory.length;
}
public void write(int address, int data) {
if (address < 0 || address >= this.memory.length) {
throw new IllegalArgumentException("Invalid memory address");
}
this.memory[address] = data;
}
public abstract class Memory {
public abstract int read(int address);
public abstract int size();
public abstract void write(int address, Word data);
/** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_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++) {
int 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();
}
}
}
public abstract void dump();
}

View file

@ -1,5 +1,6 @@
package hatelace;
/** Simple class to represent the Program Counter */
public class ProgramCounter {
private int PC;

View file

@ -0,0 +1,9 @@
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);
}

View file

@ -3,6 +3,7 @@ package hatelace.instructions;
import hatelace.Instruction;
import hatelace.Memory;
import hatelace.ProgramCounter;
import hatelace.IntWord;
public class Add extends Instruction {
private int op1;
@ -16,7 +17,7 @@ public class Add extends Instruction {
}
public void execute(Memory memory, ProgramCounter PC) {
memory.write(this.dest, this.op1 + this.op2);
memory.write(this.dest, new IntWord(this.op1 + this.op2));
PC.incPC();
}

View file

@ -0,0 +1,30 @@
package hatelace.instructions;
import hatelace.Address;
import hatelace.Word;
import hatelace.Memory;
import hatelace.ProgramCounter;
import hatelace.Instruction;
public class Copy extends Instruction {
private Word word;
private Address address;
public Copy(Word word, Address address) {
this.word = word;
this.address = address;
}
public void execute(Memory memory, ProgramCounter PC) {
memory.write(this.address.getAddress(), this.word);
PC.incPC();
}
protected String opcode() {
return "copy";
}
protected Object[] operands() {
return new Object[] {this.word, this.address};
}
}

View file

@ -3,6 +3,7 @@ package hatelace.instructions;
import hatelace.Instruction;
import hatelace.Memory;
import hatelace.ProgramCounter;
import hatelace.IntWord;
public class Mul extends Instruction {
private int op1;
@ -16,7 +17,7 @@ public class Mul extends Instruction {
}
public void execute(Memory memory, ProgramCounter PC) {
memory.write(this.dest, this.op1 * this.op2);
memory.write(this.dest, new IntWord(this.op1 * this.op2));
PC.incPC();
}