+ public FleetProcess run(final Instruction[] instructions) {
+ InterpreterProcess ip = initialize(instructions);
+ Thread ipt = new Thread(ip);
+ ipt.setDaemon(true);
+ ipt.start();
+ return ip;
+ }
+
+ public InterpreterProcess initialize(Instruction[] instr) {
+ return new InterpreterProcess(instr);
+ }
+
+ public class InterpreterProcess extends FleetProcess implements Runnable {
+ private Instruction[] instructions;
+ 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));
+ new Packet(path, new BitVector(getWordWidth()), true).send();
+ }
+ public InterpreterProcess(Instruction[] instructions) {
+ this.instructions = instructions;
+ for(Instruction i : instructions)
+ sendInstruction(i);
+ }
+ public Fleet getFleet() { return Interpreter.this; }
+ 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 {
+ return debugStream.take();
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+ protected void _terminate() { }
+ public void run() {
+ try {
+ long lastStatus = System.currentTimeMillis();
+ while(!isTerminated()) {
+ flush();
+ long now = System.currentTimeMillis();
+ if (now - lastStatus > 2000 && !Log.quiet &&
+ !"false".equals(System.getProperty("fleet.log.state","false"))) {
+ System.out.println("== State Dump ===================================================");
+ for(InterpreterShip ship : ships.values())
+ for(Dock d : ship)
+ synchronized(this) {
+ ((InterpreterDock)d).dumpState();
+ }
+ lastStatus = now;
+ }
+ }
+ for(InterpreterShip ship : ships.values())
+ ship.reset();
+ debugStream.clear();
+ } catch (Exception e) {
+ if (isTerminated()) return;
+ throw new RuntimeException(e);
+ }
+ }
+
+ 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();
+ }
+
+ public synchronized void step(Ship s) {
+ ((InterpreterShip)s).service();
+ }
+
+ }
+}