1 package com.tapina.robe.runtime.instruction;
2
3 import com.tapina.robe.runtime.Condition;
4 import com.tapina.robe.runtime.Environment;
5 import com.tapina.robe.runtime.Instruction;
6
7 import java.io.IOException;
8 import java.io.Writer;
9
10 /***
11 * Created by IntelliJ IDEA.
12 * User: gareth
13 * Date: Aug 26, 2003
14 * Time: 6:23:59 PM
15 */
16 public final class Branch extends Instruction {
17 private final int destinationOffset;
18 private final boolean link;
19
20 public Branch(Condition condition, boolean link, int offset) {
21 super(condition);
22 this.link = link;
23 this.destinationOffset = offset;
24 }
25
26 protected final boolean execute(Environment environment) {
27 final int[] R = environment.getCpu().R;
28 if (link) {
29 R[14] = R[15] - 4;
30 }
31 R[15] += destinationOffset;
32 return true;
33 }
34
35 public void dumpJavaSourceUnconditional(Writer out) throws IOException {
36 if (link) {
37 out.write(" R[14] = R[15] - 4;\n");
38 }
39 out.write("R[15] += 0x" + Integer.toHexString(destinationOffset) + "\n");
40 out.write("return;\n");
41 }
42
43 public final String toString() {
44 return "Branch offset &" + Integer.toHexString(destinationOffset + 8);
45 }
46 }