checkpoint
[fleet.git] / ships / Alu2.ship
index 94e853b..db76145 100644 (file)
@@ -12,7 +12,6 @@ ADD: add the two arguments; treat link as carry
 SUB: subtract the two arguments; treat link as carry
 MAX:
 MIN:
-SORT: output min(in1,in2) followed by max(in1,in2) (FIXME: redundant?)
 
 == TeX ==============================================================
 This ship is a two-input arithmetic unit.  It features several
@@ -23,32 +22,39 @@ FIXME: implement all the link bit stuff
 
 Use carry-in bit to create a selector?  Perhaps a waste of an ALU.
 
-Carry-save / carry completion stuff.
-
 Flags: zero, negative, overflow, ?
 
+\begin{verbatim}
 move elsewhere:
 //MUL:
 //DIV:
 //MOD:
+\end{verbatim}
 
 == Fleeterpreter ====================================================
+public long resolveLiteral(String literal) {
+  if (literal.equals("ADD")) return 0;
+  if (literal.equals("SUB")) return 1;
+  if (literal.equals("MAX")) return 2;
+  if (literal.equals("MIN")) return 3;
+  return super.resolveLiteral(literal);
+}
 public void service() {
   if (box_in1.dataReadyForShip() &&
       box_in2.dataReadyForShip() &&
       box_inOp.dataReadyForShip() &&
-      box_out.readyForItemFromShip()) {
-      int a      = box_in1.removeDataForShip();
-      int b      = box_in2.removeDataForShip();
-      int op     = box_inOp.removeDataForShip();
-      switch(op) {
+      box_out.readyForDataFromShip()) {
+      long a      = box_in1.removeDataForShip();
+      long b      = box_in2.removeDataForShip();
+      long op     = box_inOp.removeDataForShip();
+      switch((int)op) {
           case 0: box_out.addDataFromShip(a+b); // ADD
               break;
           case 1: box_out.addDataFromShip(a-b); // SUB
               break;
-          case 2: box_out.addDataFromShip(Math.max(a,b)); // ADD
+          case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
               break;
-          case 3: box_out.addDataFromShip(Math.min(a,b)); // SUB
+          case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
               break;
           default: box_out.addDataFromShip(0);
               break;
@@ -94,6 +100,36 @@ public void service() {
     end
   end
 
+== Test ==============================================================================
+// expected output
+#ship debug : Debug
+#ship alu   : Alu2
+
+#expect 17
+#expect 1
+#expect 8
+#expect 9
+
+debug.in:   [*] take, deliver;
+9:          sendto alu.in1;
+9:          sendto alu.in1;
+9:          sendto alu.in1;
+9:          sendto alu.in1;
+8:          sendto alu.in2;
+8:          sendto alu.in2;
+8:          sendto alu.in2;
+8:          sendto alu.in2;
+
+Alu2.ADD:   sendto alu.inOp;
+Alu2.SUB:   sendto alu.inOp;
+Alu2.MIN:   sendto alu.inOp;
+Alu2.MAX:   sendto alu.inOp;
+
+alu.in1:    [*] take, deliver;
+alu.in2:    [*] take, deliver;
+alu.inOp:   [*] take, deliver;
+alu.out:    [*] take, sendto debug.in;
+