X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Ffleet%2Finterpreter%2FInterpreter.java;h=311a0aa5dd0343fd3f490241d2765a3c6e4c1c6c;hb=28e41c98c299d834bb5ec9a749126d858dc81bf0;hp=cc5650b2b81c739bf25a9c59664bbf9f672e1dd7;hpb=8e530c9973e55f91569051008eb1c330595dc40d;p=fleet.git diff --git a/src/edu/berkeley/fleet/interpreter/Interpreter.java b/src/edu/berkeley/fleet/interpreter/Interpreter.java index cc5650b..311a0aa 100644 --- a/src/edu/berkeley/fleet/interpreter/Interpreter.java +++ b/src/edu/berkeley/fleet/interpreter/Interpreter.java @@ -3,128 +3,103 @@ import java.io.*; import java.util.*; import java.util.concurrent.*; import java.lang.reflect.*; -import edu.berkeley.fleet.*; import edu.berkeley.sbp.util.ANSI; -import edu.berkeley.fleet.doc.*; import edu.berkeley.fleet.api.*; -import edu.berkeley.fleet.ies44.*; +import edu.berkeley.fleet.two.*; +import edu.berkeley.fleet.assembler.*; +import edu.berkeley.fleet.util.*; -public class Interpreter extends Fleet implements Fleet.WithDynamicShips { +public class Interpreter extends FleetTwoFleet { - /** some "halt ship" can turn this on to stop the interpreter */ - private HashMap ships = new HashMap(); - private BlockingQueue debugStream = new LinkedBlockingQueue(); + /** used to allocate serial numbers; see InterpreterDestination for further detail */ + int maxAllocatedDestinationSerialNumber = 0; - public int getWordSize() { return 37; } + private InterpreterShip debugShip = null; + private BlockingQueue debugStream = new LinkedBlockingQueue(); + private LinkedHashMap ships = new LinkedHashMap(); + public Iterator iterator() { return (Iterator)(Object)ships.values().iterator(); } + public Ship getShip(String type, int ordinal) { + for(Ship s : this) + if (s.getType().equals(type)) + if (ordinal-- <= 0) + return s; + return null; + } - public FleetProcess run(final byte[] instructions) { - try { - final FleetProcess fp = new FleetProcess() { - public void invokeInstruction(Instruction i) { throw new RuntimeException("not supported"); } - public long readWord() { - try { - return debugStream.take(); - } catch (Exception e) { - throw new RuntimeException(e); - } } - protected void _terminate() { - // FIXME: hack - ships = new HashMap(); - debugStream = new LinkedBlockingQueue(); - } - }; - new Thread() { - public void run() { - try { - // find the first icache - InterpreterShip iscratch = null; - for(Ship ship : Interpreter.this) - if (ship.getClass().getSimpleName().equals("Memory")) { - iscratch = (InterpreterShip)ship; - break; - } - if (iscratch==null) - iscratch = (InterpreterShip)Class.forName("edu.berkeley.fleet.interpreter.Memory") - .getConstructor(new Class[] { Interpreter.class, String.class }) - .newInstance(new Object[] { Interpreter.this, "memory" }); - iscratch - .getClass() - .getMethod("boot", new Class[] { byte[].class }) - .invoke(iscratch, new Object[] { instructions }); - - while(!fp.isTerminated()) - for(InterpreterShip ship : ships.values()) - for(int j=0; j<10; j++) - ship._service(); - - // run the ships a bit longer for good measure - for(int i=0; i<100; i++) - for(InterpreterShip ship : ships.values()) - for(int j=0; j<10; j++) - ship._service(); - - // check the state of the ships - for(InterpreterShip ship : ships.values()) - ship.shutdown(); - - Log.println(ANSI.yellow(" DONE: ====== FLEET is halted. Have a nice day. ======")); - - } catch (Exception e) { - if (fp.isTerminated()) return; - throw new RuntimeException(e); - } - } - }.start(); - return fp; - } catch (Exception e) { - throw new RuntimeException(e); - } + /** do not use this; it is going to go away */ + public Interpreter() { this(true); } + public Interpreter(boolean logging) { + this(new String[] { + "Debug", + "Memory", + "Memory", + "Memory", + "Alu", + "Alu", + "Alu", + "Alu", + "Alu", + "Alu", + "Alu", + "Fifo", + "Fifo", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Counter", + "Lut3", + "CarrySaveAdder", + "Rotator", + "Dvi", + "Button", + "Timer", + }, logging); } - public void dispatch(Instruction i, long address) { - Log.dispatch(i); - if (i instanceof Instruction.CodeBagDescriptor) { - Instruction.CodeBagDescriptor cbd = (Instruction.CodeBagDescriptor)i; - long absolute_cbd = ((cbd.offset+address) << 6) | cbd.size; - throw new RuntimeException(); - //new Packet(this, null, (int)absolute_cbd, (InterpreterDestination)cbd.pump).send(); - - } else if (i instanceof Instruction.UnClog) { - Instruction.UnClog ic = (Instruction.UnClog)i; - ((InstructionPump)(ic.pump)).clogged--; - - } else if (i instanceof Instruction.Kill) { - InterpreterPump pump = (InterpreterPump)(((Instruction.Kill)i).pump); - ((InstructionPump)pump).kill(); - - } else if (i instanceof Instruction.Massacre) { - InterpreterPump pump = (InterpreterPump)(((Instruction.Massacre)i).pump); - ((InstructionPump)pump).massacre(); - - } else { - InterpreterPump sourcePump = (InterpreterPump)(((Instruction)i).pump); - ((InstructionPump)sourcePump).addInstruction(((Instruction)i)); + public Interpreter(String[] ships, boolean logging) { + int i=0; + Log.quiet = !logging; + for(String s : ships) { + try { + createShip(ships[i], ships[i]+"_"+i); + } catch (Exception e) { + System.err.println(e); + } + i++; } } - public Iterator iterator() { return (Iterator)(Object)ships.values().iterator(); } - - public Ship createShip(String shipType, String shipname) { + private 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 }); - InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname }); - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship"))); - ShipDescription sd = new ShipDescription(shipname, br); - ret.setShipDescription(sd); + Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class }); + String src = "/ships/" + shipType + ".ship"; + InputStream is = getClass().getResourceAsStream(src); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + ShipDescription sd = new ShipDescription(this, shipType, br); + InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd }); ships.put(shipname, ret); + if (shipType.equals("Debug") && debugShip == null) + debugShip = ret; return ret; } catch (Exception e) { - return null; + throw new RuntimeException(e); } } - public void debug(long data) { + void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); } + void debug(BitVector data) { try { if (debugStream != null) debugStream.put(data); else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol())); @@ -135,31 +110,19 @@ public class Interpreter extends Fleet implements Fleet.WithDynamicShips { // Instruction Encoding ///////////////////////////////////////////////////////////////////////// - public final InterpreterInstructionEncoder iie = new InterpreterInstructionEncoder(); - public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); } - public Instruction readInstruction(long instr) { return iie.readInstruction(instr); } - public long writeInstruction(Instruction d) { return writeInstruction(d); } - public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); } - public class InterpreterInstructionEncoder extends InstructionEncoder { - public long getDestAddr(Destination box) { return ((InterpreterDestination)box).getDestAddr(); } - public long getBoxInstAddr(Pump box) { return ((InterpreterPump)box).getDestAddr(); } - public Destination getDestByAddr(long dest) { - for(Ship ship : Interpreter.this) - for(Pump bb : ship.getPumps()) - for(Destination d : bb.getDestinations()) - if (getDestAddr(d)==dest) - return d; - return null; - } - public Pump getBoxByInstAddr(long dest) { - for(Ship ship : Interpreter.this) - for(Pump bb : ship.getPumps()) - if (getBoxInstAddr(bb) == dest) - return bb; - return null; + public BitVector getDestAddr(Path path) { + long ret = ((InterpreterDestination)path.getDestination()).getSerialNumber(); + 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 bv; } + // ShipDescription ////////////////////////////////////////////////////////////////////////////// public void expand(ShipDescription sd) { @@ -175,38 +138,24 @@ public class Interpreter extends Fleet implements Fleet.WithDynamicShips { pw.println("package edu.berkeley.fleet.interpreter;"); pw.println("import edu.berkeley.sbp.util.ANSI;"); pw.println("import edu.berkeley.fleet.api.*;"); + pw.println("import edu.berkeley.fleet.two.*;"); pw.println("import edu.berkeley.fleet.*;"); pw.println("import java.util.*;"); pw.println("import java.io.*;"); pw.println(""); pw.println("public class "+filename+" extends InterpreterShip {"); pw.println(""); - for(PumpDescription b : sd) { + pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {"); + pw.println(" super(fleet, sd);"); + pw.println(" }"); + pw.println(""); + for(DockDescription b : sd) { String name = b.getName(); - pw.print(" "); - if ( b.isInbox()) pw.print("Inbox"); - if (!b.isInbox()) pw.print("Outbox"); - pw.print(" box_"); + pw.print(" InterpreterDock box_"); pw.print(name); - pw.print(" = new "); - if ( b.isInbox()) pw.print("Inbox"); - if (!b.isInbox()) pw.print("Outbox"); - pw.print("(this, \""+name+"\", new String[] { "); - boolean first = true; - for(String destination : b) { - if (!first) pw.print(", "); - first = false; - pw.print("\""+destination+"\""); - } - pw.println("});"); + pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));"); } pw.println(""); - pw.println(" public "+filename+"(Interpreter fleet, String name) {"); - pw.println(" super(fleet, name, \""+filename+"\");"); - for(PumpDescription b : sd) - pw.println(" addPump(\""+b.getName()+"\", box_"+b.getName()+");"); - pw.println(" }"); - pw.println(""); pw.println(sd.getSection("fleeterpreter")); pw.println("}"); pw.flush(); @@ -214,5 +163,92 @@ public class Interpreter extends Fleet implements Fleet.WithDynamicShips { } catch (Exception e) { throw new RuntimeException(e); } } -} + // Run ////////////////////////////////////////////////////////////////////////////// + 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(); + } + + } +}