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.*;
15 import edu.berkeley.fleet.interpreter.ships.*;
17 public class Slipway extends Interpreter {
19 private String bitfile;
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");
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);
49 } else if (ship.getClass().getSimpleName().equals("Execute")) {
50 new DataOutbox(ship, "ihorn", true, true, false);
51 new DataOutbox(ship, "dhorn", true, false, true);
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);
61 public FleetProcess run(final byte[] instructions) {
63 return new Client(bitfile, instructions);
64 } catch (IOException e) { throw new RuntimeException(e); }
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!
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]),
81 ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
82 for(InterpreterShip ship : shiplist)
83 for(BenkoBox port : ship.getBenkoBoxes())
84 if (!((InterpreterBenkoBox)port).special())
87 new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
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);
97 new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
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);
107 new FabricTree((InterpreterBenkoBox[])ihornports.toArray(new InterpreterBenkoBox[0]),
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();
126 System.out.println();
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()+";");
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(", ");
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(" ");
164 System.out.println(");");
166 for(BenkoBox port : ship.getBenkoBoxes()) {
167 if (((InterpreterBenkoBox)port).special()) continue;
168 if (port instanceof Inbox) {
169 System.out.print("inbox");
171 System.out.print("outbox");
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();
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);");
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");
206 private static class FabricTree {
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);
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")) {
223 } else if (prefix.equals("dest")) {
230 return new Node(name,
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),
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();
243 if (o instanceof Node) {
244 return ((Node)o).describe(prefix);
255 public Node(String name, String component, Object left, Object right, int addr, int bits) {
259 this.component = component;
263 public void dumpChannels(int indentamount, boolean decl) {
265 for(int i=0; i<indentamount; i++) indent += " ";
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)+";");
271 System.out.println(" "+indent+
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)+
285 dumpChannels(left, indentamount+1, decl);
286 dumpChannels(right, indentamount+1, decl);
288 public void dumpChannels(Object o, int indentamount, boolean decl) {
290 if (o instanceof Node) {
291 ((Node)o).dumpChannels(indentamount, decl);
294 for(int i=0; i<indentamount; i++) indent += " ";
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)+";");
302 public String describe(String prefix) {
307 public static int krunk=0;
309 private static String getUniqueName(Ship ship) {
310 return ship.getType() + ship.getOrdinal();
313 public void expand(ShipDescription sd) {
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);
322 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3");
324 pw.println("`include \"macros.v\"");
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];
332 pw.print(", " + bb_name+"_r");
333 pw.print(", " + bb_name+"_a_");
334 pw.print(", " + bb_name+"_d");
336 pw.print(", " + bb_name+"_r_");
337 pw.print(", " + bb_name+"_a");
338 pw.print(", " + bb_name+"_d_");
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];
350 pw.println("`input(" +
354 "[(`DATAWIDTH-1):0],"+
358 pw.println("`output(" +
362 "[(`DATAWIDTH-1):0],"+
365 pw.println("`defreg(" +
367 "[(`DATAWIDTH-1):0],"+
375 pw.println(sd.sections.get("fpga"));
378 pw.println("endmodule");
382 } catch (Exception e) { throw new RuntimeException(e); }