store .ship files in fleet.jar
[fleet.git] / src / edu / berkeley / fleet / interpreter / Interpreter.java
index 2109085..d09b3d0 100644 (file)
@@ -18,11 +18,24 @@ public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynami
     public Ship getShip(String type, int ordinal) {
         for(Ship s : this)
             if (s.getType().equals(type))
-                if (ordinal-- < 0)
+                if (ordinal-- <= 0)
                     return s;
         return null;
     }
 
+    /** do not use this; it is going to go away */
+    public Interpreter() {
+    }
+
+    public Interpreter(String[] ships, boolean logging) {
+        int i=0;
+        Log.quiet = !logging;
+        for(String s : ships) {
+            createShip(ships[i], ships[i]+"_"+i);
+            i++;
+        }
+    }
+
     void dispatch(Instruction i) {
         Log.dispatch(i);
         long il = writeInstruction(i, debugShip.getDock("in"));
@@ -30,11 +43,15 @@ public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynami
         new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
     }
 
+    /** do not use this; it is going to go away */
     public Ship createShip(String shipType, String shipname) {
         try {
+            if (ships.get(shipname)!=null) return ships.get(shipname);
             Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
             Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
-            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
+            String src = "/ships/" + shipType + ".ship";
+            InputStream is = getClass().getResourceAsStream(src);
+            BufferedReader br = new BufferedReader(new InputStreamReader(is));
             ShipDescription sd = new ShipDescription(shipType, br);
             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
             ships.put(shipname, ret);
@@ -59,14 +76,16 @@ public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynami
 
     // Instruction Encoding /////////////////////////////////////////////////////////////////////////
 
-    public long getDestAddr(Path path) {
+    public BitVector getDestAddr(Path path) {
         long ret = ((InterpreterDestination)path.getDestination()).addr;
-        BitVector bv = path.getSignal();
-        if (bv != null) {
-            if (bv.length() > 1) throw new RuntimeException("signal was " + bv.length() + " bits long!");
-            if (bv.length() > 0 && bv.get(0)) ret |= 1;
+        BitVector sig = path.getSignal();
+        BitVector bv = new BitVector(DISPATCH_PATH.valmaskwidth+1);
+        bv.set(ret);
+        if (sig != null) {
+            if (sig.length() > 1) throw new RuntimeException("signal was " + sig.length() + " bits long!");
+            if (sig.length() > 0 && sig.get(0)) bv.set(bv.length()-1,true);
         }
-        return ret;
+        return bv;
     }
 
 
@@ -115,21 +134,31 @@ public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynami
     // Run //////////////////////////////////////////////////////////////////////////////
 
     public FleetProcess run(final Instruction[] instructions) {
-        InterpreterProcess ip = new InterpreterProcess(instructions);
+        InterpreterProcess ip = initialize(instructions);
         new Thread(ip).start();
         return ip;
     }
 
-    private class InterpreterProcess extends FleetProcess implements Runnable {
+    public InterpreterProcess initialize(Instruction[] instr) {
+        return new InterpreterProcess(instr);
+    }
+
+    public class InterpreterProcess extends FleetProcess implements Runnable {
         private Instruction[] instructions;
+        public void flush() { }
+        public void sendWord(Destination d, BitVector word) {
+            throw new RuntimeException("not implemented");
+        }
+        public void sendToken(Destination d) { throw new RuntimeException("not implemented"); }
         public InterpreterProcess(Instruction[] instructions) {
             this.instructions = instructions;
             for(Instruction i : instructions)
-                dispatchInstruction(i);
+                sendInstruction(i);
         }
-        public void dispatchInstruction(Instruction i) { dispatch(i); }
+        public Fleet getFleet() { return Interpreter.this; }
+        public void sendInstruction(Instruction i) { dispatch(i); }
         public Dock getDebugInputDock() { return debugShip.getDock("in"); }
-        public BitVector readWord() {
+        public BitVector recvWord() {
             try {
                 return debugStream.take();
             } catch (Exception e) { throw new RuntimeException(e); }
@@ -149,5 +178,14 @@ public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynami
                 throw new RuntimeException(e);
             }
         }
+
+        public void step(Dock d) {
+            ((InterpreterDock)d).service();
+        }
+        
+        public void step(Ship s) {
+            ((InterpreterShip)s).service();
+        }
+
     }
-}
\ No newline at end of file
+}