Working add/mul

This commit is contained in:
Imbus 2024-04-16 07:58:37 +02:00
parent a60862ed9d
commit 1dab9e69c7
8 changed files with 97 additions and 41 deletions

View file

@ -1,28 +0,0 @@
package hatelace;
/** Add immediate to address, not register */
public class Addi extends Instruction {
private int dest_addr;
private int src1_addr;
private int imm;
public Addi(int dest_addr, int src1_addr, int imm) {
this.dest_addr = dest_addr;
this.src1_addr = src1_addr;
this.imm = imm;
}
public void execute(Memory memory, ProgramCounter PC) {
int src1 = memory.read(this.src1_addr);
memory.write(this.dest_addr, src1 + this.imm);
PC.incPC();
}
protected String opcode() {
return "addi";
}
protected Object[] operands() {
return new Object[] {this.dest_addr, this.src1_addr, this.imm};
}
}

View file

@ -1,16 +1,27 @@
package hatelace;
import hatelace.instructions.*;
public class App {
public static void main(String[] args) {
System.out.println("Starting...");
Memory memory = new Memory();
Memory memory = new Memory(64); // 64 words of memory
Computer computer = new Computer(memory);
Program program = new Program(new int[] { 1, 2, 3, 4, 5 });
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
});
computer.load(program);
computer.run();
memory.dump();
// Print the program
System.out.println("Program:");
for (Instruction instruction : program.getInstructions()) {
System.out.println(instruction);
}
System.out.println("Completed");
}
}

View file

@ -2,22 +2,20 @@ package hatelace;
public class Computer {
private Memory memory;
private Program program;
public Computer(Memory Memory) {
this.memory = Memory;
}
public void load(Program program) {
int[] instructions = program.getInstructions();
for (int i = 0; i < instructions.length; i++) {
this.memory.write(i, instructions[i]);
}
this.program = program;
}
public void run() {
for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.memory.size(); PC.incPC()) {
int instruction = this.memory.read(PC.getPC());
System.out.println(instruction);
// Note that the instructions themselves are responsible for incrementing the PC
for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size(); ) {
program.getInstructions()[PC.getPC()].execute(this.memory, PC);
}
}
}

View file

@ -4,7 +4,11 @@ public class Memory {
protected int[] memory;
public Memory() {
this.memory = new int[1024];
this.memory = new int[256];
}
public Memory(int size) {
this.memory = new int[size];
}
public int read(int address) {
@ -27,7 +31,14 @@ public class Memory {
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

@ -1,13 +1,17 @@
package hatelace;
public class Program {
private int[] instructions;
private Instruction[] instructions;
public Program(int[] instructions) {
public Program(Instruction[] instructions) {
this.instructions = instructions;
}
public int[] getInstructions() {
public Instruction[] getInstructions() {
return this.instructions;
}
public int size() {
return this.instructions.length;
}
}

View file

@ -0,0 +1,30 @@
package hatelace.instructions;
import hatelace.Instruction;
import hatelace.Memory;
import hatelace.ProgramCounter;
public class Add extends Instruction {
private int op1;
private int op2;
private int dest;
public Add(int op1, int op2, int dest) {
this.op1 = op1;
this.op2 = op2;
this.dest = dest;
}
public void execute(Memory memory, ProgramCounter PC) {
memory.write(this.dest, this.op1 + this.op2);
PC.incPC();
}
protected String opcode() {
return "add";
}
protected Object[] operands() {
return new Object[] {this.op1, this.op2, this.dest};
}
}

View file

@ -0,0 +1,30 @@
package hatelace.instructions;
import hatelace.Instruction;
import hatelace.Memory;
import hatelace.ProgramCounter;
public class Mul extends Instruction {
private int op1;
private int op2;
private int dest;
public Mul(int op1, int op2, int dest) {
this.op1 = op1;
this.op2 = op2;
this.dest = dest;
}
public void execute(Memory memory, ProgramCounter PC) {
memory.write(this.dest, this.op1 * this.op2);
PC.incPC();
}
protected String opcode() {
return "mul";
}
protected Object[] operands() {
return new Object[] {this.op1, this.op2, this.dest};
}
}