1 package edu.berkeley.fleet.slipway;
2 import edu.berkeley.fleet.slipway.*;
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.util.*;
15 public class Slipway extends Fleet {
17 public ArrayList<SlipwayShip> shiplist = new ArrayList<SlipwayShip>();
18 public HashMap<String,SlipwayShip> ships = new HashMap<String,SlipwayShip>();
19 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)shiplist.iterator(); }
21 private String bitfile;
23 public static void main(String[] s) throws Exception {
24 new Slipway().dumpFabric(false);
27 public Slipway() { this("groundhog2.bit"); }
28 public Slipway(String bitfile) {
29 this.bitfile = bitfile;
30 createShip("Debug", "debug");
31 createShip("Alu2", "alu2a");
32 createShip("Alu2", "alu2b");
33 createShip("Alu2", "alu2c");
34 createShip("Alu2", "alu2d");
35 createShip("Fifo", "fifo1");
36 createShip("Fifo", "fifo2");
37 createShip("Fifo", "fifo3");
38 createShip("Fifo", "fifo4");
39 createShip("Execute", "execute");
40 createShip("Memory", "Memory");
41 createShip("Memory", "Memory");
42 createShip("Lut3", "lut3");
43 createShip("Alu1", "alu1");
44 createShip("Alu3", "alu3");
45 createShip("Choice", "Choice");
46 createShip("Choice", "Choice");
47 createShip("Choice", "Choice");
48 createShip("Choice", "Choice");
49 createShip("Stack", "Stack");
53 public Ship createShip(String type, String name) {
55 ShipDescription sd = new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
56 SlipwayShip ship = new SlipwayShip(this, name, type, sd);
57 ships.put(name, ship);
60 } catch (IOException e) { throw new RuntimeException(e); }
63 public FleetProcess run(final byte[] instructions) {
65 return new Client(bitfile, instructions);
66 } catch (IOException e) { throw new RuntimeException(e); }
69 public void dumpFabric(boolean quiet) {
70 // FIXME: this is really ugly: the order of port declarations in
71 // the XXXShip.java file must match the order in the .balsa file!
73 ArrayList instructionports = new ArrayList<SlipwayBenkoBox>();
74 for(SlipwayShip ship : shiplist)
75 for(BenkoBox port : ship.getBenkoBoxes())
76 if (!((SlipwayBenkoBox)port).special())
77 instructionports.add(port);
78 FabricTree instructions =
79 new FabricTree((SlipwayBenkoBox[])instructionports.toArray(new SlipwayBenkoBox[0]),
83 ArrayList inputports = new ArrayList<SlipwayBenkoBox>();
84 for(SlipwayShip ship : shiplist)
85 for(BenkoBox port : ship.getBenkoBoxes())
86 if (!((SlipwayBenkoBox)port).special())
89 new FabricTree((SlipwayBenkoBox[])inputports.toArray(new SlipwayBenkoBox[0]),
93 ArrayList outputports = new ArrayList<SlipwayBenkoBox>();
94 for(SlipwayShip ship : shiplist)
95 for(BenkoBox port : ship.getBenkoBoxes())
96 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
97 outputports.add(port);
99 new FabricTree((SlipwayBenkoBox[])outputports.toArray(new SlipwayBenkoBox[0]),
103 ArrayList ihornports = new ArrayList<SlipwayBenkoBox>();
104 for(SlipwayShip ship : shiplist)
105 for(BenkoBox port : ship.getBenkoBoxes())
106 if (((SlipwayBenkoBox)port).ihorn())
107 ihornports.add(port);
109 new FabricTree((SlipwayBenkoBox[])ihornports.toArray(new SlipwayBenkoBox[0]),
114 System.out.println("`include \"macros.v\"");
115 System.out.println("module fabric(clk, data_Memory0_command_r, data_Memory0_command_a, data_Memory0_command,");
116 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
117 System.out.println(" input clk;");
118 System.out.println(" input data_Memory0_command_r;");
119 System.out.println(" output data_Memory0_command_a;");
120 System.out.println(" output data_Debug0_out_r;");
121 System.out.println(" input data_Debug0_out_a;");
122 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
123 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Memory0_command;");
124 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Memory0_ihorn;");
125 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Memory0_dhorn;");
126 System.out.println();
128 System.out.println();
130 instructions.dumpChannels(true);
131 outputs.dumpChannels(true);
132 inputs.dumpChannels(true);
133 ihorns.dumpChannels(true);
134 for(SlipwayShip ship : shiplist)
135 for(BenkoBox port : ship.getBenkoBoxes())
136 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
137 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
138 +getUniqueName(ship)+"_"+port.getName()+";");
140 System.out.println("");
141 instructions.dumpChannels(false);
142 System.out.println("");
143 outputs.dumpChannels(false);
144 System.out.println("");
145 inputs.dumpChannels(false);
146 System.out.println("");
147 ihorns.dumpChannels(false);
148 System.out.println("");
149 for(SlipwayShip ship : shiplist) {
150 System.out.print(ship.getType().toLowerCase());
151 System.out.print(" ");
152 System.out.print("krunk"+(krunk++));
153 System.out.print("(clk, ");
154 boolean first = true;
155 for(BenkoBox port : ship.getBenkoBoxes()) {
156 if (!first) System.out.print(", ");
158 String prefix = "data_";
159 if (((SlipwayBenkoBox)port).ihorn()) prefix = "ihorn_";
160 if (((SlipwayBenkoBox)port).dhorn()) prefix = "source_";
161 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
162 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
163 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
164 System.out.print(" ");
166 System.out.println(");");
168 for(BenkoBox port : ship.getBenkoBoxes()) {
169 if (((SlipwayBenkoBox)port).special()) continue;
170 if (((SlipwayBenkoBox)port).inbox) {
171 System.out.print("inbox");
173 System.out.print("outbox");
175 System.out.print(" krunk"+(krunk++)+"(clk, ");
176 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
177 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
178 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
179 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
180 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
181 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
182 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
183 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
184 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
185 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
186 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
187 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
188 System.out.print(");");
189 System.out.println();
194 System.out.println("funnel topfun(clk,"+
195 " dest_r, dest_a, dest,"+
196 " source_r, source_a, source,"+
197 " data_Memory0_dhorn_r, data_Memory0_dhorn_a, data_Memory0_dhorn);");
199 System.out.println("assign instruction_r = ihorn_r;");
200 System.out.println("assign ihorn_a = instruction_a;");
201 System.out.println("assign instruction = ihorn;");
202 System.out.println("assign dest_r = source_r;");
203 System.out.println("assign source_a = dest_a;");
204 System.out.println("assign dest = source;");
205 System.out.println("endmodule");
208 private static class FabricTree {
212 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
213 public FabricTree(SlipwayBenkoBox[] ports, String component, String prefix) {
214 this.prefix = prefix;
215 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
217 private Object mkNode(String name, String component, SlipwayBenkoBox[] ports,
218 int start, int end, int addr, int bits) {
219 if (end-start == 0) return null;
220 if (end-start == 1) {
221 SlipwayBenkoBox p = ports[start];
222 if (prefix.equals("instruction")) {
225 } else if (prefix.equals("dest")) {
229 throw new RuntimeException("too many benkoboxen!");
231 for(Destination d : p.getDestinations()) {
232 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
233 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
234 vp.addr = p.addr | (count << bits);
244 if (end-start <= 2) {
245 breakpt = (start+end)/2;
247 for(int i=start; i<end; i++)
248 count += count(ports[i].getDestinations());
249 for(int i=start; i<end-1; i++) {
250 count2 += count(ports[i].getDestinations());
252 if (i>start && count2 >= count/2) break;
255 return new Node(name,
257 mkNode(name+"_0", component, ports, start, breakpt, addr, bits+1),
258 mkNode(name+"_1", component, ports, breakpt, end, addr | (1 << bits), bits+1),
262 private String describe(String prefix, Object o) {
263 if (o==null) return null;
264 if (o instanceof SlipwayBenkoBox) {
265 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
266 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
268 if (o instanceof Node) {
269 return ((Node)o).describe(prefix);
280 public Node(String name, String component, Object left, Object right, int addr, int bits) {
284 this.component = component;
288 public void dumpChannels(int indentamount, boolean decl) {
290 for(int i=0; i<indentamount; i++) indent += " ";
292 String n = describe(prefix).startsWith("instruction")
293 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
294 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
296 System.out.println(" "+indent+
298 "krunk"+(krunk++)+"(clk, "+
299 describe(prefix)+"_r, "+
300 describe(prefix)+"_a, "+
301 describe(prefix)+", "+
302 FabricTree.this.describe(prefix, left)+"_r, "+
303 FabricTree.this.describe(prefix, left)+"_a, "+
304 FabricTree.this.describe(prefix, left)+", "+
305 FabricTree.this.describe(prefix, right)+"_r, "+
306 FabricTree.this.describe(prefix, right)+"_a, "+
307 FabricTree.this.describe(prefix, right)+
310 dumpChannels(left, indentamount+1, decl);
311 dumpChannels(right, indentamount+1, decl);
313 public void dumpChannels(Object o, int indentamount, boolean decl) {
315 if (o instanceof Node) {
316 ((Node)o).dumpChannels(indentamount, decl);
319 for(int i=0; i<indentamount; i++) indent += " ";
321 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
322 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
323 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
327 public String describe(String prefix) {
332 public static int krunk=0;
334 private static String getUniqueName(Ship ship) {
335 return ship.getType() + ship.getOrdinal();
338 private static int count(Iterable<Destination> it) {
340 for(Destination d : it)
345 public void expand(ShipDescription sd) {
347 String filename = sd.getName().toLowerCase();
348 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
349 new File(outf.getParent()).mkdirs();
350 System.err.println("writing to " + outf);
351 FileOutputStream out = new FileOutputStream(outf);
352 PrintWriter pw = new PrintWriter(out);
354 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice") || filename.equals("alu3") || filename.equals("stack");
356 pw.println("`include \"macros.v\"");
358 pw.println("module " + filename + "( clk");
359 for(BenkoBoxDescription bb : sd) {
360 String bb_name = bb.getName();
363 pw.print(", " + bb_name+"_r");
364 pw.print(", " + bb_name+"_a_");
365 pw.print(", " + bb_name+"_d");
367 pw.print(", " + bb_name+"_r_");
368 pw.print(", " + bb_name+"_a");
369 pw.print(", " + bb_name+"_d_");
375 pw.println(" input clk;");
376 for(BenkoBoxDescription bb : sd) {
377 String bb_name = bb.getName();
380 pw.println("`input(" +
384 "[(`PACKET_WIDTH-1):0],"+
388 pw.println("`output(" +
392 "[(`PACKET_WIDTH-1):0],"+
395 pw.println("`defreg(" +
397 "[(`PACKET_WIDTH-1):0],"+
405 pw.println(sd.getSection("fpga"));
408 pw.println("endmodule");
412 } catch (Exception e) { throw new RuntimeException(e); }
415 public int computeOffset(int origin, int target) { return (target - origin)/6; }
416 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
418 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
419 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
420 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
421 public long writeInstruction(Instruction d) { return writeInstruction(d); }
422 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
424 private class SlipwayInstructionEncoder extends InstructionEncoder {
425 public long getDestAddr(Destination box) { return ((SlipwayBenkoBox.VirtualPort)box).addr; }
426 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
427 public Destination getDestByAddr(long dest) {
428 for(Ship ship : Slipway.this)
429 for(BenkoBox bb : ship.getBenkoBoxes())
430 for(Destination d : bb.getDestinations())
431 if (getDestAddr(d)==dest)
435 public BenkoBox getBoxByInstAddr(long dest) {
436 for(Ship ship : Slipway.this)
437 for(BenkoBox bb : ship.getBenkoBoxes())
438 if (((SlipwayBenkoBox)bb).instr_addr == dest)