mild overhaul of Interpreter; now capable of performing MergeSort
authorAdam Megacz <adam@megacz.com>
Sun, 30 Aug 2009 00:18:22 +0000 (17:18 -0700)
committerAdam Megacz <adam@megacz.com>
Sun, 30 Aug 2009 00:18:22 +0000 (17:18 -0700)
src/edu/berkeley/fleet/interpreter/DebugDock.java
src/edu/berkeley/fleet/interpreter/InterpreterDock.java
src/edu/berkeley/fleet/interpreter/InterpreterShip.java

index 2ab6fc4..36ffa66 100644 (file)
@@ -21,7 +21,7 @@ public class DebugDock {
     public boolean getDataReadyForShip()        { return dock.dataReadyForShip; }
     public boolean getReadyForDataFromShip()    { return dock.readyForDataFromShip;}
     public long getDataFromShip()               { return dock.dataFromShip; }
     public boolean getDataReadyForShip()        { return dock.dataReadyForShip; }
     public boolean getReadyForDataFromShip()    { return dock.readyForDataFromShip;}
     public long getDataFromShip()               { return dock.dataFromShip; }
-    public boolean getTorpedoWaiting()          { return dock.torpedoWaiting; }
+    public boolean getTorpedoWaiting()          { throw new RuntimeException("obsolete"); }
     public boolean isHatchOpen()                { return !dock.requeueStageInCirculatingState; }
     public boolean getFlagA()                   { return dock.flag_a; }
     public boolean getFlagB()                   { return dock.flag_b; }
     public boolean isHatchOpen()                { return !dock.requeueStageInCirculatingState; }
     public boolean getFlagA()                   { return dock.flag_a; }
     public boolean getFlagB()                   { return dock.flag_b; }
index 3cacdf1..5c7e9ec 100644 (file)
@@ -18,15 +18,13 @@ class InterpreterDock extends FleetTwoDock {
     final BitVector dataLatch = new BitVector(getShip().getFleet().getWordWidth());
     InterpreterPath pathLatch = null;
     boolean         requeueStageInCirculatingState = false;
     final BitVector dataLatch = new BitVector(getShip().getFleet().getWordWidth());
     InterpreterPath pathLatch = null;
     boolean         requeueStageInCirculatingState = false;
-    boolean         requeueStageHasTailInstruction = false;
-    boolean         torpedoWaiting = false;
     boolean         flushing = false;
 
     LinkedList<Instruction> instructions = new LinkedList<Instruction>();
     LinkedList<Packet> dataPackets = new LinkedList<Packet>();
 
     // HACK
     boolean         flushing = false;
 
     LinkedList<Instruction> instructions = new LinkedList<Instruction>();
     LinkedList<Packet> dataPackets = new LinkedList<Packet>();
 
     // HACK
-    private LinkedList<Instruction> instructionsBackedUpIntoSwitchFabric = new LinkedList<Instruction>();
+    private LinkedList<Packet> instructionsBackedUpIntoSwitchFabric = new LinkedList<Packet>();
 
     boolean dataReadyForShip = false;
     boolean readyForDataFromShip = true;
 
     boolean dataReadyForShip = false;
     boolean readyForDataFromShip = true;
@@ -45,13 +43,11 @@ class InterpreterDock extends FleetTwoDock {
         dataLatch.set(0);
         pathLatch = null;
         requeueStageInCirculatingState = false;
         dataLatch.set(0);
         pathLatch = null;
         requeueStageInCirculatingState = false;
-        requeueStageHasTailInstruction = false;
         instructions.clear();
         dataPackets.clear();
         instructionsBackedUpIntoSwitchFabric.clear();
         dataReadyForShip = false;
         readyForDataFromShip = true;
         instructions.clear();
         dataPackets.clear();
         instructionsBackedUpIntoSwitchFabric.clear();
         dataReadyForShip = false;
         readyForDataFromShip = true;
-        torpedoWaiting = false;
         flushing = false;
     }
 
         flushing = false;
     }
 
@@ -60,20 +56,7 @@ class InterpreterDock extends FleetTwoDock {
     /** includes the epilogue fifo */
     public InterpreterDestination instructionDestination = new InterpreterDestination(this) {
             public String toString() { return getDock()+":i"; }
     /** includes the epilogue fifo */
     public InterpreterDestination instructionDestination = new InterpreterDestination(this) {
             public String toString() { return getDock()+":i"; }
-            public void addDataFromFabric(Packet p) {
-                if (p.isToken()) {
-                    if (instructionsBackedUpIntoSwitchFabric.size()!=0)
-                        throw new RuntimeException("torpedo arrived while instructions were backed up into switch fabric");
-                    if (torpedoWaiting) throw new RuntimeException("two torpedoes collided at dock "+this);
-                    torpedoWaiting = true;
-                    return;
-                }
-
-                Instruction inst =
-                    getInterpreter().decodeInstruction(p.getValue(),
-                                                       InterpreterDock.this /* this is wrong, but harmless */);
-                addInstruction(inst);
-            }
+            public void addDataFromFabric(Packet p) { addInstruction(p); }
         };
     public InterpreterDestination dataDestination = new InterpreterDestination(this) {
             public String toString() { return getDock()+""; }
         };
     public InterpreterDestination dataDestination = new InterpreterDestination(this) {
             public String toString() { return getDock()+""; }
@@ -93,19 +76,22 @@ class InterpreterDock extends FleetTwoDock {
 
     boolean trace = false;
 
 
     boolean trace = false;
 
-    private void addInstruction(Instruction inst) {
-        if (requeueStageInCirculatingState || requeueStageHasTailInstruction) {
-            // GROSS HACK
-            instructionsBackedUpIntoSwitchFabric.add(inst);
-            return;
-        }
-        if (inst instanceof Instruction.Tail) {
-            requeueStageHasTailInstruction = true;
-            return;
+    private void addInstruction(Packet p) {
+        if (p.isToken() ||
+            instructionsBackedUpIntoSwitchFabric.size()!=0 ||
+            requeueStageInCirculatingState ||
+            (p2i(p) instanceof Instruction.Tail)) {
+            instructionsBackedUpIntoSwitchFabric.add(p);
+        } else {
+            instructions.add(p2i(p));
         }
         }
-        instructions.add(inst);
     }
     
     }
     
+    private Instruction p2i(Packet p) {
+        if (p.isToken()) throw new RuntimeException();
+        return getInterpreter().decodeInstruction(p.getValue(), InterpreterDock.this /* this is wrong, but harmless */);
+    }
+
     protected final void service() {
 
         if (dataReadyForShip || flushing) return;
     protected final void service() {
 
         if (dataReadyForShip || flushing) return;
@@ -113,10 +99,12 @@ class InterpreterDock extends FleetTwoDock {
 
         if (instructions.peek() instanceof Instruction.Head) {
             if (requeueStageInCirculatingState) { instructions.remove(); return; }
 
         if (instructions.peek() instanceof Instruction.Head) {
             if (requeueStageInCirculatingState) { instructions.remove(); return; }
-            if (!requeueStageHasTailInstruction) return;
-            requeueStageHasTailInstruction = false;
-            requeueStageInCirculatingState = true;
-            instructions.remove();
+            Packet p = instructionsBackedUpIntoSwitchFabric.peek();
+            if (p!=null && !p.isToken() && p2i(p) instanceof Instruction.Tail) {
+                instructionsBackedUpIntoSwitchFabric.remove();
+                requeueStageInCirculatingState = true;
+                instructions.remove();
+            }
             return;
         }
 
             return;
         }
 
@@ -132,11 +120,14 @@ class InterpreterDock extends FleetTwoDock {
 
                 if (ilc==0) { ilc = 1; break; }
 
 
                 if (ilc==0) { ilc = 1; break; }
 
-                if (move.interruptible && torpedoWaiting) {
-                    torpedoWaiting = false;
-                    ilc = 1;
-                    flag_d = true;
-                    break;
+                if (move.interruptible) {
+                    Packet p = instructionsBackedUpIntoSwitchFabric.peek();
+                    if (p!=null && p.isToken()) {
+                        instructionsBackedUpIntoSwitchFabric.remove();
+                        ilc = 1;
+                        flag_d = true;
+                        break;
+                    }
                 }
 
                 if (move.dataIn  && !isInputDock() && readyForDataFromShip)  return;
                 }
 
                 if (move.dataIn  && !isInputDock() && readyForDataFromShip)  return;
@@ -177,8 +168,11 @@ class InterpreterDock extends FleetTwoDock {
 
             } else if (instructions.peek() instanceof Instruction.Abort) {
                 requeueStageInCirculatingState = false;
 
             } else if (instructions.peek() instanceof Instruction.Abort) {
                 requeueStageInCirculatingState = false;
+                LinkedList<Packet> temp = new LinkedList<Packet>();
                 while (instructionsBackedUpIntoSwitchFabric.size()!=0)
                 while (instructionsBackedUpIntoSwitchFabric.size()!=0)
-                    addInstruction(instructionsBackedUpIntoSwitchFabric.remove());
+                    temp.add(instructionsBackedUpIntoSwitchFabric.remove());
+                while (temp.size()!=0)
+                    addInstruction(temp.remove());
                 break;
 
             } else if (instructions.peek() instanceof Instruction.Flush) {
                 break;
 
             } else if (instructions.peek() instanceof Instruction.Flush) {
@@ -280,9 +274,7 @@ class InterpreterDock extends FleetTwoDock {
         if (instructions.size()==0 &&
             dataPackets.size()==0 &&
             instructionsBackedUpIntoSwitchFabric.size()==0 &&
         if (instructions.size()==0 &&
             dataPackets.size()==0 &&
             instructionsBackedUpIntoSwitchFabric.size()==0 &&
-            !requeueStageHasTailInstruction &&
             !requeueStageInCirculatingState &&
             !requeueStageInCirculatingState &&
-            !torpedoWaiting &&
             !flushing &&
             !dataReadyForShip &&
             readyForDataFromShip)
             !flushing &&
             !dataReadyForShip &&
             readyForDataFromShip)
@@ -295,8 +287,6 @@ class InterpreterDock extends FleetTwoDock {
                            (flag_c?"[c] ":"")+
                            (flag_d?"[d] ":"")+
                            (requeueStageInCirculatingState?"[recirculating] ":"")+
                            (flag_c?"[c] ":"")+
                            (flag_d?"[d] ":"")+
                            (requeueStageInCirculatingState?"[recirculating] ":"")+
-                           (requeueStageHasTailInstruction?"[tail waiting] ":"")+
-                           (torpedoWaiting?"[torpedo waiting] ":"")+
                            (flushing?"[flushing] ":"")
                            );
         if (!readyForDataFromShip)
                            (flushing?"[flushing] ":"")
                            );
         if (!readyForDataFromShip)
@@ -305,7 +295,7 @@ class InterpreterDock extends FleetTwoDock {
             System.out.println(ANSI.cyan("  waiting for ship to accept: " + dataLatch.toLong()));
         for(Instruction i : instructions)
             System.out.println(ANSI.red("  "+i));
             System.out.println(ANSI.cyan("  waiting for ship to accept: " + dataLatch.toLong()));
         for(Instruction i : instructions)
             System.out.println(ANSI.red("  "+i));
-        for(Instruction i : instructionsBackedUpIntoSwitchFabric)
+        for(Packet i : instructionsBackedUpIntoSwitchFabric)
             System.out.println(ANSI.red(ANSI.bold("  "+i+" BACKED UP")));
         for(Packet p : dataPackets)
             System.out.println(ANSI.cyan("  "+p));
             System.out.println(ANSI.red(ANSI.bold("  "+i+" BACKED UP")));
         for(Packet p : dataPackets)
             System.out.println(ANSI.cyan("  "+p));
index 811c02b..85358fa 100644 (file)
@@ -1,3 +1,4 @@
+
 package edu.berkeley.fleet.interpreter;
 import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.two.*;
 package edu.berkeley.fleet.interpreter;
 import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.two.*;
@@ -47,10 +48,8 @@ abstract class InterpreterShip extends FleetTwoShip {
             return;
         } else if (someflushing && !someempty) {
             for(InterpreterDock d : docks.values())
             return;
         } else if (someflushing && !someempty) {
             for(InterpreterDock d : docks.values())
-                if (d.isInputDock() && !d.flushing && d.dataReadyForShip) {
-                    System.out.println("FLUSH AT " + this);
+                if (d.isInputDock() && !d.flushing && d.dataReadyForShip)
                     d.dataReadyForShip = false;
                     d.dataReadyForShip = false;
-                }
         }
 
         // now pass control to the subclass
         }
 
         // now pass control to the subclass