add interpreter code and tests for loop counter instructions
[fleet.git] / src / edu / berkeley / fleet / interpreter / Outbox.java
index 4c82c28..85b3926 100644 (file)
@@ -1,12 +1,17 @@
 package edu.berkeley.fleet.interpreter;
 import edu.berkeley.sbp.util.ANSI;
 import edu.berkeley.fleet.api.*;
+import edu.berkeley.fleet.ies44.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.fleet.util.*;
+import static edu.berkeley.fleet.util.BitManipulations.*;
 import edu.berkeley.fleet.api.Instruction;
 
-public class Outbox extends InstructionBenkoBox {
+public class Outbox extends InstructionPump {
 
     /** are we ready to accept another item from the ship? */
-    private boolean readyForItemFromShip = true;
+    private boolean readyForDataFromShip = false;
+    private boolean haveDataFromShip     = false;
 
     /** data which has been presented by the ship and is waiting to depart */
     private long    itemPresentedByShip;
@@ -22,21 +27,37 @@ public class Outbox extends InstructionBenkoBox {
     public Outbox(InterpreterShip ship, String name) { this(ship, name, new String[] { "" }); }
     public Outbox(InterpreterShip ship, String name, String[] ports) { super(ship, name, ports); }
 
-    protected final boolean service(Instruction.Executable instruction) {
+    protected void setDataLatch(long value) { register = value; }
+    protected long peekDataLatch() { return register; }
+
+    protected final boolean service(Instruction.Executable instruction_) {
+        if (clogged>0) return false;
+        if (instruction_ instanceof Instruction.Clog) { clogged++; return true; }
+        if (instruction_ instanceof Instruction.LocalLiteral) {
+            Instruction.LocalLiteral ll = (Instruction.LocalLiteral)instruction_;
+            register =
+                ll.high
+                ? setField(36, 19, ll.literal, register)
+                : setField(18,  0, ll.literal, register);
+            return true;
+        }
+        Instruction.Move instruction = (Instruction.Move)instruction_;
 
         // if no instruction waiting, do nothing
         if (instruction == null) return false;
 
         // check firing conditions
         if (instruction.tokenIn && triggersReceived <= 0) return false;
-        if (instruction.dataIn && readyForItemFromShip) return false;
+        if (instruction.dataIn) {
+            if (!haveDataFromShip) { readyForDataFromShip = true; return false; }
+        }
         
         // consume trigger
         if (instruction.tokenIn) triggersReceived--;
 
         // consume item
         if (instruction.dataIn) {
-            readyForItemFromShip = true;
+            haveDataFromShip = false;
             if (instruction.latch) 
                 register = itemPresentedByShip;
         }
@@ -44,34 +65,53 @@ public class Outbox extends InstructionBenkoBox {
         if (instruction.dataOut) {
 
             // if item to be transmitted, send it
-            new Packet(getInterpreter(), this, register, (InterpreterDestination)instruction.dest).send();
+            InterpreterDestination dest = (InterpreterDestination)instruction.dest;
+            if (instruction.dataOutDest) {
+                // FIXME: still not supported
+                long bits = BitManipulations.getField(InstructionEncoder.OFFSET_PUMP_ADDR+InstructionEncoder.WIDTH_PUMP_ADDR-1,
+                                                      InstructionEncoder.OFFSET_PUMP_ADDR,
+                                                      register);
+                getInterpreter().dispatch(((Interpreter)getInterpreter()).iie.readInstruction(register), bits);
+                /*
+                dest = (InterpreterDestination)(((Interpreter)getInterpreter()).iie.getDestByAddr(bits));
+                if (dest == null) {
+                    if (pump != null) {
+                        
+                Pump pump = ((Interpreter)getInterpreter()).iie.getDestByInstAddr(bits);
+                    }
+                }
+                */
+                //throw new RuntimeException();
+            } else {
+                new Packet(getInterpreter(), this, register, dest).send();
+            }
             if (instruction.tokenOut)
                 throw new RuntimeException("outboxes may not send acks!");
 
         } else if (instruction.tokenOut) {
 
             // if no item was sent, we might still send an ack
-            new Packet(getInterpreter(), this, 0, (InterpreterBenkoBox)instruction.dest).send();
+            new Packet(getInterpreter(), this, 0, (InterpreterDestination)instruction.dest).send();
         }
 
         return true;
     }
 
-    public final boolean readyForItemFromShip() { return readyForItemFromShip; }
+    public final boolean readyForDataFromShip() { return readyForDataFromShip; }
     public       void addDataFromShip(long data) { addItemFromShip(data); }
-    public       boolean readyForDataFromShip() { return readyForItemFromShip(); }
     public       void addDataFromFabric(Packet packet) { triggersReceived++; }
 
     /** subclass invokes this to add an item from the ship */
     protected final void addItemFromShip(long data) {
-        if (!readyForItemFromShip)
+        if (!readyForDataFromShip)
             throw new RuntimeException("tried to add an item to an outbox which was not ready!  you have a buggy ship!");
-        readyForItemFromShip = false;
+        readyForDataFromShip = false;
+        haveDataFromShip = true;
         itemPresentedByShip = data;
     }
 
     void shutdown() {
-        if (!readyForItemFromShip)
+        if (haveDataFromShip)
             Log.println(ANSI.red(" WARNING: you left a value ("+itemPresentedByShip+") on outbox port " + this));
         if (triggersReceived > 0)
             Log.println(ANSI.red(" WARNING: you left a token on the trigger input to port " + this));