1 package edu.berkeley.fleet.interpreter;
4 import java.util.concurrent.*;
5 import java.lang.reflect.*;
6 import edu.berkeley.sbp.util.ANSI;
7 import edu.berkeley.fleet.api.*;
8 import edu.berkeley.fleet.two.*;
9 import edu.berkeley.fleet.assembler.*;
10 import edu.berkeley.fleet.util.*;
12 public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynamicShips {
14 private InterpreterShip debugShip = null;
15 private BlockingQueue<BitVector> debugStream = new LinkedBlockingQueue<BitVector>();
16 private HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
17 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
18 public Ship getShip(String type, int ordinal) {
20 if (s.getType().equals(type))
26 void dispatch(Instruction i) {
28 long il = writeInstruction(i, debugShip.getDock("in"));
29 Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
30 new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
33 public Ship createShip(String shipType, String shipname) {
35 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
36 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
37 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
38 ShipDescription sd = new ShipDescription(shipType, br);
39 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
40 ships.put(shipname, ret);
41 if (shipType.equals("Debug") && debugShip == null)
44 } catch (Exception e) {
50 void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
51 void debug(BitVector data) {
53 if (debugStream != null) debugStream.put(data);
54 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
55 } catch (Exception e) {
56 throw new RuntimeException(e);
60 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
62 public long getDestAddr(Path path) {
63 long ret = ((InterpreterDestination)path.getDestination()).addr;
64 BitVector bv = path.getSignal();
66 if (bv.length() > 1) throw new RuntimeException("signal was " + bv.length() + " bits long!");
67 if (bv.length() > 0 && bv.get(0)) ret |= 1;
73 // ShipDescription //////////////////////////////////////////////////////////////////////////////
75 public void expand(ShipDescription sd) {
77 String filename = sd.getName();
78 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
79 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
80 new File(outf.getParent()).mkdirs();
81 System.err.println("writing to " + outf);
82 FileOutputStream out = new FileOutputStream(outf);
83 PrintWriter pw = new PrintWriter(out);
85 pw.println("package edu.berkeley.fleet.interpreter;");
86 pw.println("import edu.berkeley.sbp.util.ANSI;");
87 pw.println("import edu.berkeley.fleet.api.*;");
88 pw.println("import edu.berkeley.fleet.two.*;");
89 pw.println("import edu.berkeley.fleet.*;");
90 pw.println("import java.util.*;");
91 pw.println("import java.io.*;");
93 pw.println("public class "+filename+" extends InterpreterShip {");
95 for(DockDescription b : sd) {
96 String name = b.getName();
97 pw.print(" InterpreterDock box_");
99 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
102 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
103 pw.println(" super(fleet, sd);");
104 for(DockDescription b : sd)
105 pw.println(" addDock(\""+b.getName()+"\", box_"+b.getName()+");");
108 pw.println(sd.getSection("fleeterpreter"));
112 } catch (Exception e) { throw new RuntimeException(e); }
115 // Run //////////////////////////////////////////////////////////////////////////////
117 public FleetProcess run(final Instruction[] instructions) {
118 InterpreterProcess ip = new InterpreterProcess(instructions);
119 new Thread(ip).start();
123 private class InterpreterProcess extends FleetProcess implements Runnable {
124 private Instruction[] instructions;
125 public InterpreterProcess(Instruction[] instructions) {
126 this.instructions = instructions;
127 for(Instruction i : instructions)
128 dispatchInstruction(i);
130 public void dispatchInstruction(Instruction i) { dispatch(i); }
131 public Dock getDebugInputDock() { return debugShip.getDock("in"); }
132 public BitVector readWord() {
134 return debugStream.take();
135 } catch (Exception e) { throw new RuntimeException(e); }
137 protected void _terminate() { }
140 while(!isTerminated())
141 for(InterpreterShip ship : ships.values())
142 for(int j=0; j<10; j++)
144 for(InterpreterShip ship : ships.values())
147 } catch (Exception e) {
148 if (isTerminated()) return;
149 throw new RuntimeException(e);