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