-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add JAB instruction & MOV labels support
This commit adds two changes: - Absolute Jump instruction (JAB). It receives one parameter: either a target or a fixed number. Contrary to MOV <SRC> PC which effectively jumps to PC+1, JAB jumps __exactly__ to PC. - MOV is now able to operate on labels, treating them as PC indices corresponding to their location. These two changes allow for simpler function implementations, see an example function that multiplies input arg by two: Before this commit: ``` #DEFINE KEYPAD DOWN #DEFINE STACK UP BEGIN: MOV KEYPAD ACC SAV MOV PC ACC ADD 5 MOV ACC STACK SWP MOV ACC STACK JMP MUL2 MOV STACK ACC JMP BEGIN MUL2: MOV STACK ACC MUL 2 SAV MOV STACK ACC SWP MOV ACC STACK SWP SUB PC JRO ACC ``` After: ``` #DEFINE KEYPAD DOWN #DEFINE STACK UP BEGIN: MOV KEYPAD STACK MOV MUL2_RET STACK JMP MUL2 MUL2_RET: MOV STACK ACC JMP BEGIN MUL2: MOV STACK ACC SAV MOV STACK ACC MUL 2 MOV ACC STACK SWP JAB ACC ```
- Loading branch information
Showing
6 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...va/li/cil/tis3d/common/module/execution/instruction/JumpAbsoluteImmediateInstruction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package li.cil.tis3d.common.module.execution.instruction; | ||
|
||
import li.cil.tis3d.common.module.execution.Machine; | ||
|
||
public class JumpAbsoluteImmediateInstruction implements Instruction { | ||
private final short pc; | ||
|
||
public JumpAbsoluteImmediateInstruction(final short pc) { | ||
this.pc = pc; | ||
} | ||
|
||
@Override | ||
public void step(final Machine machine) { | ||
machine.getState().pc = pc; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return JumpAbsoluteInstruction.NAME + " " + pc; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...c/main/java/li/cil/tis3d/common/module/execution/instruction/JumpAbsoluteInstruction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package li.cil.tis3d.common.module.execution.instruction; | ||
|
||
import li.cil.tis3d.common.module.execution.Machine; | ||
import li.cil.tis3d.common.module.execution.target.Target; | ||
import li.cil.tis3d.common.module.execution.target.TargetInterface; | ||
|
||
public class JumpAbsoluteInstruction implements Instruction { | ||
public static final String NAME = "JAB"; | ||
|
||
private final Target source; | ||
|
||
public JumpAbsoluteInstruction(final Target source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public void step(final Machine machine) { | ||
final TargetInterface sourceInterface = machine.getInterface(source); | ||
|
||
if (!sourceInterface.isReading()) { | ||
sourceInterface.beginRead(); | ||
} | ||
if (sourceInterface.canTransfer()) { | ||
machine.getState().pc = sourceInterface.read(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return NAME + " " + source; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/main/java/li/cil/tis3d/common/module/execution/instruction/MoveLabelInstruction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package li.cil.tis3d.common.module.execution.instruction; | ||
|
||
import li.cil.tis3d.common.module.execution.Machine; | ||
import li.cil.tis3d.common.module.execution.target.Target; | ||
import li.cil.tis3d.common.module.execution.target.TargetInterface; | ||
|
||
public class MoveLabelInstruction extends AbstractMoveInstruction { | ||
private final String label; | ||
|
||
public MoveLabelInstruction(final String label, final Target destination) { | ||
super(destination); | ||
this.label = label; | ||
} | ||
|
||
@Override | ||
public void step(final Machine machine) { | ||
final TargetInterface destinationInterface = machine.getInterface(destination); | ||
int addr = machine.getState().labels.get(label); | ||
|
||
if (!destinationInterface.isWriting()) { | ||
if (destinationInterface.beginWrite((short) addr)) { | ||
machine.getState().pc++; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoveInstruction.NAME + " " + label + " " + destination; | ||
} | ||
} |