new syntax: receive, notify, dismiss
[fleet.git] / src / edu / berkeley / fleet / slipway / Slipway.java
1 package edu.berkeley.fleet.slipway;
2 import edu.berkeley.fleet.interpreter.*;
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 import edu.berkeley.fleet.interpreter.ships.*;
16
17 public class Slipway extends Interpreter {
18
19     private String bitfile;
20
21     public Slipway() { this("superbowl3.bit"); }
22     public Slipway(String bitfile) {
23         this.bitfile = bitfile;
24         createShip("Debug",   "debug");
25         createShip("Alu2",    "alu2a");
26         createShip("Alu2",    "alu2b");
27         createShip("Alu2",    "alu2c");
28         createShip("Alu2",    "alu2d");
29         createShip("Execute", "execute");
30         createShip("Fifo",    "fifo1");
31         createShip("Fifo",    "fifo2");
32         createShip("Fifo",    "fifo3");
33         createShip("Fifo",    "fifo4");
34         // FIXME cant have less than two feeding the instruction horn or BUG
35         createShip("Iscratch",  "iscratch1");
36         createShip("Iscratch",  "iscratch2");
37         createShip("Dscratch",  "dscratch1");
38         createShip("Dscratch",  "dscratch2");
39         createShip("Lut3",      "lut3");
40         createShip("Alu1",      "alu1");
41         dumpFabric(true);
42     }
43
44     public Ship createShip(String type, String name) {
45         InterpreterShip ship = (InterpreterShip)super.createShip(type, name);
46         if        (ship.getClass().getSimpleName().equals("Debug")) {
47             new DataOutbox(ship, "out", true);
48             
49         } else if (ship.getClass().getSimpleName().equals("Execute")) {
50             new DataOutbox(ship, "ihorn", true, true, false);
51             new DataOutbox(ship, "dhorn", true, false, true);
52             
53         } else if (ship.getClass().getSimpleName().equals("Iscratch")) {
54             new DataInbox(ship,  "command", true);
55             new DataOutbox(ship, "ihorn",   true, true, false);
56             new DataOutbox(ship, "dhorn",   true, false, true);
57         }
58         return ship;
59     }
60
61     public FleetProcess run(final byte[] instructions) {
62         try {
63             return new Client(bitfile, instructions);
64         } catch (IOException e) { throw new RuntimeException(e); }
65     }
66
67     public void dumpFabric(boolean quiet) {
68         // FIXME: this is really ugly: the order of port declarations in
69         //        the XXXShip.java file must match the order in the .balsa file!
70
71         ArrayList instructionports = new ArrayList<InterpreterBenkoBox>();
72         for(InterpreterShip ship : shiplist)
73             for(BenkoBox port : ship.getBenkoBoxes())
74                 if (!((InterpreterBenkoBox)port).special())
75                     instructionports.add(port);
76         FabricTree instructions =
77             new FabricTree((InterpreterBenkoBox[])instructionports.toArray(new InterpreterBenkoBox[0]),
78                            "ihorn",
79                            "instruction");
80
81         ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
82         for(InterpreterShip ship : shiplist)
83             for(BenkoBox port : ship.getBenkoBoxes())
84                 if (!((InterpreterBenkoBox)port).special())
85                     inputports.add(port);
86         FabricTree inputs =
87             new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
88                            "horn",
89                            "dest");
90
91         ArrayList outputports = new ArrayList<InterpreterBenkoBox>();
92         for(InterpreterShip ship : shiplist)
93             for(BenkoBox port : ship.getBenkoBoxes())
94                 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
95                     outputports.add(port);
96         FabricTree outputs =
97             new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
98                            "funnel",
99                            "source");
100
101         ArrayList ihornports = new ArrayList<InterpreterBenkoBox>();
102         for(InterpreterShip ship : shiplist)
103             for(BenkoBox port : ship.getBenkoBoxes())
104                 if (((InterpreterBenkoBox)port).ihorn())
105                     ihornports.add(port);
106         FabricTree ihorns =
107             new FabricTree((InterpreterBenkoBox[])ihornports.toArray(new InterpreterBenkoBox[0]),
108                            "funnel",
109                            "ihorn");
110         
111         if (quiet) return;
112         System.out.println("`include \"macros.v\"");
113         System.out.println("module fabric(clk, data_Iscratch0_command_r, data_Iscratch0_command_a, data_Iscratch0_command,");
114         System.out.println("                   data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
115         System.out.println("  input  clk;");
116         System.out.println("  input  data_Iscratch0_command_r;");
117         System.out.println("  output data_Iscratch0_command_a;");
118         System.out.println("  output data_Debug0_out_r;");
119         System.out.println("  input  data_Debug0_out_a;");
120         System.out.println("  output [(`PACKET_WIDTH-1):0]      data_Debug0_out;");
121         System.out.println("  input  [(`PACKET_WIDTH-1):0]      data_Iscratch0_command;");
122         //System.out.println("  wire   [(`INSTRUCTION_WIDTH-1):0] data_Iscratch0_ihorn;");
123         //System.out.println("  wire   [(`PACKET_WIDTH-1):0]      data_Iscratch0_dhorn;");
124         System.out.println();
125         
126         System.out.println();
127
128         instructions.dumpChannels(true);
129         outputs.dumpChannels(true);
130         inputs.dumpChannels(true);
131         ihorns.dumpChannels(true);
132         for(InterpreterShip ship : shiplist)
133             for(BenkoBox port : ship.getBenkoBoxes())
134                 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
135                     System.out.println("  wire [(`PACKET_WIDTH-1):0] data_"
136                                        +getUniqueName(ship)+"_"+port.getName()+";");
137
138         System.out.println("");
139         instructions.dumpChannels(false);
140         System.out.println("");
141         outputs.dumpChannels(false);
142         System.out.println("");
143         inputs.dumpChannels(false);
144         System.out.println("");
145         ihorns.dumpChannels(false);
146         System.out.println("");
147         for(InterpreterShip ship : shiplist) {
148             System.out.print(ship.getClass().getSimpleName().toLowerCase());
149             System.out.print(" ");
150             System.out.print("krunk"+(krunk++));
151             System.out.print("(clk, ");
152             boolean first = true;
153             for(BenkoBox port : ship.getBenkoBoxes()) {
154                 if (!first) System.out.print(", ");
155                 first = false;
156                 String prefix = "data_";
157                 if (((InterpreterBenkoBox)port).ihorn()) prefix = "ihorn_";
158                 if (((InterpreterBenkoBox)port).dhorn()) prefix = "source_";
159                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
160                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
161                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
162                 System.out.print(" ");
163             }
164             System.out.println(");");
165
166             for(BenkoBox port : ship.getBenkoBoxes()) {
167                 if (((InterpreterBenkoBox)port).special()) continue;
168                 if (port instanceof Inbox) {
169                     System.out.print("inbox");
170                 } else {
171                     System.out.print("outbox");
172                 }
173                 System.out.print(" krunk"+(krunk++)+"(clk, ");
174                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
175                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
176                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
177                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
178                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
179                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
180                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
181                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
182                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
183                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
184                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
185                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
186                 System.out.print(");");
187                 System.out.println();
188             }
189
190         }
191         /*
192         System.out.println("funnel topfun(clk,"+
193                            "              dest_r, dest_a, dest,"+
194                            "              source_r, source_a, source,"+
195                            "              data_Iscratch0_dhorn_r, data_Iscratch0_dhorn_a, data_Iscratch0_dhorn);");
196         */
197         System.out.println("assign instruction_r = ihorn_r;");
198         System.out.println("assign ihorn_a = instruction_a;");
199         System.out.println("assign instruction = ihorn;");
200         System.out.println("assign dest_r = source_r;");
201         System.out.println("assign source_a = dest_a;");
202         System.out.println("assign dest = source;");
203         System.out.println("endmodule");
204     }
205
206     private static class FabricTree {
207         int master_idx = 1;
208         String prefix;
209         Node root;
210         public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
211         public FabricTree(InterpreterBenkoBox[] ports, String component, String prefix) {
212             this.prefix = prefix;
213             root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
214         }
215         private Object mkNode(String name, String component, InterpreterBenkoBox[] ports,
216                               int start, int end, int addr, int bits) {
217             if (end-start == 0) return null;
218             if (end-start == 1) {
219                 InterpreterBenkoBox p = ports[start];
220                 if (prefix.equals("instruction")) {
221                     p.instr_addr = addr;
222                     p.instr_bits = bits;
223                 } else if (prefix.equals("dest")) {
224                     p.addr = addr;
225                     p.bits = bits;
226                 }
227                 return p;
228             }
229             int len = end-start;
230             return new Node(name,
231                             component,
232                             mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
233                             mkNode(name+"_1", component, ports, start+len/2, end,   addr | (1 << bits), bits+1),
234                             addr,
235                             bits);
236         }
237         private String describe(String prefix, Object o) {
238             if (o==null) return null;
239             if (o instanceof InterpreterBenkoBox) {
240                 InterpreterBenkoBox p = (InterpreterBenkoBox)o;
241                 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
242             }
243             if (o instanceof Node) {
244                 return ((Node)o).describe(prefix);
245             }
246             return null;
247         }
248         private class Node {
249             Object left;
250             Object right;
251             String name;
252             String component;
253             int addr;
254             int bits;
255             public Node(String name, String component, Object left, Object right, int addr, int bits) {
256                 this.left = left;
257                 this.right = right;
258                 this.name = name;
259                 this.component = component;
260                 this.addr = addr;
261                 this.bits = bits;
262             }
263             public void dumpChannels(int indentamount, boolean decl) {
264                 String indent = "";
265                 for(int i=0; i<indentamount; i++) indent += "  ";
266                 if (decl) {
267                     String n = describe(prefix).startsWith("instruction")
268                         ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
269                     System.out.println("  wire "+n+" "+indent+describe(prefix)+";");
270                 } else {
271                     System.out.println("     "+indent+
272                                        component+" "+
273                                        "krunk"+(krunk++)+"(clk, "+
274                                        describe(prefix)+"_r, "+
275                                        describe(prefix)+"_a, "+
276                                        describe(prefix)+", "+
277                                        FabricTree.this.describe(prefix, left)+"_r, "+
278                                        FabricTree.this.describe(prefix, left)+"_a, "+
279                                        FabricTree.this.describe(prefix, left)+", "+
280                                        FabricTree.this.describe(prefix, right)+"_r, "+
281                                        FabricTree.this.describe(prefix, right)+"_a, "+
282                                        FabricTree.this.describe(prefix, right)+
283                                        ");");
284                 }
285                 dumpChannels(left, indentamount+1, decl);
286                 dumpChannels(right, indentamount+1, decl);
287             }
288             public void dumpChannels(Object o, int indentamount, boolean decl) {
289                 if (o==null) return;
290                 if (o instanceof Node) {
291                     ((Node)o).dumpChannels(indentamount, decl);
292                 } else {
293                     String indent = "";
294                     for(int i=0; i<indentamount; i++) indent += "  ";
295                     if (decl) {
296                         String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
297                             ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
298                         System.out.println("  wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
299                     }
300                 }
301             }
302             public String describe(String prefix) {
303                 return prefix+name;
304             }
305         }
306     }
307     public static int krunk=0;
308
309     private static String getUniqueName(Ship ship) {
310         return ship.getType() + ship.getOrdinal();
311     }
312
313     public void expand(ShipDescription sd) {
314         try {
315             String filename = sd.name.toLowerCase();
316             File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
317             new File(outf.getParent()).mkdirs();
318             System.err.println("writing to " + outf);
319             FileOutputStream out = new FileOutputStream(outf);
320             PrintWriter pw = new PrintWriter(out);
321
322             boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3");
323             if (auto) {
324                 pw.println("`include \"macros.v\"");
325                 pw.println();
326                 pw.println("module " + filename + "( clk");
327                 for(ShipDescription.BenkoBox bb : sd.benkoBoxes) {
328                     if (bb.ports.length > 1) throw new RuntimeException("gah");
329                     String bb_name = bb.ports[0];
330                     pw.print("        ");
331                     if (bb.inbox) {
332                         pw.print(", " + bb_name+"_r");
333                         pw.print(", " + bb_name+"_a_");
334                         pw.print(", " + bb_name+"_d");
335                     } else {
336                         pw.print(", " + bb_name+"_r_");
337                         pw.print(", " + bb_name+"_a");
338                         pw.print(", " + bb_name+"_d_");
339                     }
340                     pw.println();
341                 }
342                 pw.println("        );");
343                 pw.println();
344                 pw.println("    input clk;");
345                 for(ShipDescription.BenkoBox bb : sd.benkoBoxes) {
346                     if (bb.ports.length > 1) throw new RuntimeException("gah");
347                     String bb_name = bb.ports[0];
348                     pw.print("        ");
349                     if (bb.inbox) {
350                         pw.println("`input(" +
351                                    bb_name+"_r,  "+
352                                    bb_name+"_a,  "+
353                                    bb_name+"_a_, "+
354                                    "[(`DATAWIDTH-1):0],"+
355                                    bb_name+"_d)"
356                                    );
357                     } else {
358                         pw.println("`output(" +
359                                    bb_name+"_r,  "+
360                                    bb_name+"_r_, "+
361                                    bb_name+"_a,  "+
362                                    "[(`DATAWIDTH-1):0],"+
363                                    bb_name+"_d_)"
364                                    );
365                         pw.println("`defreg(" +
366                                    bb_name+"_d_,  "+
367                                    "[(`DATAWIDTH-1):0],"+
368                                    bb_name+"_d)"
369                                    );
370                     }
371                     pw.println();
372                 }
373             }
374
375             pw.println(sd.sections.get("fpga"));
376
377             if (auto)
378                 pw.println("endmodule");
379
380             pw.flush();
381             pw.close();
382         } catch (Exception e) { throw new RuntimeException(e); }
383     }
384
385 }