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