Compare commits
2 commits
505bc620a4
...
a323782664
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a323782664 | ||
![]() |
4227d3998b |
11 changed files with 62 additions and 57 deletions
|
@ -0,0 +1,20 @@
|
||||||
|
package Programs;
|
||||||
|
|
||||||
|
import hatelace.*;
|
||||||
|
import hatelace.instructions.*;
|
||||||
|
|
||||||
|
public class Factorial extends Program {
|
||||||
|
public Factorial() {
|
||||||
|
Address n = new Address(0),
|
||||||
|
fac = new Address(1);
|
||||||
|
|
||||||
|
add(new Copy(new IntWord(5), n));
|
||||||
|
add(new Copy(new IntWord(1), fac));
|
||||||
|
add(new JumpEq(6, n, new IntWord(1)));
|
||||||
|
add(new Mul(fac, n, fac));
|
||||||
|
add(new Add(n, new IntWord(-1), n));
|
||||||
|
add(new Jump(2));
|
||||||
|
add(new Print(fac));
|
||||||
|
add(new Halt());
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ public class Computer {
|
||||||
public void run() {
|
public void run() {
|
||||||
// Note that the instructions themselves are responsible for incrementing the PC
|
// Note that the instructions themselves are responsible for incrementing the PC
|
||||||
for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size() && !PC.halted() && PC.getSysTicks() < 100; ) {
|
for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size() && !PC.halted() && PC.getSysTicks() < 100; ) {
|
||||||
program.getInstructions()[PC.getPC()].execute(this.memory, PC);
|
program.get(PC.getPC()).execute(this.memory, PC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package hatelace;
|
package hatelace;
|
||||||
|
|
||||||
import hatelace.instructions.*;
|
import Programs.Factorial;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -8,26 +8,15 @@ public class Main {
|
||||||
|
|
||||||
Memory memory = new IntMemory(64); // 64 words of memory
|
Memory memory = new IntMemory(64); // 64 words of memory
|
||||||
Computer computer = new Computer(memory);
|
Computer computer = new Computer(memory);
|
||||||
|
Program program = new Factorial();
|
||||||
|
|
||||||
Address n = new Address(0), fac = new Address(1);
|
|
||||||
|
|
||||||
Program program = new Program(new Instruction[] {
|
|
||||||
new Copy(new IntWord(5), n),
|
|
||||||
new Copy(new IntWord(1), fac),
|
|
||||||
new JumpEq(6, n, new IntWord(1)),
|
|
||||||
new Mul(fac, n, fac),
|
|
||||||
new Add(n, new IntWord(-1), n),
|
|
||||||
new Jump(2),
|
|
||||||
new Print(fac),
|
|
||||||
new Halt()
|
|
||||||
});
|
|
||||||
computer.load(program);
|
computer.load(program);
|
||||||
computer.run();
|
computer.run();
|
||||||
memory.dump();
|
memory.dump();
|
||||||
|
|
||||||
// Print the program
|
// Print the program
|
||||||
System.out.println("Program:");
|
System.out.println("Program:");
|
||||||
for (Instruction instruction : program.getInstructions()) {
|
for (Instruction instruction : program) {
|
||||||
System.out.println(instruction);
|
System.out.println(instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,6 @@
|
||||||
package hatelace;
|
package hatelace;
|
||||||
|
|
||||||
public class Program {
|
import java.util.ArrayList;
|
||||||
private Instruction[] instructions;
|
|
||||||
|
|
||||||
public Program(Instruction[] instructions) {
|
// This is simply an intermediate class to comply with the specified API requirements.
|
||||||
this.instructions = instructions;
|
public abstract class Program extends ArrayList<Instruction> {}
|
||||||
}
|
|
||||||
|
|
||||||
public Instruction[] getInstructions() {
|
|
||||||
return this.instructions;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int size() {
|
|
||||||
return this.instructions.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,18 +3,19 @@ package hatelace.instructions;
|
||||||
import hatelace.*;
|
import hatelace.*;
|
||||||
|
|
||||||
public class Add extends Instruction {
|
public class Add extends Instruction {
|
||||||
private Address op1;
|
private Address src;
|
||||||
private Word op2;
|
private Word imm;
|
||||||
private Address dest;
|
private Address dest;
|
||||||
|
|
||||||
public Add(Address op1, Word op2, Address dest) {
|
/** Add immediate value to memory address. */
|
||||||
this.op1 = op1;
|
public Add(Address src, Word imm, Address dest) {
|
||||||
this.op2 = op2;
|
this.src = src;
|
||||||
|
this.imm = imm;
|
||||||
this.dest = dest;
|
this.dest = dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
memory.write(this.dest, memory.read(this.op1).add(this.op2));
|
memory.write(this.dest, memory.read(this.src).add(this.imm));
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +24,6 @@ public class Add extends Instruction {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object[] operands() {
|
protected Object[] operands() {
|
||||||
return new Object[] { this.op1, this.op2, this.dest };
|
return new Object[] { this.src, this.imm, this.dest };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,17 @@ package hatelace.instructions;
|
||||||
import hatelace.*;
|
import hatelace.*;
|
||||||
|
|
||||||
public class Copy extends Instruction {
|
public class Copy extends Instruction {
|
||||||
private Word word;
|
private Word imm;
|
||||||
private Address address;
|
private Address dest;
|
||||||
|
|
||||||
public Copy(Word word, Address address) {
|
/** Copy immediate value to memory address. */
|
||||||
this.word = word;
|
public Copy(Word imm, Address dest) {
|
||||||
this.address = address;
|
this.imm = imm;
|
||||||
|
this.dest = dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
memory.write(this.address, this.word);
|
memory.write(this.dest, this.imm);
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +22,6 @@ public class Copy extends Instruction {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object[] operands() {
|
protected Object[] operands() {
|
||||||
return new Object[] {this.word, this.address};
|
return new Object[] {this.imm, this.dest};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package hatelace.instructions;
|
||||||
import hatelace.*;
|
import hatelace.*;
|
||||||
|
|
||||||
public class Halt extends Instruction {
|
public class Halt extends Instruction {
|
||||||
|
/** Halts the CPU */
|
||||||
public Halt(){};
|
public Halt(){};
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import hatelace.*;
|
||||||
public class Jump extends Instruction {
|
public class Jump extends Instruction {
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
|
/** Unconditional jump, non-relative */
|
||||||
public Jump(int index) {
|
public Jump(int index) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,18 @@ import hatelace.*;
|
||||||
|
|
||||||
public class JumpEq extends Instruction {
|
public class JumpEq extends Instruction {
|
||||||
private int index;
|
private int index;
|
||||||
private Address address;
|
private Address src;
|
||||||
private Word value;
|
private Word imm;
|
||||||
|
|
||||||
public JumpEq(int index, Address address, Word value) {
|
/** Conditional jump */
|
||||||
|
public JumpEq(int index, Address src, Word imm) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.address = address;
|
this.src = src;
|
||||||
this.value = value;
|
this.imm = imm;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
if (this.value.equals(memory.read(this.address))) {
|
if (this.imm.equals(memory.read(this.src))) {
|
||||||
PC.setPC(this.index);
|
PC.setPC(this.index);
|
||||||
} else {
|
} else {
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
|
@ -26,6 +27,6 @@ public class JumpEq extends Instruction {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object[] operands() {
|
protected Object[] operands() {
|
||||||
return new Object[] {this.index, this.address, this.value};
|
return new Object[] {this.index, this.src, this.imm};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,19 @@ package hatelace.instructions;
|
||||||
import hatelace.*;
|
import hatelace.*;
|
||||||
|
|
||||||
public class Mul extends Instruction {
|
public class Mul extends Instruction {
|
||||||
private Address op1;
|
private Address src1;
|
||||||
private Address op2;
|
private Address src2;
|
||||||
private Address dest;
|
private Address dest;
|
||||||
|
|
||||||
public Mul(Address op1, Address op2, Address dest) {
|
/** Multiply contents of two addresses and store the result in a third memory address. */
|
||||||
this.op1 = op1;
|
public Mul(Address src1, Address src2, Address dest) {
|
||||||
this.op2 = op2;
|
this.src1 = src1;
|
||||||
|
this.src2 = src2;
|
||||||
this.dest = dest;
|
this.dest = dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Memory memory, ProgramCounter PC) {
|
public void execute(Memory memory, ProgramCounter PC) {
|
||||||
memory.write(this.dest, memory.read(this.op1).multiply(memory.read(this.op2)));
|
memory.write(this.dest, memory.read(this.src1).multiply(memory.read(this.src2)));
|
||||||
PC.incPC();
|
PC.incPC();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +24,6 @@ public class Mul extends Instruction {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object[] operands() {
|
protected Object[] operands() {
|
||||||
return new Object[] {this.op1, this.op2, this.dest};
|
return new Object[] { this.src1, this.src2, this.dest };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import hatelace.*;
|
||||||
public class Print extends Instruction {
|
public class Print extends Instruction {
|
||||||
private Address address;
|
private Address address;
|
||||||
|
|
||||||
|
/** Print content of memory address */
|
||||||
public Print(Address address) {
|
public Print(Address address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue