final pass to update instruction encoding; should now match the spec
[fleet.git] / src / edu / berkeley / fleet / fpga / Fpga.java
index 928ca64..72a7597 100644 (file)
@@ -28,11 +28,11 @@ public class Fpga extends Fleet {
     public Fpga(String bitfile) {
         this.bitfile = bitfile;
         createShip("Debug",   "debug");
-        createShip("Memory",    "Memory");
+        createShip("Memory",    "memory");
+        createShip("Memory",    "memory2");  // need this to avoid a bug
         createShip("Fifo",    "fifo1");
         createShip("Fifo",    "fifo2");
         createShip("Fifo",    "fifo3");
-        createShip("Execute", "execute");
         createShip("Alu1",    "alu1");
         createShip("Alu2",    "alu2a");
         createShip("Lut3",      "lut3");
@@ -76,46 +76,46 @@ public class Fpga extends Fleet {
         // FIXME: this is really ugly: the order of port declarations in
         //        the XXXShip.java file must match the order in the .balsa file!
 
-        ArrayList instructionports = new ArrayList<FpgaBenkoBox>();
+        ArrayList instructionports = new ArrayList<FpgaPump>();
         for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special())
+            for(Pump port : ship.getPumps())
+                if (!((FpgaPump)port).special())
                     instructionports.add(port);
         FabricTree instructions =
-            new FabricTree((FpgaBenkoBox[])instructionports.toArray(new FpgaBenkoBox[0]),
+            new FabricTree((FpgaPump[])instructionports.toArray(new FpgaPump[0]),
                            "ihorn",
                            "instruction");
 
-        ArrayList inputports = new ArrayList<FpgaBenkoBox>();
+        ArrayList inputports = new ArrayList<FpgaPump>();
         for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special())
+            for(Pump port : ship.getPumps())
+                if (!((FpgaPump)port).special())
                     inputports.add(port);
         FabricTree inputs =
-            new FabricTree((FpgaBenkoBox[])inputports.toArray(new FpgaBenkoBox[0]),
+            new FabricTree((FpgaPump[])inputports.toArray(new FpgaPump[0]),
                            "horn",
                            "dest");
 
-        ArrayList outputports = new ArrayList<FpgaBenkoBox>();
+        ArrayList outputports = new ArrayList<FpgaPump>();
         for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special() || ((FpgaBenkoBox)port).dhorn())
+            for(Pump port : ship.getPumps())
+                if (!((FpgaPump)port).special() || ((FpgaPump)port).dhorn())
                     outputports.add(port);
         FabricTree outputs =
-            new FabricTree((FpgaBenkoBox[])outputports.toArray(new FpgaBenkoBox[0]),
+            new FabricTree((FpgaPump[])outputports.toArray(new FpgaPump[0]),
                            "funnel",
                            "source");
 
-        ArrayList ihornports = new ArrayList<FpgaBenkoBox>();
+        ArrayList ihornports = new ArrayList<FpgaPump>();
         for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (((FpgaBenkoBox)port).ihorn())
+            for(Pump port : ship.getPumps())
+                if (((FpgaPump)port).ihorn())
                     ihornports.add(port);
         FabricTree ihorns =
-            new FabricTree((FpgaBenkoBox[])ihornports.toArray(new FpgaBenkoBox[0]),
+            new FabricTree((FpgaPump[])ihornports.toArray(new FpgaPump[0]),
                            "funnel",
                            "ihorn");
-        
+
         if (quiet) return;
         System.out.println("`include \"macros.v\"");
         System.out.println("module fabric(clk, data_Memory0_command_r, data_Memory0_command_a, data_Memory0_command,");
@@ -138,11 +138,13 @@ public class Fpga extends Fleet {
         inputs.dumpChannels(true);
         ihorns.dumpChannels(true);
         for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special() || ((FpgaBenkoBox)port).dhorn())
+            for(Pump port : ship.getPumps())
+                if (!((FpgaPump)port).special() || ((FpgaPump)port).dhorn())
                     System.out.println("  wire [(`PACKET_WIDTH-1):0] data_"
                                        +getUniqueName(ship)+"_"+port.getName()+";");
 
+        System.out.println("wire [(`PACKET_WIDTH-1):0] ihornleft;");
+
         System.out.println("");
         instructions.dumpChannels(false);
         System.out.println("");
@@ -158,12 +160,12 @@ public class Fpga extends Fleet {
             System.out.print("krunk"+(krunk++));
             System.out.print("(clk, ");
             boolean first = true;
-            for(BenkoBox port : ship.getBenkoBoxes()) {
+            for(Pump port : ship.getPumps()) {
                 if (!first) System.out.print(", ");
                 first = false;
                 String prefix = "data_";
-                if (((FpgaBenkoBox)port).ihorn()) prefix = "ihorn_";
-                if (((FpgaBenkoBox)port).dhorn()) prefix = "source_";
+                if (((FpgaPump)port).ihorn()) prefix = "ihorn_";
+                if (((FpgaPump)port).dhorn()) prefix = "source_";
                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
@@ -171,9 +173,9 @@ public class Fpga extends Fleet {
             }
             System.out.println(");");
 
-            for(BenkoBox port : ship.getBenkoBoxes()) {
-                if (((FpgaBenkoBox)port).special()) continue;
-                if (((FpgaBenkoBox)port).inbox) {
+            for(Pump port : ship.getPumps()) {
+                if (((FpgaPump)port).special()) continue;
+                if (((FpgaPump)port).inbox) {
                     System.out.print("inbox");
                 } else {
                     System.out.print("outbox");
@@ -181,7 +183,7 @@ public class Fpga extends Fleet {
                 System.out.print(" krunk"+(krunk++)+"(clk, ");
                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
+                System.out.print("`packet_data(instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"), ");
                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
@@ -196,18 +198,23 @@ public class Fpga extends Fleet {
             }
 
         }
+
+        System.out.println("funnel ihornfun(clk,"+
+                           "             ihornleft_r, ihornleft_a, ihornleft,"+
+                           "             ihorn_r, ihorn_a, ihorn,"+
+                           "             source_r, source_a, source);");
+        System.out.println("horn tophorn(clk,"+
+                           "             ihornleft_r, ihornleft_a, ihornleft,"+
+                           "             instruction_r, instruction_a, instruction,"+
+                           "             dest_r, dest_a, dest);");
         /*
-        System.out.println("funnel topfun(clk,"+
-                           "              dest_r, dest_a, dest,"+
-                           "              source_r, source_a, source,"+
-                           "              data_Memory0_dhorn_r, data_Memory0_dhorn_a, data_Memory0_dhorn);");
-        */
         System.out.println("assign instruction_r = ihorn_r;");
         System.out.println("assign ihorn_a = instruction_a;");
         System.out.println("assign instruction = ihorn;");
         System.out.println("assign dest_r = source_r;");
         System.out.println("assign source_a = dest_a;");
         System.out.println("assign dest = source;");
+        */
         System.out.println("endmodule");
     }
 
@@ -216,27 +223,27 @@ public class Fpga extends Fleet {
         String prefix;
         Node root;
         public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
-        public FabricTree(FpgaBenkoBox[] ports, String component, String prefix) {
+        public FabricTree(FpgaPump[] ports, String component, String prefix) {
             this.prefix = prefix;
             root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
         }
-        private Object mkNode(String name, String component, FpgaBenkoBox[] ports,
+        private Object mkNode(String name, String component, FpgaPump[] ports,
                               int start, int end, int addr, int bits) {
             if (end-start == 0) return null;
             if (end-start == 1) {
-                FpgaBenkoBox p = ports[start];
+                FpgaPump p = ports[start];
                 if (prefix.equals("instruction")) {
-                    p.instr_addr = addr;
-                    p.instr_bits = bits;
+                    p.instr_addr = (addr<<1);
+                    p.instr_bits = bits+1;
                 } else if (prefix.equals("dest")) {
-                    p.addr = addr;
-                    p.bits = bits;
+                    p.addr = (addr << 1) | 1;
+                    p.bits = bits+1;
                     if (bits >= 11)
-                        throw new RuntimeException("too many benkoboxen!");
+                        throw new RuntimeException("too many pumps!");
                     int count = 0;
                     for(Destination d : p.getDestinations()) {
-                        if (!(d instanceof FpgaBenkoBox.VirtualPort)) continue;
-                        FpgaBenkoBox.VirtualPort vp = (FpgaBenkoBox.VirtualPort)d;
+                        if (!(d instanceof FpgaPump.VirtualPort)) continue;
+                        FpgaPump.VirtualPort vp = (FpgaPump.VirtualPort)d;
                         vp.addr = p.addr | (count << bits);
                         count++;
                     }
@@ -267,8 +274,8 @@ public class Fpga extends Fleet {
         }
         private String describe(String prefix, Object o) {
             if (o==null) return null;
-            if (o instanceof FpgaBenkoBox) {
-                FpgaBenkoBox p = (FpgaBenkoBox)o;
+            if (o instanceof FpgaPump) {
+                FpgaPump p = (FpgaPump)o;
                 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
             }
             if (o instanceof Node) {
@@ -296,7 +303,7 @@ public class Fpga extends Fleet {
                 for(int i=0; i<indentamount; i++) indent += "  ";
                 if (decl) {
                     String n = describe(prefix).startsWith("instruction")
-                        ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
+                        ? "[(`PACKET_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
                     System.out.println("  wire "+n+" "+indent+describe(prefix)+";");
                 } else {
                     System.out.println("     "+indent+
@@ -325,7 +332,7 @@ public class Fpga extends Fleet {
                     for(int i=0; i<indentamount; i++) indent += "  ";
                     if (decl) {
                         String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
-                            ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
+                            ? "[(`PACKET_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
                         System.out.println("  wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
                     }
                 }
@@ -352,7 +359,7 @@ public class Fpga extends Fleet {
         try {
             if (sd.getSection("fpga")==null) return;
             String filename = sd.getName().toLowerCase();
-            File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
+            File outf = new File("build/fpga/"+filename+".v");
             new File(outf.getParent()).mkdirs();
             System.err.println("writing to " + outf);
             FileOutputStream out = new FileOutputStream(outf);
@@ -367,7 +374,7 @@ public class Fpga extends Fleet {
                 pw.println("`include \"macros.v\"");
                 pw.println();
                 pw.println("module " + filename + "( clk");
-                for(BenkoBoxDescription bb : sd) {
+                for(PumpDescription bb : sd) {
                     String bb_name = bb.getName();
                     pw.print("        ");
                     if (bb.isInbox()) {
@@ -384,7 +391,7 @@ public class Fpga extends Fleet {
                 pw.println("        );");
                 pw.println();
                 pw.println("    input clk;");
-                for(BenkoBoxDescription bb : sd) {
+                for(PumpDescription bb : sd) {
                     String bb_name = bb.getName();
                     pw.print("        ");
                     if (bb.isInbox()) {
@@ -433,20 +440,20 @@ public class Fpga extends Fleet {
     public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
 
     private class FpgaInstructionEncoder extends InstructionEncoder {
-        public long getDestAddr(Destination box) { return ((FpgaBenkoBox.VirtualPort)box).addr; }
-        public long getBoxInstAddr(BenkoBox box) { return ((FpgaBenkoBox)box).instr_addr; }
+        public long getDestAddr(Destination box) { return ((FpgaPump.VirtualPort)box).addr; }
+        public long getBoxInstAddr(Pump box) { return ((FpgaPump)box).instr_addr; }
         public Destination getDestByAddr(long dest) {
             for(Ship ship : Fpga.this)
-                for(BenkoBox bb : ship.getBenkoBoxes())
+                for(Pump bb : ship.getPumps())
                     for(Destination d : bb.getDestinations())
                         if (getDestAddr(d)==dest)
                             return d;
             return null;
         }
-        public BenkoBox getBoxByInstAddr(long dest) {
+        public Pump getBoxByInstAddr(long dest) {
             for(Ship ship : Fpga.this)
-                for(BenkoBox bb : ship.getBenkoBoxes())
-                    if (((FpgaBenkoBox)bb).instr_addr == dest)
+                for(Pump bb : ship.getPumps())
+                    if (((FpgaPump)bb).instr_addr == dest)
                         return bb;
             return null;
         }