mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-18 02:06:43 +00:00
Merge pull request #83 from Francessco121/feature/pushf-popf
Add PUSHF and POPF instructions
This commit is contained in:
commit
93786d92cb
@ -97,6 +97,8 @@ public class CPU implements JSONSerialisable {
|
||||
instructionSet.add(new JncInstruction(this));
|
||||
instructionSet.add(new JnoInstruction(this));
|
||||
instructionSet.add(new JoInstruction(this));
|
||||
instructionSet.add(new PushfInstruction(this));
|
||||
instructionSet.add(new PopfInstruction(this));
|
||||
|
||||
status = new Status();
|
||||
memory = new Memory(config.getInt("memory_size"));
|
||||
|
@ -132,4 +132,20 @@ public class Status {
|
||||
public void setErrorFlag(boolean errorFlag) {
|
||||
this.errorFlag = errorFlag;
|
||||
}
|
||||
|
||||
public char toByte() {
|
||||
char stat = 0;
|
||||
stat = (char) (stat | ((signFlag ? 1 : 0) << 3));
|
||||
stat = (char) (stat | ((zeroFlag ? 1 : 0) << 2));
|
||||
stat = (char) (stat | ((carryFlag ? 1 : 0) << 1));
|
||||
stat = (char) (stat | (overflowFlag ? 1 : 0));
|
||||
return stat;
|
||||
}
|
||||
|
||||
public void fromByte(char stat) {
|
||||
setSignFlag((stat & (1 << 3)) != 0);
|
||||
setZeroFlag((stat & (1 << 2)) != 0);
|
||||
setCarryFlag((stat & (1 << 1)) != 0);
|
||||
setOverflowFlag((stat & 1) != 0);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package net.simon987.server.assembly.instruction;
|
||||
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.assembly.Instruction;
|
||||
import net.simon987.server.assembly.Register;
|
||||
import net.simon987.server.assembly.Status;
|
||||
|
||||
/**
|
||||
* Pops a single word off the top of the stack and sets the CPU flags to it.
|
||||
*/
|
||||
public class PopfInstruction extends Instruction {
|
||||
|
||||
/**
|
||||
* Opcode of the instruction
|
||||
*/
|
||||
public static final int OPCODE = 44;
|
||||
|
||||
private CPU cpu;
|
||||
|
||||
public PopfInstruction(CPU cpu) {
|
||||
super("popf", OPCODE);
|
||||
|
||||
this.cpu = cpu;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status execute(Status status) {
|
||||
|
||||
Register sp = cpu.getRegisterSet().getRegister("SP");
|
||||
|
||||
// Get the word on the top of the stack
|
||||
char flags = (char) cpu.getMemory().get(sp.getValue());
|
||||
|
||||
// Overwrite the CPU flags
|
||||
status.fromByte(flags);
|
||||
|
||||
// Increment SP
|
||||
sp.setValue(sp.getValue() + 1);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean noOperandsValid() {
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package net.simon987.server.assembly.instruction;
|
||||
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.assembly.Instruction;
|
||||
import net.simon987.server.assembly.Register;
|
||||
import net.simon987.server.assembly.Status;
|
||||
|
||||
/**
|
||||
* Pushes the current CPU flags onto the stack.
|
||||
*/
|
||||
public class PushfInstruction extends Instruction {
|
||||
|
||||
/**
|
||||
* Opcode of the instruction
|
||||
*/
|
||||
public static final int OPCODE = 43;
|
||||
|
||||
private CPU cpu;
|
||||
|
||||
public PushfInstruction(CPU cpu) {
|
||||
super("pushf", OPCODE);
|
||||
|
||||
this.cpu = cpu;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status execute(Status status) {
|
||||
|
||||
// Decrement SP
|
||||
Register sp = cpu.getRegisterSet().getRegister("SP");
|
||||
sp.setValue(sp.getValue() - 1);
|
||||
|
||||
// Push the current flags
|
||||
cpu.getMemory().set(sp.getValue(), status.toByte());
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean noOperandsValid() {
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user