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