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