Rough outline
This commit is contained in:
parent
a10d0ecbb9
commit
a60862ed9d
8 changed files with 155 additions and 13 deletions
28
app/src/main/java/hatelace/Addi.java
Normal file
28
app/src/main/java/hatelace/Addi.java
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
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};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,16 @@
|
||||||
/*
|
|
||||||
* This Java source file was generated by the Gradle 'init' task.
|
|
||||||
*/
|
|
||||||
package hatelace;
|
package hatelace;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public String getGreeting() {
|
|
||||||
return "Hello World!";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(new App().getGreeting());
|
System.out.println("Starting...");
|
||||||
|
|
||||||
|
Memory memory = new Memory();
|
||||||
|
Computer computer = new Computer(memory);
|
||||||
|
Program program = new Program(new int[] { 1, 2, 3, 4, 5 });
|
||||||
|
computer.load(program);
|
||||||
|
computer.run();
|
||||||
|
memory.dump();
|
||||||
|
|
||||||
|
System.out.println("Completed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
23
app/src/main/java/hatelace/Computer.java
Normal file
23
app/src/main/java/hatelace/Computer.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package hatelace;
|
||||||
|
|
||||||
|
public class Computer {
|
||||||
|
private Memory memory;
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
app/src/main/java/hatelace/Instruction.java
Normal file
23
app/src/main/java/hatelace/Instruction.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package hatelace;
|
||||||
|
|
||||||
|
public abstract class Instruction {
|
||||||
|
public abstract void execute(Memory memory, ProgramCounter pc);
|
||||||
|
|
||||||
|
protected abstract String opcode();
|
||||||
|
|
||||||
|
protected abstract Object[] operands();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
// Buffer to hold our formatted instruction
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(this.opcode());
|
||||||
|
|
||||||
|
for (Object operand : this.operands()) {
|
||||||
|
sb.append(" ");
|
||||||
|
sb.append(operand);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
35
app/src/main/java/hatelace/Memory.java
Normal file
35
app/src/main/java/hatelace/Memory.java
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package hatelace;
|
||||||
|
|
||||||
|
public class Memory {
|
||||||
|
protected int[] memory;
|
||||||
|
|
||||||
|
public Memory() {
|
||||||
|
this.memory = new int[1024];
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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++) {
|
||||||
|
System.out.printf("%02X ", this.memory[i + j]);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
app/src/main/java/hatelace/Program.java
Normal file
13
app/src/main/java/hatelace/Program.java
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package hatelace;
|
||||||
|
|
||||||
|
public class Program {
|
||||||
|
private int[] instructions;
|
||||||
|
|
||||||
|
public Program(int[] instructions) {
|
||||||
|
this.instructions = instructions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getInstructions() {
|
||||||
|
return this.instructions;
|
||||||
|
}
|
||||||
|
}
|
22
app/src/main/java/hatelace/ProgramCounter.java
Normal file
22
app/src/main/java/hatelace/ProgramCounter.java
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package hatelace;
|
||||||
|
|
||||||
|
public class ProgramCounter {
|
||||||
|
private int PC;
|
||||||
|
|
||||||
|
public ProgramCounter() {
|
||||||
|
this.PC = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProgramCounter(int PC) {
|
||||||
|
this.PC = PC;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPC() {
|
||||||
|
return this.PC;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int incPC() {
|
||||||
|
this.PC = this.PC + 1 <= 0 ? 0 : this.PC + 1;
|
||||||
|
return this.PC;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,3 @@
|
||||||
/*
|
|
||||||
* This Java source file was generated by the Gradle 'init' task.
|
|
||||||
*/
|
|
||||||
package hatelace;
|
package hatelace;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -8,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class AppTest {
|
class AppTest {
|
||||||
@Test void appHasAGreeting() {
|
@Test void appHasAGreeting() {
|
||||||
App classUnderTest = new App();
|
assertNotNull(1);
|
||||||
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue