refactor codebag-memory-block creation code
[fleet.git] / src / edu / berkeley / fleet / fpga / Fpga.java
index 928ca64..06bf207 100644 (file)
@@ -1,8 +1,7 @@
 package edu.berkeley.fleet.fpga;
 import edu.berkeley.fleet.fpga.*;
-import edu.berkeley.fleet.doc.*;
 import edu.berkeley.fleet.api.*;
-import edu.berkeley.fleet.ies44.*;
+import edu.berkeley.fleet.two.*;
 import edu.berkeley.fleet.*;
 import java.lang.reflect.*;
 import edu.berkeley.sbp.chr.*;
@@ -11,366 +10,184 @@ import edu.berkeley.sbp.meta.*;
 import edu.berkeley.sbp.util.*;
 import java.util.*;
 import java.io.*;
+import edu.berkeley.fleet.two.*;
+import static edu.berkeley.fleet.two.FleetTwoFleet.*;
+import static edu.berkeley.fleet.fpga.verilog.Verilog.*;
 
-public class Fpga extends Fleet {
 
-    public ArrayList<FpgaShip> shiplist   = new ArrayList<FpgaShip>();
-    public HashMap<String,FpgaShip> ships = new HashMap<String,FpgaShip>();
-    public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)shiplist.iterator(); }
+public class Fpga extends FleetTwoFleet {
 
-    private String bitfile;
-
-    public static void main(String[] s) throws Exception {
-        new Fpga().dumpFabric(false);
+    public Ship getShip(String type, int ordinal) {
+        for(Ship s : this)
+            if (s.getType().equals(type))
+                if (ordinal-- < 0)
+                    return s;
+        return null;
     }
 
-    public Fpga() { this("gaspified.bit"); }
-    public Fpga(String bitfile) {
-        this.bitfile = bitfile;
-        createShip("Debug",   "debug");
-        createShip("Memory",    "Memory");
-        createShip("Fifo",    "fifo1");
-        createShip("Fifo",    "fifo2");
-        createShip("Fifo",    "fifo3");
-        createShip("Execute", "execute");
-        createShip("Alu1",    "alu1");
-        createShip("Alu2",    "alu2a");
-        createShip("Lut3",      "lut3");
-        createShip("Choice",    "Choice");
-        createShip("Stack",     "Stack");
-        createShip("Alu3",      "alu3");
-        createShip("BitFifo",   "bitfifo");
-
-        // above is the minimal ship set needed to run the regression suite
-        // below are extra bonush ships
-        /*
-        createShip("Alu2",    "alu2b");
-        createShip("Alu2",    "alu2c");
-        createShip("Alu2",    "alu2d");
-        createShip("Fifo",    "fifo4");
-        createShip("Memory",    "Memory");
-        createShip("Choice",    "Choice");
-        createShip("Choice",    "Choice");
-        createShip("Choice",    "Choice");
-        */
-        dumpFabric(true);
+    public int getWordWidth() { return 37; }
+    private static final BitVector SIGNAL_ZERO = new BitVector(1);
+    private static final BitVector SIGNAL_ONE  = new BitVector(1);
+    static {
+        SIGNAL_ONE.set(0,true);
     }
 
-    public Ship createShip(String type, String name) {
-        try {
-            ShipDescription sd = new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
-            FpgaShip ship = new FpgaShip(this, name, type, sd);
-            ships.put(name, ship);
-            shiplist.add(ship);
-            return ship;
-        } catch (IOException e) { throw new RuntimeException(e); }
+    public Module top;
+    public FabricElement top_horn;
+
+    public LinkedHashMap<String,FpgaShip> ships = new LinkedHashMap<String,FpgaShip>();
+    public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
+
+    public static void main(String[] s) throws Exception { 
+        String prefix = s[0];
+
+        new FunnelModule().dump(prefix);
+        new HornModule().dump(prefix);
+
+        new FifoModule(0).dump(prefix);
+        new FifoModule(4).dump(prefix);
+        new FifoModule(8).dump(prefix);
+        new FpgaDock.DockModule(false).dump(prefix);
+        new FpgaDock.DockModule(true).dump(prefix);
+
+        Module top = new Module("root");
+        new Fpga(top).top.dump(prefix);
     }
 
-    public FleetProcess run(final byte[] instructions) {
+    public FleetProcess run(Instruction[] instructions) {
         try {
-            return new Client(bitfile, instructions);
-        } catch (IOException e) { throw new RuntimeException(e); }
+            return new Client(this, "none", instructions);
+        } catch (Exception e) { throw new RuntimeException(e); }
     }
 
-    public void dumpFabric(boolean quiet) {
-        // 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>();
-        for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special())
-                    instructionports.add(port);
-        FabricTree instructions =
-            new FabricTree((FpgaBenkoBox[])instructionports.toArray(new FpgaBenkoBox[0]),
-                           "ihorn",
-                           "instruction");
-
-        ArrayList inputports = new ArrayList<FpgaBenkoBox>();
-        for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special())
-                    inputports.add(port);
-        FabricTree inputs =
-            new FabricTree((FpgaBenkoBox[])inputports.toArray(new FpgaBenkoBox[0]),
-                           "horn",
-                           "dest");
-
-        ArrayList outputports = new ArrayList<FpgaBenkoBox>();
-        for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special() || ((FpgaBenkoBox)port).dhorn())
-                    outputports.add(port);
-        FabricTree outputs =
-            new FabricTree((FpgaBenkoBox[])outputports.toArray(new FpgaBenkoBox[0]),
-                           "funnel",
-                           "source");
-
-        ArrayList ihornports = new ArrayList<FpgaBenkoBox>();
-        for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (((FpgaBenkoBox)port).ihorn())
-                    ihornports.add(port);
-        FabricTree ihorns =
-            new FabricTree((FpgaBenkoBox[])ihornports.toArray(new FpgaBenkoBox[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,");
-        System.out.println("                   data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
-        System.out.println("  input  clk;");
-        System.out.println("  input  data_Memory0_command_r;");
-        System.out.println("  output data_Memory0_command_a;");
-        System.out.println("  output data_Debug0_out_r;");
-        System.out.println("  input  data_Debug0_out_a;");
-        System.out.println("  output [(`PACKET_WIDTH-1):0]      data_Debug0_out;");
-        System.out.println("  input  [(`PACKET_WIDTH-1):0]      data_Memory0_command;");
-        //System.out.println("  wire   [(`INSTRUCTION_WIDTH-1):0] data_Memory0_ihorn;");
-        //System.out.println("  wire   [(`PACKET_WIDTH-1):0]      data_Memory0_dhorn;");
-        System.out.println();
-        
-        System.out.println();
-
-        instructions.dumpChannels(true);
-        outputs.dumpChannels(true);
-        inputs.dumpChannels(true);
-        ihorns.dumpChannels(true);
-        for(FpgaShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((FpgaBenkoBox)port).special() || ((FpgaBenkoBox)port).dhorn())
-                    System.out.println("  wire [(`PACKET_WIDTH-1):0] data_"
-                                       +getUniqueName(ship)+"_"+port.getName()+";");
-
-        System.out.println("");
-        instructions.dumpChannels(false);
-        System.out.println("");
-        outputs.dumpChannels(false);
-        System.out.println("");
-        inputs.dumpChannels(false);
-        System.out.println("");
-        ihorns.dumpChannels(false);
-        System.out.println("");
-        for(FpgaShip ship : shiplist) {
-            System.out.print(ship.getType().toLowerCase());
-            System.out.print(" ");
-            System.out.print("krunk"+(krunk++));
-            System.out.print("(clk, ");
-            boolean first = true;
-            for(BenkoBox port : ship.getBenkoBoxes()) {
-                if (!first) System.out.print(", ");
-                first = false;
-                String prefix = "data_";
-                if (((FpgaBenkoBox)port).ihorn()) prefix = "ihorn_";
-                if (((FpgaBenkoBox)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());
-                System.out.print(" ");
-            }
-            System.out.println(");");
-
-            for(BenkoBox port : ship.getBenkoBoxes()) {
-                if (((FpgaBenkoBox)port).special()) continue;
-                if (((FpgaBenkoBox)port).inbox) {
-                    System.out.print("inbox");
-                } else {
-                    System.out.print("outbox");
-                }
-                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("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()+", ");
-                System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
-                System.out.print(");");
-                System.out.println();
-            }
+    // Setup //////////////////////////////////////////////////////////////////////////////
 
-        }
-        /*
-        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");
+    public Ship createShip(String type, String name) throws IOException {
+        ShipDescription sd = new ShipDescription(type, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
+        FpgaShip ship = new FpgaShip(this, sd);
+        ships.put(name, ship);
+        return ship;
     }
 
-    private static class FabricTree {
-        int master_idx = 1;
-        String prefix;
-        Node root;
-        public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
-        public FabricTree(FpgaBenkoBox[] 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,
-                              int start, int end, int addr, int bits) {
-            if (end-start == 0) return null;
-            if (end-start == 1) {
-                FpgaBenkoBox p = ports[start];
-                if (prefix.equals("instruction")) {
-                    p.instr_addr = addr;
-                    p.instr_bits = bits;
-                } 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 FpgaBenkoBox.VirtualPort)) continue;
-                        FpgaBenkoBox.VirtualPort vp = (FpgaBenkoBox.VirtualPort)d;
-                        vp.addr = p.addr | (count << bits);
-                        count++;
-                    }
-                }
-                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, breakpt, addr,               bits+1),
-                            mkNode(name+"_1", component, ports, breakpt, end,   addr | (1 << bits), bits+1),
-                            addr,
-                            bits);
+    public Fpga() throws Exception { this(new Module("root")); }
+    public Fpga(Module top) throws Exception {
+        this.top = top;
+        debugShip = createShip("Debug",     "debug");
+        createShip("Memory",    "memory");
+        createShip("Fifo",      "fifo1");
+        createShip("Fifo",      "fifo2");
+        createShip("Alu2",      "alu2a");
+        createShip("Rotator",   "rotator");
+        //createShip("Alu1",      "alu1");
+        createShip("Lut3",      "lut3");
+        createShip("Alu3",      "alu3");
+
+        Module fifostage = new FifoModule(0);
+        Module fifo4     = new FifoModule(4);
+        Module fifo8     = new FifoModule(8);
+        Module horn      = new HornModule();
+        Module funnel    = new FunnelModule();
+        Module outbox    = new FpgaDock.DockModule(false);
+        Module inbox     = new FpgaDock.DockModule(true);
+
+        Module.SinkPort    debug_in    = top.createWirePort("debug_in", WIDTH_PACKET);
+        Module.SourcePort  debug_out   = null;
+        for(FpgaShip ship : (Iterable<FpgaShip>)(Object)this) {
+            if (ship.getType().toLowerCase().equals("debug"))
+                debug_out = ship.getVerilogModule().getOutputPort("debug_out");
         }
-        private String describe(String prefix, Object o) {
-            if (o==null) return null;
-            if (o instanceof FpgaBenkoBox) {
-                FpgaBenkoBox p = (FpgaBenkoBox)o;
-                return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
-            }
-            if (o instanceof Node) {
-                return ((Node)o).describe(prefix);
+
+        Module.SourcePort  in          = top.createInputPort("in", 8);
+        Module.SinkPort    out         = top.createOutputPort("out", 8, "");
+        Module.Latch       temp_in     = top.new Latch("temp", WIDTH_PACKET) { public String doReset() { return name+"=0;"; } };
+        Module.Latch       count       = top.new Latch("count", 8);
+        Module.Latch       count_out   = top.new Latch("count_out", 8);
+        top.new Event(new Object[] { in, debug_in },
+                      new Object[] { new SimpleAction(temp_in.getVerilogName()+" = ("+temp_in.getVerilogName()+" << 8) | in;"),
+                                     new SimpleAction("if (count >= 5) begin"+
+                                                          " count <= 0; "+
+                                                          " `packet_token("+debug_in.getVerilogName()+") <= 0;"+
+                                                          " `packet_data("+debug_in.getVerilogName()+") <= "+temp_in.getVerilogName()+";"+
+                                                          " `packet_dest("+debug_in.getVerilogName()+") <= `instruction_dest("+temp_in.getVerilogName()+");"+
+                                                          " "+debug_in.getVerilogName()+"_r <= 1; "+
+                                                          "end else count <= count+1; "),
+                                     in
+                  });
+        top.new Event(new Object[] { out, debug_out },
+                      new Object[] { new SimpleAction(out.getVerilogName()+" <= ("+debug_out.getVerilogName()+">> (count_out*8));"),
+                                     new SimpleAction("if (count_out >= 5) begin "+
+                                                          "count_out <= 0; "+debug_out.getVerilogName()+"_a <= 1; end"+
+                                                          " else count_out <= count_out+1; "),
+                                     out });
+
+        ArrayList sources = new ArrayList<FabricElement>();
+        ArrayList dests   = new ArrayList<FabricElement>();
+        for(FpgaShip ship : (Iterable<FpgaShip>)(Object)this) {
+            if (ship.getType().toLowerCase().equals("debug"))
+                debug_out = ship.getVerilogModule().getOutputPort("debug_out");
+            for(Dock port : ship) {
+                sources.add(((FpgaDock)port));
+                dests.add(port.getInstructionDestination());
+                dests.add(port.getDataDestination());
             }
-            return null;
         }
-        private class Node {
-            Object left;
-            Object right;
-            String name;
-            String component;
-            int addr;
-            int bits;
-            public Node(String name, String component, Object left, Object right, int addr, int bits) {
-                this.left = left;
-                this.right = right;
-                this.name = name;
-                this.component = component;
-                this.addr = addr;
-                this.bits = bits;
-            }
-            public void dumpChannels(int indentamount, boolean decl) {
-                String indent = "";
-                for(int i=0; i<indentamount; i++) indent += "  ";
-                if (decl) {
-                    String n = describe(prefix).startsWith("instruction")
-                        ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
-                    System.out.println("  wire "+n+" "+indent+describe(prefix)+";");
-                } else {
-                    System.out.println("     "+indent+
-                                       component+" "+
-                                       "krunk"+(krunk++)+"(clk, "+
-                                       describe(prefix)+"_r, "+
-                                       describe(prefix)+"_a, "+
-                                       describe(prefix)+", "+
-                                       FabricTree.this.describe(prefix, left)+"_r, "+
-                                       FabricTree.this.describe(prefix, left)+"_a, "+
-                                       FabricTree.this.describe(prefix, left)+", "+
-                                       FabricTree.this.describe(prefix, right)+"_r, "+
-                                       FabricTree.this.describe(prefix, right)+"_a, "+
-                                       FabricTree.this.describe(prefix, right)+
-                                       ");");
-                }
-                dumpChannels(left, indentamount+1, decl);
-                dumpChannels(right, indentamount+1, decl);
-            }
-            public void dumpChannels(Object o, int indentamount, boolean decl) {
-                if (o==null) return;
-                if (o instanceof Node) {
-                    ((Node)o).dumpChannels(indentamount, decl);
-                } else {
-                    String indent = "";
-                    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]";
-                        System.out.println("  wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
-                    }
-                }
-            }
-            public String describe(String prefix) {
-                return prefix+name;
+        top_horn = mkNode((FabricElement[])dests.toArray(new FabricElement[0]), true);
+        FabricElement   source  = mkNode((FabricElement[])sources.toArray(new FabricElement[0]), false);
+        FunnelModule.FunnelInstance top_funnel = new FunnelModule.FunnelInstance(top, debug_in, source.getOutputPort());
+        ((FunnelModule.FunnelInstance)source).out = top_funnel;
+        //top_horn.addInput(top_funnel, top_funnel.getOutputPort());
+        top_funnel.addOutput(top_horn, top_horn.getInputPort());
+    }
+
+    public FabricElement mkNode(FabricElement[] ports, boolean is_horn) { return mkNode(ports, is_horn, 0, ports.length); }
+    public FabricElement mkNode(FabricElement[] ports, boolean is_horn, int start, int end) {
+        switch(end-start) {
+            case 0: return null;
+            case 1: return ports[start];
+            default: {
+                FabricElement leftPort  = mkNode(ports, is_horn,  start,         (end+start)/2);
+                FabricElement rightPort = mkNode(ports, is_horn,  (end+start)/2, end);
+                return is_horn
+                    ? new HornModule.HornInstance(top,     leftPort, rightPort)
+                    : new FunnelModule.FunnelInstance(top, leftPort, rightPort);
             }
         }
     }
-    public static int krunk=0;
 
-    private static String getUniqueName(Ship ship) {
-        return ship.getType() + ship.getOrdinal();
-    }
+    public Module getVerilogModule() { return top; }
 
-    private static int count(Iterable<Destination> it) {
-        int ret = 0;
-        for(Destination d : it)
-            ret++;
-        return ret;
-    }
+
+    // Expand //////////////////////////////////////////////////////////////////////////////
 
     public void expand(ShipDescription sd) {
         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);
             PrintWriter pw = new PrintWriter(out);
 
-            boolean auto =
-                !"debug".equals(filename) &&
-                !"execute".equals(filename) &&
-                !"memory".equals(filename) &&
-                !"fifo".equals(filename);
+            boolean auto = !"debug".equals(filename);
+
             if (auto) {
                 pw.println("`include \"macros.v\"");
                 pw.println();
-                pw.println("module " + filename + "( clk");
-                for(BenkoBoxDescription bb : sd) {
+
+                pw.print("`define reset ");
+                for(DockDescription bb : sd) {
+                    String bb_name = bb.getName();
+                    if (bb.isInputDock()) pw.print(bb_name+"_a <= 1; ");
+                    else              pw.print(bb_name+"_r <= 0; ");
+                }
+                pw.println();
+
+                pw.println("module " + filename + "( clk, rst ");
+                for(DockDescription bb : sd) {
                     String bb_name = bb.getName();
                     pw.print("        ");
-                    if (bb.isInbox()) {
+                    if (bb.isInputDock()) {
                         pw.print(", " + bb_name+"_r");
                         pw.print(", " + bb_name+"_a_");
                         pw.print(", " + bb_name+"_d");
@@ -384,15 +201,16 @@ public class Fpga extends Fleet {
                 pw.println("        );");
                 pw.println();
                 pw.println("    input clk;");
-                for(BenkoBoxDescription bb : sd) {
+                pw.println("    input rst;");
+                for(DockDescription bb : sd) {
                     String bb_name = bb.getName();
                     pw.print("        ");
-                    if (bb.isInbox()) {
+                    if (bb.isInputDock()) {
                         pw.println("`input(" +
                                    bb_name+"_r,  "+
                                    bb_name+"_a,  "+
                                    bb_name+"_a_, "+
-                                   "[(`PACKET_WIDTH-1):0],"+
+                                   "[("+WIDTH_WORD+"-1):0],"+
                                    bb_name+"_d)"
                                    );
                     } else {
@@ -400,14 +218,15 @@ public class Fpga extends Fleet {
                                    bb_name+"_r,  "+
                                    bb_name+"_r_, "+
                                    bb_name+"_a,  "+
-                                   "[(`PACKET_WIDTH-1):0],"+
+                                   "[("+WIDTH_WORD+"-1):0],"+
                                    bb_name+"_d_)"
                                    );
-                        pw.println("`defreg(" +
-                                   bb_name+"_d_,  "+
-                                   "[(`PACKET_WIDTH-1):0],"+
-                                   bb_name+"_d)"
-                                   );
+                        if (!bb_name.equals("out") || !"memory".equals(filename))
+                            pw.println("`defreg(" +
+                                       bb_name+"_d_,  "+
+                                       "[("+WIDTH_WORD+"-1):0],"+
+                                       bb_name+"_d)"
+                                       );
                     }
                     pw.println();
                 }
@@ -423,33 +242,32 @@ public class Fpga extends Fleet {
         } catch (Exception e) { throw new RuntimeException(e); }
     }
 
-    public int computeOffset(int origin, int target) { return (target - origin)/6; }
-    public int computeTarget(int origin, int offset) { return origin + (offset*6); }
 
-    private FpgaInstructionEncoder iie = new FpgaInstructionEncoder();
-    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); }
+    private Ship debugShip;
+
 
-    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 Destination getDestByAddr(long dest) {
+        public Dock getUniversalSource() { return debugShip.getDock("in"); }
+        public long getDestAddr(Path path) {
+            return ((FpgaPath)path).toLong();
+        }
+        public Path getPathByAddr(Dock source, long dest) {
             for(Ship ship : Fpga.this)
-                for(BenkoBox bb : ship.getBenkoBoxes())
-                    for(Destination d : bb.getDestinations())
-                        if (getDestAddr(d)==dest)
-                            return d;
+                for(Dock bb : ship) {
+                    for(Destination d : new Destination[] { bb.getInstructionDestination(), bb.getDataDestination() }) {
+                        for(BitVector signal : new BitVector[] { SIGNAL_ZERO, SIGNAL_ONE }) {
+                            FpgaPath p = (FpgaPath)source.getPath(d, signal);
+                            if (p.toLong() == dest) return p;
+                        }
+                    }
+                }
             return null;
         }
-        public BenkoBox getBoxByInstAddr(long dest) {
+        public Dock getBoxByInstAddr(long dest) {
             for(Ship ship : Fpga.this)
-                for(BenkoBox bb : ship.getBenkoBoxes())
-                    if (((FpgaBenkoBox)bb).instr_addr == dest)
+                for(Dock bb : ship)
+                    if (((FpgaDestination)((FpgaDock)bb).getInstructionDestination()).getAddr() == dest)
                         return bb;
             return null;
         }
-    }
 
-}
\ No newline at end of file
+}