- 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() {
- shiplist = new ArrayList<InterpreterShip>();
- ships = new HashMap<String,InterpreterShip>();
- debugStream = new LinkedBlockingQueue<Long>();
- mem = new int[0];
- }
- };
- new Thread() {
- public void run() {
- try {
- go(fp, instructions);
- } catch (Exception e) {
- if (fp.isTerminated()) return;
- throw new RuntimeException(e);
- }
- }
- }.start();
- return fp;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public void go(FleetProcess fp, byte[] instructions) {
- try {
- // find the first icache
- InterpreterShip iscratch = null;
- for(Ship ship : this)
- if (ship.getClass().getSimpleName().equals("Iscratch")) {
- iscratch = (InterpreterShip)ship;
- break;
- }
- if (iscratch==null)
- iscratch = (InterpreterShip)Class.forName("edu.berkeley.fleet.interpreter.ships.Iscratch")
- .getConstructor(new Class[] { Interpreter.class, String.class })
- .newInstance(new Object[] { this, "iscratch" });
- iscratch
- .getClass()
- .getMethod("boot", new Class[] { byte[].class })
- .invoke(iscratch, new Object[] { instructions });
-
- while(!halt && !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(Log.yellow(" DONE: ====== FLEET is halted. Have a nice day. ======"));
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public void dispatch(Instruction i, long address) {
- Log.dispatch(i);
- if (i instanceof Instruction.Executable) {
- InterpreterBenkoBox sourceBenkoBox = (InterpreterBenkoBox)(((Instruction.Executable)i).benkoBox);
- ((InstructionPort)sourceBenkoBox).addInstruction(((Instruction.Executable)i));
-
- } else if (i instanceof Instruction.Literal.CodeBagDescriptor) {
- Instruction.Literal.CodeBagDescriptor cbd = (Instruction.Literal.CodeBagDescriptor)i;
- InterpreterBenkoBox destBenkoBox = (InterpreterBenkoBox)(cbd.dest);
- long absolute_cbd = ((cbd.offset+address) << 6) | cbd.size;
- destBenkoBox.addDataFromFabric((int)absolute_cbd);
-
- } else if (i instanceof Instruction.Literal.Absolute) {
- InterpreterBenkoBox destBenkoBox = (InterpreterBenkoBox)(((Instruction.Literal.Absolute)i).dest);
- Log.data(((Instruction.Literal.Absolute)i).value+"", null, destBenkoBox);
- destBenkoBox.addDataFromFabric((int)((Instruction.Literal.Absolute)i).value);