add code to "balance" the switch fabric tree
[fleet.git] / src / edu / berkeley / fleet / slipway / Slipway.java
index 9fdefaf..1de2f01 100644 (file)
@@ -25,7 +25,7 @@ public class Slipway extends Fleet {
         new Slipway().dumpFabric(false);
     }
 
-    public Slipway() { this("groundhog.bit"); }
+    public Slipway() { this("groundhog2.bit"); }
     public Slipway(String bitfile) {
         this.bitfile = bitfile;
         createShip("Debug",   "debug");
@@ -42,10 +42,12 @@ public class Slipway extends Fleet {
         createShip("Memory",    "Memory");
         createShip("Lut3",      "lut3");
         createShip("Alu1",      "alu1");
+        createShip("Alu3",      "alu3");
         createShip("Choice",    "Choice");
         createShip("Choice",    "Choice");
         createShip("Choice",    "Choice");
         createShip("Choice",    "Choice");
+        createShip("Stack",     "Stack");
         dumpFabric(true);
     }
 
@@ -224,6 +226,8 @@ public class Slipway extends Fleet {
                 } else if (prefix.equals("dest")) {
                     p.addr = addr;
                     p.bits = bits;
+                    if (bits >= 11)
+                        throw new RuntimeException("too many benkoboxen!");
                     int count = 0;
                     for(Destination d : p.getDestinations()) {
                         if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
@@ -235,10 +239,24 @@ public class Slipway extends Fleet {
                 return p;
             }
             int len = end-start;
+            int count   = 0;
+            int count2  = 0;
+            int breakpt = 0;
+            if (end-start <= 2) {
+                breakpt = (start+end)/2;
+            } else {
+                for(int i=start; i<end; i++)
+                    count += count(ports[i].getDestinations());
+                for(int i=start; i<end-1; i++) {
+                    count2 += count(ports[i].getDestinations());
+                    breakpt = i;
+                    if (i>start && count2 >= count/2) break;
+                }
+            }
             return new Node(name,
                             component,
-                            mkNode(name+"_0", component, ports, start, start+len/2, addr,               bits+1),
-                            mkNode(name+"_1", component, ports, start+len/2, end,   addr | (1 << bits), bits+1),
+                            mkNode(name+"_0", component, ports, start, breakpt, addr,               bits+1),
+                            mkNode(name+"_1", component, ports, breakpt, end,   addr | (1 << bits), bits+1),
                             addr,
                             bits);
         }
@@ -318,6 +336,13 @@ public class Slipway extends Fleet {
         return ship.getType() + ship.getOrdinal();
     }
 
+    private static int count(Iterable<Destination> it) {
+        int ret = 0;
+        for(Destination d : it)
+            ret++;
+        return ret;
+    }
+
     public void expand(ShipDescription sd) {
         try {
             String filename = sd.getName().toLowerCase();
@@ -327,7 +352,7 @@ public class Slipway extends Fleet {
             FileOutputStream out = new FileOutputStream(outf);
             PrintWriter pw = new PrintWriter(out);
 
-            boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice");
+            boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice") || filename.equals("alu3") || filename.equals("stack");
             if (auto) {
                 pw.println("`include \"macros.v\"");
                 pw.println();