Better comments in instructions

This commit is contained in:
Imbus 2024-04-16 14:58:51 +02:00
parent 505bc620a4
commit 4227d3998b
7 changed files with 35 additions and 28 deletions

View file

@ -3,18 +3,19 @@ package hatelace.instructions;
import hatelace.*;
public class Add extends Instruction {
private Address op1;
private Word op2;
private Address src;
private Word imm;
private Address dest;
public Add(Address op1, Word op2, Address dest) {
this.op1 = op1;
this.op2 = op2;
/** Add immediate value to memory address. */
public Add(Address src, Word imm, Address dest) {
this.src = src;
this.imm = imm;
this.dest = dest;
}
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();
}
@ -23,6 +24,6 @@ public class Add extends Instruction {
}
protected Object[] operands() {
return new Object[] { this.op1, this.op2, this.dest };
return new Object[] { this.src, this.imm, this.dest };
}
}

View file

@ -3,16 +3,17 @@ package hatelace.instructions;
import hatelace.*;
public class Copy extends Instruction {
private Word word;
private Address address;
private Word imm;
private Address dest;
public Copy(Word word, Address address) {
this.word = word;
this.address = address;
/** Copy immediate value to memory address. */
public Copy(Word imm, Address dest) {
this.imm = imm;
this.dest = dest;
}
public void execute(Memory memory, ProgramCounter PC) {
memory.write(this.address, this.word);
memory.write(this.dest, this.imm);
PC.incPC();
}
@ -21,6 +22,6 @@ public class Copy extends Instruction {
}
protected Object[] operands() {
return new Object[] {this.word, this.address};
return new Object[] {this.imm, this.dest};
}
}

View file

@ -3,6 +3,7 @@ package hatelace.instructions;
import hatelace.*;
public class Halt extends Instruction {
/** Halts the CPU */
public Halt(){};
public void execute(Memory memory, ProgramCounter PC) {

View file

@ -5,6 +5,7 @@ import hatelace.*;
public class Jump extends Instruction {
private int index;
/** Unconditional jump, non-relative */
public Jump(int index) {
this.index = index;
}

View file

@ -4,17 +4,18 @@ import hatelace.*;
public class JumpEq extends Instruction {
private int index;
private Address address;
private Word value;
private Address src;
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.address = address;
this.value = value;
this.src = src;
this.imm = imm;
}
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);
} else {
PC.incPC();
@ -26,6 +27,6 @@ public class JumpEq extends Instruction {
}
protected Object[] operands() {
return new Object[] {this.index, this.address, this.value};
return new Object[] {this.index, this.src, this.imm};
}
}

View file

@ -3,18 +3,19 @@ package hatelace.instructions;
import hatelace.*;
public class Mul extends Instruction {
private Address op1;
private Address op2;
private Address src1;
private Address src2;
private Address dest;
public Mul(Address op1, Address op2, Address dest) {
this.op1 = op1;
this.op2 = op2;
/** Multiply contents of two addresses and store the result in a third memory address. */
public Mul(Address src1, Address src2, Address dest) {
this.src1 = src1;
this.src2 = src2;
this.dest = dest;
}
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();
}
@ -23,6 +24,6 @@ public class Mul extends Instruction {
}
protected Object[] operands() {
return new Object[] {this.op1, this.op2, this.dest};
return new Object[] { this.src1, this.src2, this.dest };
}
}

View file

@ -5,6 +5,7 @@ import hatelace.*;
public class Print extends Instruction {
private Address address;
/** Print content of memory address */
public Print(Address address) {
this.address = address;
}