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 BlockingQueue<BitVector> debugStream = new LinkedBlockingQueue<BitVector>();
15 private InterpreterShip debugShip = null;
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 ((InterpreterDock)i.dock).addInstruction(i);
31 public Ship createShip(String shipType, String shipname) {
33 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
34 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
35 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
36 ShipDescription sd = new ShipDescription(shipType, br);
37 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
38 ships.put(shipname, ret);
39 if (shipType.equals("Debug") && debugShip == null)
42 } catch (Exception e) {
49 throw new RuntimeException();
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();
65 if (bv.length() > 1) throw new RuntimeException();
66 if (bv.length() > 0 && bv.get(0)) ret |= 1;
71 // ShipDescription //////////////////////////////////////////////////////////////////////////////
73 public void expand(ShipDescription sd) {
75 String filename = sd.getName();
76 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
77 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
78 new File(outf.getParent()).mkdirs();
79 System.err.println("writing to " + outf);
80 FileOutputStream out = new FileOutputStream(outf);
81 PrintWriter pw = new PrintWriter(out);
83 pw.println("package edu.berkeley.fleet.interpreter;");
84 pw.println("import edu.berkeley.sbp.util.ANSI;");
85 pw.println("import edu.berkeley.fleet.api.*;");
86 pw.println("import edu.berkeley.fleet.two.*;");
87 pw.println("import edu.berkeley.fleet.*;");
88 pw.println("import java.util.*;");
89 pw.println("import java.io.*;");
91 pw.println("public class "+filename+" extends InterpreterShip {");
93 for(DockDescription b : sd) {
94 String name = b.getName();
95 pw.print(" InterpreterDock box_");
97 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
100 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
101 pw.println(" super(fleet, sd);");
102 for(DockDescription b : sd)
103 pw.println(" addDock(\""+b.getName()+"\", box_"+b.getName()+");");
106 pw.println(sd.getSection("fleeterpreter"));
110 } catch (Exception e) { throw new RuntimeException(e); }
113 // Run //////////////////////////////////////////////////////////////////////////////
115 public FleetProcess run(final Instruction[] instructions) {
116 InterpreterProcess ip = new InterpreterProcess(instructions);
117 new Thread(ip).start();
121 private class InterpreterProcess extends FleetProcess implements Runnable {
122 private Instruction[] instructions;
123 public InterpreterProcess(Instruction[] instructions) {
124 this.instructions = instructions;
126 public void dispatchInstruction(Instruction i) { dispatch(i); }
127 public Dock getDebugInputDock() { return debugShip.getDock("in"); }
128 public BitVector readWord() {
130 return debugStream.take();
131 } catch (Exception e) { throw new RuntimeException(e); }
133 protected void _terminate() { }
136 while(!isTerminated())
137 for(InterpreterShip ship : ships.values())
138 for(int j=0; j<10; j++)
140 for(InterpreterShip ship : ships.values())
143 } catch (Exception e) {
144 if (isTerminated()) return;
145 throw new RuntimeException(e);