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 /** do not use this; it is going to go away */
27 public Interpreter() {
30 public Interpreter(String[] ships, boolean logging) {
33 for(String s : ships) {
34 createShip(ships[i], ships[i]+"_"+i);
39 void dispatch(Instruction i) {
41 long il = writeInstruction(i, debugShip.getDock("in"));
42 Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
43 new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
46 /** do not use this; it is going to go away */
47 public Ship createShip(String shipType, String shipname) {
49 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
50 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
51 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
52 ShipDescription sd = new ShipDescription(shipType, br);
53 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
54 ships.put(shipname, ret);
55 if (shipType.equals("Debug") && debugShip == null)
58 } catch (Exception e) {
64 void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
65 void debug(BitVector data) {
67 if (debugStream != null) debugStream.put(data);
68 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
69 } catch (Exception e) {
70 throw new RuntimeException(e);
74 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
76 public long getDestAddr(Path path) {
77 long ret = ((InterpreterDestination)path.getDestination()).addr;
78 BitVector bv = path.getSignal();
80 if (bv.length() > 1) throw new RuntimeException("signal was " + bv.length() + " bits long!");
81 if (bv.length() > 0 && bv.get(0)) ret |= 1;
87 // ShipDescription //////////////////////////////////////////////////////////////////////////////
89 public void expand(ShipDescription sd) {
91 String filename = sd.getName();
92 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
93 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
94 new File(outf.getParent()).mkdirs();
95 System.err.println("writing to " + outf);
96 FileOutputStream out = new FileOutputStream(outf);
97 PrintWriter pw = new PrintWriter(out);
99 pw.println("package edu.berkeley.fleet.interpreter;");
100 pw.println("import edu.berkeley.sbp.util.ANSI;");
101 pw.println("import edu.berkeley.fleet.api.*;");
102 pw.println("import edu.berkeley.fleet.two.*;");
103 pw.println("import edu.berkeley.fleet.*;");
104 pw.println("import java.util.*;");
105 pw.println("import java.io.*;");
107 pw.println("public class "+filename+" extends InterpreterShip {");
109 for(DockDescription b : sd) {
110 String name = b.getName();
111 pw.print(" InterpreterDock box_");
113 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
116 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
117 pw.println(" super(fleet, sd);");
118 for(DockDescription b : sd)
119 pw.println(" addDock(\""+b.getName()+"\", box_"+b.getName()+");");
122 pw.println(sd.getSection("fleeterpreter"));
126 } catch (Exception e) { throw new RuntimeException(e); }
129 // Run //////////////////////////////////////////////////////////////////////////////
131 public FleetProcess run(final Instruction[] instructions) {
132 InterpreterProcess ip = new InterpreterProcess(instructions);
133 new Thread(ip).start();
137 private class InterpreterProcess extends FleetProcess implements Runnable {
138 private Instruction[] instructions;
139 public InterpreterProcess(Instruction[] instructions) {
140 this.instructions = instructions;
141 for(Instruction i : instructions)
142 dispatchInstruction(i);
144 public void dispatchInstruction(Instruction i) { dispatch(i); }
145 public Dock getDebugInputDock() { return debugShip.getDock("in"); }
146 public BitVector readWord() {
148 return debugStream.take();
149 } catch (Exception e) { throw new RuntimeException(e); }
151 protected void _terminate() { }
154 while(!isTerminated())
155 for(InterpreterShip ship : ships.values())
156 for(int j=0; j<10; j++)
158 for(InterpreterShip ship : ships.values())
161 } catch (Exception e) {
162 if (isTerminated()) return;
163 throw new RuntimeException(e);