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