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("superbowl.bit"); }
22 public Slipway(String bitfile) {
23 this.bitfile = bitfile;
24 createShip("Debug", "debug");
25 createShip("Fifo", "fifo1");
26 createShip("Alu2", "alu2a");
27 createShip("Alu2", "alu2b");
28 createShip("Alu2", "alu2c");
29 createShip("Alu2", "alu2d");
30 createShip("Debug", "debug");
31 //createShip("Execute", "execute");
32 createShip("Fifo", "fifo1");
33 createShip("Fifo", "fifo2");
34 createShip("Fifo", "fifo3");
35 createShip("Fifo", "fifo4");
36 createShip("Iscratch", "iscratch1");
37 createShip("Iscratch", "iscratch2");
38 createShip("Dscratch", "dscratch1");
39 createShip("Dscratch", "dscratch2");
43 public Ship createShip(String type, String name) {
44 InterpreterShip ship = (InterpreterShip)super.createShip(type, name);
45 if (ship.getClass().getSimpleName().equals("Debug")) {
46 new DataOutbox(ship, "out", true);
48 } else if (ship.getClass().getSimpleName().equals("Execute")) {
49 new DataOutbox(ship, "ihorn", true, true, false);
50 new DataOutbox(ship, "dhorn", true, false, true);
52 } else if (ship.getClass().getSimpleName().equals("Iscratch")) {
53 new DataInbox(ship, "command", true);
54 new DataOutbox(ship, "ihorn", true, true, false);
55 new DataOutbox(ship, "dhorn", true, false, true);
60 public FleetProcess run(final byte[] instructions) {
62 return new Client(bitfile, instructions);
63 } catch (IOException e) { throw new RuntimeException(e); }
66 public void dumpFabric(boolean quiet) {
67 // FIXME: this is really ugly: the order of port declarations in
68 // the XXXShip.java file must match the order in the .balsa file!
70 ArrayList instructionports = new ArrayList<InterpreterBenkoBox>();
71 for(InterpreterShip ship : shiplist)
72 for(BenkoBox port : ship.getBenkoBoxes())
73 if (!((InterpreterBenkoBox)port).special())
74 instructionports.add(port);
75 FabricTree instructions =
76 new FabricTree((InterpreterBenkoBox[])instructionports.toArray(new InterpreterBenkoBox[0]),
80 ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
81 for(InterpreterShip ship : shiplist)
82 for(BenkoBox port : ship.getBenkoBoxes())
83 if (!((InterpreterBenkoBox)port).special())
86 new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
90 ArrayList outputports = new ArrayList<InterpreterBenkoBox>();
91 for(InterpreterShip ship : shiplist)
92 for(BenkoBox port : ship.getBenkoBoxes())
93 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
94 outputports.add(port);
96 new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
100 ArrayList ihornports = new ArrayList<InterpreterBenkoBox>();
101 for(InterpreterShip ship : shiplist)
102 for(BenkoBox port : ship.getBenkoBoxes())
103 if (((InterpreterBenkoBox)port).ihorn())
104 ihornports.add(port);
106 new FabricTree((InterpreterBenkoBox[])ihornports.toArray(new InterpreterBenkoBox[0]),
111 System.out.println("`include \"macros.v\"");
112 System.out.println("module fabric(clk, data_Iscratch0_command_r, data_Iscratch0_command_a, data_Iscratch0_command,");
113 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
114 System.out.println(" input clk;");
115 System.out.println(" input data_Iscratch0_command_r;");
116 System.out.println(" output data_Iscratch0_command_a;");
117 System.out.println(" output data_Debug0_out_r;");
118 System.out.println(" input data_Debug0_out_a;");
119 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
120 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Iscratch0_command;");
121 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Iscratch0_ihorn;");
122 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Iscratch0_dhorn;");
123 System.out.println();
125 System.out.println();
127 instructions.dumpChannels(true);
128 outputs.dumpChannels(true);
129 inputs.dumpChannels(true);
130 ihorns.dumpChannels(true);
131 for(InterpreterShip ship : shiplist)
132 for(BenkoBox port : ship.getBenkoBoxes())
133 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
134 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
135 +getUniqueName(ship)+"_"+port.getName()+";");
137 System.out.println("");
138 instructions.dumpChannels(false);
139 System.out.println("");
140 outputs.dumpChannels(false);
141 System.out.println("");
142 inputs.dumpChannels(false);
143 System.out.println("");
144 ihorns.dumpChannels(false);
145 System.out.println("");
146 for(InterpreterShip ship : shiplist) {
147 System.out.print(ship.getClass().getSimpleName().toLowerCase());
148 System.out.print(" ");
149 System.out.print("krunk"+(krunk++));
150 System.out.print("(clk, ");
151 boolean first = true;
152 for(BenkoBox port : ship.getBenkoBoxes()) {
153 if (!first) System.out.print(", ");
155 String prefix = "data_";
156 if (((InterpreterBenkoBox)port).ihorn()) prefix = "ihorn_";
157 if (((InterpreterBenkoBox)port).dhorn()) prefix = "source_";
158 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
159 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
160 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
161 System.out.print(" ");
163 System.out.println(");");
165 for(BenkoBox port : ship.getBenkoBoxes()) {
166 if (((InterpreterBenkoBox)port).special()) continue;
167 if (port instanceof Inbox) {
168 System.out.print("inbox");
170 System.out.print("outbox");
172 System.out.print(" krunk"+(krunk++)+"(clk, ");
173 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
174 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
175 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
176 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
177 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
178 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
179 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
180 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
181 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
182 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
183 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
184 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
185 System.out.print(");");
186 System.out.println();
191 System.out.println("funnel topfun(clk,"+
192 " dest_r, dest_a, dest,"+
193 " source_r, source_a, source,"+
194 " data_Iscratch0_dhorn_r, data_Iscratch0_dhorn_a, data_Iscratch0_dhorn);");
196 System.out.println("assign instruction_r = ihorn_r;");
197 System.out.println("assign ihorn_a = instruction_a;");
198 System.out.println("assign instruction = ihorn;");
199 System.out.println("assign dest_r = source_r;");
200 System.out.println("assign source_a = dest_a;");
201 System.out.println("assign dest = source;");
202 System.out.println("endmodule");
205 private static class FabricTree {
209 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
210 public FabricTree(InterpreterBenkoBox[] ports, String component, String prefix) {
211 this.prefix = prefix;
212 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
214 private Object mkNode(String name, String component, InterpreterBenkoBox[] ports,
215 int start, int end, int addr, int bits) {
216 if (end-start == 0) return null;
217 if (end-start == 1) {
218 InterpreterBenkoBox p = ports[start];
219 if (prefix.equals("instruction")) {
222 } else if (prefix.equals("dest")) {
229 return new Node(name,
231 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
232 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
236 private String describe(String prefix, Object o) {
237 if (o==null) return null;
238 if (o instanceof InterpreterBenkoBox) {
239 InterpreterBenkoBox p = (InterpreterBenkoBox)o;
240 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
242 if (o instanceof Node) {
243 return ((Node)o).describe(prefix);
254 public Node(String name, String component, Object left, Object right, int addr, int bits) {
258 this.component = component;
262 public void dumpChannels(int indentamount, boolean decl) {
264 for(int i=0; i<indentamount; i++) indent += " ";
266 String n = describe(prefix).startsWith("instruction")
267 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
268 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
270 System.out.println(" "+indent+
272 "krunk"+(krunk++)+"(clk, "+
273 describe(prefix)+"_r, "+
274 describe(prefix)+"_a, "+
275 describe(prefix)+", "+
276 FabricTree.this.describe(prefix, left)+"_r, "+
277 FabricTree.this.describe(prefix, left)+"_a, "+
278 FabricTree.this.describe(prefix, left)+", "+
279 FabricTree.this.describe(prefix, right)+"_r, "+
280 FabricTree.this.describe(prefix, right)+"_a, "+
281 FabricTree.this.describe(prefix, right)+
284 dumpChannels(left, indentamount+1, decl);
285 dumpChannels(right, indentamount+1, decl);
287 public void dumpChannels(Object o, int indentamount, boolean decl) {
289 if (o instanceof Node) {
290 ((Node)o).dumpChannels(indentamount, decl);
293 for(int i=0; i<indentamount; i++) indent += " ";
295 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
296 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
297 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
301 public String describe(String prefix) {
306 public static int krunk=0;
308 private static String getUniqueName(Ship ship) {
309 return ship.getType() + ship.getOrdinal();
312 public void expand(ShipDescription sd) {
314 String filename = sd.name.toLowerCase();
315 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
316 new File(outf.getParent()).mkdirs();
317 System.err.println("writing to " + outf);
318 FileOutputStream out = new FileOutputStream(outf);
319 PrintWriter pw = new PrintWriter(out);
321 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3");
323 pw.println("`include \"macros.v\"");
325 pw.println("module " + filename + "( clk");
326 for(ShipDescription.BenkoBox bb : sd.benkoBoxes) {
327 if (bb.ports.length > 1) throw new RuntimeException("gah");
328 String bb_name = bb.ports[0];
331 pw.print(", " + bb_name+"_r");
332 pw.print(", " + bb_name+"_a_");
333 pw.print(", " + bb_name+"_d");
335 pw.print(", " + bb_name+"_r_");
336 pw.print(", " + bb_name+"_a");
337 pw.print(", " + bb_name+"_d_");
343 pw.println(" input clk;");
344 for(ShipDescription.BenkoBox bb : sd.benkoBoxes) {
345 if (bb.ports.length > 1) throw new RuntimeException("gah");
346 String bb_name = bb.ports[0];
349 pw.println("`input(" +
353 "[(`DATAWIDTH-1):0],"+
357 pw.println("`output(" +
361 "[(`DATAWIDTH-1):0],"+
364 pw.println("`defreg(" +
366 "[(`DATAWIDTH-1):0],"+
374 pw.println(sd.sections.get("fpga"));
377 pw.println("endmodule");
381 } catch (Exception e) { throw new RuntimeException(e); }