Interpreter: use Packet.send() more often
[fleet.git] / src / edu / berkeley / fleet / interpreter / Interpreter.java
index 50bcc20..5f10626 100644 (file)
@@ -60,6 +60,7 @@ public class Interpreter extends FleetTwoFleet {
                 "Lut3",
                 "CarrySaveAdder",
                 "Rotator",
+                "Timer",
             }, logging);
     }
 
@@ -72,13 +73,6 @@ public class Interpreter extends FleetTwoFleet {
         }
     }
 
-    void dispatch(Instruction i) {
-        Log.dispatch(i);
-        long il = writeInstruction(i, debugShip.getDock("in"));
-        Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
-        new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
-    }
-
     private Ship createShip(String shipType, String shipname) {
         try {
             if (ships.get(shipname)!=null) return ships.get(shipname);
@@ -87,7 +81,7 @@ public class Interpreter extends FleetTwoFleet {
             String src = "/ships/" + shipType + ".ship";
             InputStream is = getClass().getResourceAsStream(src);
             BufferedReader br = new BufferedReader(new InputStreamReader(is));
-            ShipDescription sd = new ShipDescription(shipType, br);
+            ShipDescription sd = new ShipDescription(this, shipType, br);
             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
             ships.put(shipname, ret);
             if (shipType.equals("Debug") && debugShip == null)
@@ -168,7 +162,9 @@ public class Interpreter extends FleetTwoFleet {
 
     public FleetProcess run(final Instruction[] instructions) {
         InterpreterProcess ip = initialize(instructions);
-        new Thread(ip).start();
+        Thread ipt = new Thread(ip);
+        ipt.setDaemon(true);
+        ipt.start();
         return ip;
     }
 
@@ -178,16 +174,14 @@ public class Interpreter extends FleetTwoFleet {
 
     public class InterpreterProcess extends FleetProcess implements Runnable {
         private Instruction[] instructions;
-        public void flush() { }
-        public synchronized void sendWord(Destination d, BitVector word) {
-            InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
-            ((InterpreterDestination)d).
-                addDataFromFabric(new Packet(path, word, false));
+        public synchronized void sendWord(Destination d, BitVector word) { sendWord(d, word, null); }
+        public synchronized void sendWord(Destination d, BitVector word, BitVector signal) {
+            InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, signal==null?new BitVector(1):signal);
+            new Packet(path, word, false).send();
         }
         public synchronized void sendToken(Destination d) {
             InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
-            ((InterpreterDestination)d).
-                addDataFromFabric(new Packet(path, new BitVector(getWordWidth()), true));
+            new Packet(path, new BitVector(getWordWidth()), true).send();
         }
         public InterpreterProcess(Instruction[] instructions) {
             this.instructions = instructions;
@@ -195,7 +189,11 @@ public class Interpreter extends FleetTwoFleet {
                 sendInstruction(i);
         }
         public Fleet getFleet() { return Interpreter.this; }
-        public void sendInstruction(Instruction i) { dispatch(i); }
+        public synchronized void sendInstruction(Instruction i) {
+            long il = writeInstruction(i, debugShip.getDock("in"));
+            Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
+            new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
+        }
         public Dock getDebugInputDock() { return debugShip.getDock("in"); }
         public BitVector recvWord() {
             try {
@@ -206,11 +204,7 @@ public class Interpreter extends FleetTwoFleet {
         public void run() {
             try {
                 while(!isTerminated()) {
-                    for(InterpreterShip ship : ships.values())
-                        for(int j=0; j<10; j++)
-                            synchronized(this) {
-                                ship._service();
-                            }
+                    flush();
                 }
                 for(InterpreterShip ship : ships.values())
                     ship.reset();
@@ -221,6 +215,16 @@ public class Interpreter extends FleetTwoFleet {
             }
         }
 
+        public void flush() {
+            // FIXME: should this run until we detect some sort of "quiescence"?  OTOH that might never happen.
+            for(InterpreterShip ship : ships.values())
+                for(int j=0; j<10; j++)
+                    if (!isTerminated())
+                        synchronized(this) {
+                            ship._service();
+                        }
+        }
+
         public synchronized void step(Dock d) {
             ((InterpreterDock)d).service();
         }