change bitfile name to groundhog2.bit
[fleet.git] / src / edu / berkeley / fleet / slipway / Slipway.java
1 package edu.berkeley.fleet.slipway;
2 import edu.berkeley.fleet.slipway.*;
3 import edu.berkeley.fleet.doc.*;
4 import edu.berkeley.fleet.api.*;
5 import edu.berkeley.fleet.ies44.*;
6 import edu.berkeley.fleet.*;
7 import java.lang.reflect.*;
8 import edu.berkeley.sbp.chr.*;
9 import edu.berkeley.sbp.misc.*;
10 import edu.berkeley.sbp.meta.*;
11 import edu.berkeley.sbp.bind.*;
12 import edu.berkeley.sbp.util.*;
13 import java.util.*;
14 import java.io.*;
15
16 public class Slipway extends Fleet {
17
18     public ArrayList<SlipwayShip> shiplist   = new ArrayList<SlipwayShip>();
19     public HashMap<String,SlipwayShip> ships = new HashMap<String,SlipwayShip>();
20     public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)shiplist.iterator(); }
21
22     private String bitfile;
23
24     public static void main(String[] s) throws Exception {
25         new Slipway().dumpFabric(false);
26     }
27
28     public Slipway() { this("groundhog2.bit"); }
29     public Slipway(String bitfile) {
30         this.bitfile = bitfile;
31         createShip("Debug",   "debug");
32         createShip("Alu2",    "alu2a");
33         createShip("Alu2",    "alu2b");
34         createShip("Alu2",    "alu2c");
35         createShip("Alu2",    "alu2d");
36         createShip("Fifo",    "fifo1");
37         createShip("Fifo",    "fifo2");
38         createShip("Fifo",    "fifo3");
39         createShip("Fifo",    "fifo4");
40         createShip("Execute", "execute");
41         createShip("Memory",    "Memory");
42         createShip("Memory",    "Memory");
43         createShip("Lut3",      "lut3");
44         createShip("Alu1",      "alu1");
45         createShip("Alu3",      "alu3");
46         createShip("Choice",    "Choice");
47         createShip("Choice",    "Choice");
48         createShip("Choice",    "Choice");
49         createShip("Choice",    "Choice");
50         createShip("Stack",     "Stack");
51         dumpFabric(true);
52     }
53
54     public Ship createShip(String type, String name) {
55         try {
56             ShipDescription sd = new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
57             SlipwayShip ship = new SlipwayShip(this, name, type, sd);
58             ships.put(name, ship);
59             shiplist.add(ship);
60             return ship;
61         } catch (IOException e) { throw new RuntimeException(e); }
62     }
63
64     public FleetProcess run(final byte[] instructions) {
65         try {
66             return new Client(bitfile, instructions);
67         } catch (IOException e) { throw new RuntimeException(e); }
68     }
69
70     public void dumpFabric(boolean quiet) {
71         // FIXME: this is really ugly: the order of port declarations in
72         //        the XXXShip.java file must match the order in the .balsa file!
73
74         ArrayList instructionports = new ArrayList<SlipwayBenkoBox>();
75         for(SlipwayShip ship : shiplist)
76             for(BenkoBox port : ship.getBenkoBoxes())
77                 if (!((SlipwayBenkoBox)port).special())
78                     instructionports.add(port);
79         FabricTree instructions =
80             new FabricTree((SlipwayBenkoBox[])instructionports.toArray(new SlipwayBenkoBox[0]),
81                            "ihorn",
82                            "instruction");
83
84         ArrayList inputports = new ArrayList<SlipwayBenkoBox>();
85         for(SlipwayShip ship : shiplist)
86             for(BenkoBox port : ship.getBenkoBoxes())
87                 if (!((SlipwayBenkoBox)port).special())
88                     inputports.add(port);
89         FabricTree inputs =
90             new FabricTree((SlipwayBenkoBox[])inputports.toArray(new SlipwayBenkoBox[0]),
91                            "horn",
92                            "dest");
93
94         ArrayList outputports = new ArrayList<SlipwayBenkoBox>();
95         for(SlipwayShip ship : shiplist)
96             for(BenkoBox port : ship.getBenkoBoxes())
97                 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
98                     outputports.add(port);
99         FabricTree outputs =
100             new FabricTree((SlipwayBenkoBox[])outputports.toArray(new SlipwayBenkoBox[0]),
101                            "funnel",
102                            "source");
103
104         ArrayList ihornports = new ArrayList<SlipwayBenkoBox>();
105         for(SlipwayShip ship : shiplist)
106             for(BenkoBox port : ship.getBenkoBoxes())
107                 if (((SlipwayBenkoBox)port).ihorn())
108                     ihornports.add(port);
109         FabricTree ihorns =
110             new FabricTree((SlipwayBenkoBox[])ihornports.toArray(new SlipwayBenkoBox[0]),
111                            "funnel",
112                            "ihorn");
113         
114         if (quiet) return;
115         System.out.println("`include \"macros.v\"");
116         System.out.println("module fabric(clk, data_Memory0_command_r, data_Memory0_command_a, data_Memory0_command,");
117         System.out.println("                   data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
118         System.out.println("  input  clk;");
119         System.out.println("  input  data_Memory0_command_r;");
120         System.out.println("  output data_Memory0_command_a;");
121         System.out.println("  output data_Debug0_out_r;");
122         System.out.println("  input  data_Debug0_out_a;");
123         System.out.println("  output [(`PACKET_WIDTH-1):0]      data_Debug0_out;");
124         System.out.println("  input  [(`PACKET_WIDTH-1):0]      data_Memory0_command;");
125         //System.out.println("  wire   [(`INSTRUCTION_WIDTH-1):0] data_Memory0_ihorn;");
126         //System.out.println("  wire   [(`PACKET_WIDTH-1):0]      data_Memory0_dhorn;");
127         System.out.println();
128         
129         System.out.println();
130
131         instructions.dumpChannels(true);
132         outputs.dumpChannels(true);
133         inputs.dumpChannels(true);
134         ihorns.dumpChannels(true);
135         for(SlipwayShip ship : shiplist)
136             for(BenkoBox port : ship.getBenkoBoxes())
137                 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
138                     System.out.println("  wire [(`PACKET_WIDTH-1):0] data_"
139                                        +getUniqueName(ship)+"_"+port.getName()+";");
140
141         System.out.println("");
142         instructions.dumpChannels(false);
143         System.out.println("");
144         outputs.dumpChannels(false);
145         System.out.println("");
146         inputs.dumpChannels(false);
147         System.out.println("");
148         ihorns.dumpChannels(false);
149         System.out.println("");
150         for(SlipwayShip ship : shiplist) {
151             System.out.print(ship.getType().toLowerCase());
152             System.out.print(" ");
153             System.out.print("krunk"+(krunk++));
154             System.out.print("(clk, ");
155             boolean first = true;
156             for(BenkoBox port : ship.getBenkoBoxes()) {
157                 if (!first) System.out.print(", ");
158                 first = false;
159                 String prefix = "data_";
160                 if (((SlipwayBenkoBox)port).ihorn()) prefix = "ihorn_";
161                 if (((SlipwayBenkoBox)port).dhorn()) prefix = "source_";
162                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
163                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
164                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
165                 System.out.print(" ");
166             }
167             System.out.println(");");
168
169             for(BenkoBox port : ship.getBenkoBoxes()) {
170                 if (((SlipwayBenkoBox)port).special()) continue;
171                 if (((SlipwayBenkoBox)port).inbox) {
172                     System.out.print("inbox");
173                 } else {
174                     System.out.print("outbox");
175                 }
176                 System.out.print(" krunk"+(krunk++)+"(clk, ");
177                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
178                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
179                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
180                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
181                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
182                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
183                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
184                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
185                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
186                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
187                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
188                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
189                 System.out.print(");");
190                 System.out.println();
191             }
192
193         }
194         /*
195         System.out.println("funnel topfun(clk,"+
196                            "              dest_r, dest_a, dest,"+
197                            "              source_r, source_a, source,"+
198                            "              data_Memory0_dhorn_r, data_Memory0_dhorn_a, data_Memory0_dhorn);");
199         */
200         System.out.println("assign instruction_r = ihorn_r;");
201         System.out.println("assign ihorn_a = instruction_a;");
202         System.out.println("assign instruction = ihorn;");
203         System.out.println("assign dest_r = source_r;");
204         System.out.println("assign source_a = dest_a;");
205         System.out.println("assign dest = source;");
206         System.out.println("endmodule");
207     }
208
209     private static class FabricTree {
210         int master_idx = 1;
211         String prefix;
212         Node root;
213         public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
214         public FabricTree(SlipwayBenkoBox[] ports, String component, String prefix) {
215             this.prefix = prefix;
216             root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
217         }
218         private Object mkNode(String name, String component, SlipwayBenkoBox[] ports,
219                               int start, int end, int addr, int bits) {
220             if (end-start == 0) return null;
221             if (end-start == 1) {
222                 SlipwayBenkoBox p = ports[start];
223                 if (prefix.equals("instruction")) {
224                     p.instr_addr = addr;
225                     p.instr_bits = bits;
226                 } else if (prefix.equals("dest")) {
227                     p.addr = addr;
228                     p.bits = bits;
229                     int count = 0;
230                     for(Destination d : p.getDestinations()) {
231                         if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
232                         SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
233                         vp.addr = p.addr | (count << bits);
234                         count++;
235                     }
236                 }
237                 return p;
238             }
239             int len = end-start;
240             return new Node(name,
241                             component,
242                             mkNode(name+"_0", component, ports, start, start+len/2, addr,               bits+1),
243                             mkNode(name+"_1", component, ports, start+len/2, end,   addr | (1 << bits), bits+1),
244                             addr,
245                             bits);
246         }
247         private String describe(String prefix, Object o) {
248             if (o==null) return null;
249             if (o instanceof SlipwayBenkoBox) {
250                 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
251                 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
252             }
253             if (o instanceof Node) {
254                 return ((Node)o).describe(prefix);
255             }
256             return null;
257         }
258         private class Node {
259             Object left;
260             Object right;
261             String name;
262             String component;
263             int addr;
264             int bits;
265             public Node(String name, String component, Object left, Object right, int addr, int bits) {
266                 this.left = left;
267                 this.right = right;
268                 this.name = name;
269                 this.component = component;
270                 this.addr = addr;
271                 this.bits = bits;
272             }
273             public void dumpChannels(int indentamount, boolean decl) {
274                 String indent = "";
275                 for(int i=0; i<indentamount; i++) indent += "  ";
276                 if (decl) {
277                     String n = describe(prefix).startsWith("instruction")
278                         ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
279                     System.out.println("  wire "+n+" "+indent+describe(prefix)+";");
280                 } else {
281                     System.out.println("     "+indent+
282                                        component+" "+
283                                        "krunk"+(krunk++)+"(clk, "+
284                                        describe(prefix)+"_r, "+
285                                        describe(prefix)+"_a, "+
286                                        describe(prefix)+", "+
287                                        FabricTree.this.describe(prefix, left)+"_r, "+
288                                        FabricTree.this.describe(prefix, left)+"_a, "+
289                                        FabricTree.this.describe(prefix, left)+", "+
290                                        FabricTree.this.describe(prefix, right)+"_r, "+
291                                        FabricTree.this.describe(prefix, right)+"_a, "+
292                                        FabricTree.this.describe(prefix, right)+
293                                        ");");
294                 }
295                 dumpChannels(left, indentamount+1, decl);
296                 dumpChannels(right, indentamount+1, decl);
297             }
298             public void dumpChannels(Object o, int indentamount, boolean decl) {
299                 if (o==null) return;
300                 if (o instanceof Node) {
301                     ((Node)o).dumpChannels(indentamount, decl);
302                 } else {
303                     String indent = "";
304                     for(int i=0; i<indentamount; i++) indent += "  ";
305                     if (decl) {
306                         String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
307                             ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
308                         System.out.println("  wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
309                     }
310                 }
311             }
312             public String describe(String prefix) {
313                 return prefix+name;
314             }
315         }
316     }
317     public static int krunk=0;
318
319     private static String getUniqueName(Ship ship) {
320         return ship.getType() + ship.getOrdinal();
321     }
322
323     public void expand(ShipDescription sd) {
324         try {
325             String filename = sd.getName().toLowerCase();
326             File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
327             new File(outf.getParent()).mkdirs();
328             System.err.println("writing to " + outf);
329             FileOutputStream out = new FileOutputStream(outf);
330             PrintWriter pw = new PrintWriter(out);
331
332             boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice") || filename.equals("alu3") || filename.equals("stack");
333             if (auto) {
334                 pw.println("`include \"macros.v\"");
335                 pw.println();
336                 pw.println("module " + filename + "( clk");
337                 for(BenkoBoxDescription bb : sd) {
338                     String bb_name = bb.getName();
339                     pw.print("        ");
340                     if (bb.isInbox()) {
341                         pw.print(", " + bb_name+"_r");
342                         pw.print(", " + bb_name+"_a_");
343                         pw.print(", " + bb_name+"_d");
344                     } else {
345                         pw.print(", " + bb_name+"_r_");
346                         pw.print(", " + bb_name+"_a");
347                         pw.print(", " + bb_name+"_d_");
348                     }
349                     pw.println();
350                 }
351                 pw.println("        );");
352                 pw.println();
353                 pw.println("    input clk;");
354                 for(BenkoBoxDescription bb : sd) {
355                     String bb_name = bb.getName();
356                     pw.print("        ");
357                     if (bb.isInbox()) {
358                         pw.println("`input(" +
359                                    bb_name+"_r,  "+
360                                    bb_name+"_a,  "+
361                                    bb_name+"_a_, "+
362                                    "[(`PACKET_WIDTH-1):0],"+
363                                    bb_name+"_d)"
364                                    );
365                     } else {
366                         pw.println("`output(" +
367                                    bb_name+"_r,  "+
368                                    bb_name+"_r_, "+
369                                    bb_name+"_a,  "+
370                                    "[(`PACKET_WIDTH-1):0],"+
371                                    bb_name+"_d_)"
372                                    );
373                         pw.println("`defreg(" +
374                                    bb_name+"_d_,  "+
375                                    "[(`PACKET_WIDTH-1):0],"+
376                                    bb_name+"_d)"
377                                    );
378                     }
379                     pw.println();
380                 }
381             }
382
383             pw.println(sd.getSection("fpga"));
384
385             if (auto)
386                 pw.println("endmodule");
387
388             pw.flush();
389             pw.close();
390         } catch (Exception e) { throw new RuntimeException(e); }
391     }
392
393     public int computeOffset(int origin, int target) { return (target - origin)/6; }
394     public int computeTarget(int origin, int offset) { return origin + (offset*6); }
395
396     private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
397     public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
398     public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
399     public long writeInstruction(Instruction d) { return writeInstruction(d); }
400     public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
401
402     private class SlipwayInstructionEncoder extends InstructionEncoder {
403         public long getDestAddr(Destination box) {
404             long ret;
405             if (box instanceof SlipwayBenkoBox)
406                 ret = ((SlipwayBenkoBox)box).addr;
407             else
408                 ret = ((SlipwayBenkoBox.VirtualPort)box).addr;
409             return ret;
410         }
411         public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
412         public Destination getDestByAddr(long dest) {
413             for(Ship ship : Slipway.this)
414                 for(BenkoBox bb : ship.getBenkoBoxes()) {
415                     if (((SlipwayBenkoBox)bb).addr == dest)
416                         return bb;
417                     for(Destination d : bb.getDestinations())
418                         if (getDestAddr(d)==dest)
419                             return d;
420                 }
421             return null;
422         }
423         public BenkoBox getBoxByInstAddr(long dest) {
424             for(Ship ship : Slipway.this)
425                 for(BenkoBox bb : ship.getBenkoBoxes())
426                     if (((SlipwayBenkoBox)bb).instr_addr == dest)
427                         return bb;
428             return null;
429         }
430     }
431
432 }