1 package edu.berkeley.fleet.interpreter.ships;
2 import edu.berkeley.fleet.interpreter.*;
3 import edu.berkeley.fleet.*;
9 You should note the following:
10 I haven't implemented all the link-out policies.
11 You can give the SHIP a single cmd with up to four operations. For
12 example, "ADD ZERO 0 SUBTRACT SIGN 1 MAX MAX 0". These will be done
13 in left-to-right order.
14 You can use any link-out policy with any operation, though some don't
19 * @author Dominic Antonelli <dantonel@berkeley.edu>
21 public class ArithmeticShip extends InterpreterShip {
25 DataInbox A = new DataInbox(this, "A");
26 DataInbox B = new DataInbox(this, "B");
27 DataInbox cmd = new DataInbox(this, "cmd");
28 DataOutbox out = new DataOutbox(this, "out");
30 public ArithmeticShip(Interpreter fleet, String name) {
34 public enum Operation {
35 // NOTE: NOP is not to be used, but rather simply prevents other ops from using opcode 0
36 // This is so that we can detect the "end" of a command - when the command is all zero
37 NOP { int eval(int a, int b, int link) { System.out.println("ERROR: Arith_NOP\n"); return 0; } },
38 ADD { int eval(int a, int b, int link) { return a + b + link; } },
39 SUBTRACT { int eval(int a, int b, int link) { return a - b + link; } },
40 COPY_A { int eval(int a, int b, int link) { return a + link; } },
41 COPY_B { int eval(int a, int b, int link) { return b + link; } },
42 SELECT { int eval(int a, int b, int link) { return ((link==1) ? b : a); } },
43 NEGATE_A { int eval(int a, int b, int link) { return -(a+link); } },
44 NEGATE_B { int eval(int a, int b, int link) { return -(b+link); } },
45 ABS_A { int eval(int a, int b, int link) { a+=link; return (a<0 ? -a : a); } },
46 ABS_B { int eval(int a, int b, int link) { b+=link; return (b<0 ? -b : b); } },
47 MAX { int eval(int a, int b, int link) { return (a>b ? a : b) + link; } },
48 MIN { int eval(int a, int b, int link) { return (a<b ? a : b) + link; } },
49 INC_A { int eval(int a, int b, int link) { return a+1+link; } },
50 INC_B { int eval(int a, int b, int link) { return b+1+link; } },
51 RESERVED1 { int eval(int a, int b, int link) { System.out.println("ERROR: Arith_RESERVED0\n"); return 0; } },
52 RESERVED2 { int eval(int a, int b, int link) { System.out.println("ERROR: Arith_RESERVED1\n"); return 0; } };
56 // Do arithmetic op represented by this constant
57 abstract int eval(int a, int b, int link);
58 public static Operation convertInt( int i ) {
63 public enum LinkOutPolicy {
64 LINK_IN, ZERO, CARRY, SIGN, OLD_SIGN, OVERFLOW, MAX, MIN;
65 public static LinkOutPolicy convertInt( int i ) {
70 private long savedCommand = 0;
72 public void service() {
73 if (!out.readyForDataFromShip()) return;
74 if (!A.dataReadyForShip()) return;
75 if (!B.dataReadyForShip()) return;
79 if (savedCommand != 0) {
80 command = savedCommand;
81 } else if (!cmd.dataReadyForShip()) {
84 command = cmd.removeDataForShip();
87 int inA = A.removeDataForShip();
88 int inB = B.removeDataForShip();
89 int linkInPolicy = (int)(command & 0x1);
90 LinkOutPolicy linkOutPolicy = LinkOutPolicy.convertInt((int)((command >> 1) & 0x7));
91 Operation op = Operation.convertInt((int)((command >> 4) & 0xf));
95 if (op == Operation.SELECT) {
96 link = link ^ linkInPolicy; // LinkInPolicy = 1 mean flip the selection for the SELECT op.
98 link = link & linkInPolicy; // If linkInPolicy is zero, unset the link bit for the upcoming computation.
99 // NOTE: The final value of link will be computed after eval.
102 result = op.eval(inA, inB, link);
104 switch (linkOutPolicy) {
109 link = (result == 0) ? 1 : 0;
113 link = (result >> 31) & 0x1;
116 if (inA > inB) link = 1; else link = 0;
119 if (inA < inB) link = 1; else link = 0;
124 System.out.println("ERROR: non-implemented linkOutPolicy selected");
127 System.out.println("ERROR: Unknown linkOutPolicy selected");
130 out.addDataFromShip(result);
132 savedCommand = command >> 8;
134 // System.out.println("Link is now " + link);
137 public int resolveShipSpecificConstant(String shipSpecificData) {
138 String[] data = shipSpecificData.split("\\s");
141 if ((data.length % 3) != 0) {
142 System.out.println("ERROR: ArithmeticShip received invalid ShipSpecificConstant");
144 for (int i = data.length - 1; i >= 2; i-=3) {
146 if (data[i].equals("1")) {
149 for (LinkOutPolicy policy : LinkOutPolicy.values()) {
150 if (data[i-1].equals(policy.toString())) {
151 result |= (policy.ordinal() << 1);
154 for (Operation op : Operation.values()) {
155 if (data[i-2].equals(op.toString())) {
156 result |= (op.ordinal() << 4);