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.bind.*;
12 import edu.berkeley.sbp.util.*;
16 public class Slipway extends Fleet {
18 public ArrayList<SlipwayShip> shiplist = new ArrayList<SlipwayShip>();
19 public HashMap<String,SlipwayShip> ships = new HashMap<String,SlipwayShip>();
20 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)shiplist.iterator(); }
22 private String bitfile;
24 public static void main(String[] s) throws Exception {
25 new Slipway().dumpFabric(false);
28 public Slipway() { this("groundhog2.bit"); }
29 public Slipway(String bitfile) {
30 this.bitfile = bitfile;
31 createShip("Debug", "debug");
32 createShip("Alu2", "alu2a");
33 createShip("Alu2", "alu2b");
34 createShip("Alu2", "alu2c");
35 createShip("Alu2", "alu2d");
36 createShip("Fifo", "fifo1");
37 createShip("Fifo", "fifo2");
38 createShip("Fifo", "fifo3");
39 createShip("Fifo", "fifo4");
40 createShip("Execute", "execute");
41 createShip("Memory", "Memory");
42 createShip("Memory", "Memory");
43 createShip("Lut3", "lut3");
44 createShip("Alu1", "alu1");
45 createShip("Alu3", "alu3");
46 createShip("Choice", "Choice");
47 createShip("Choice", "Choice");
48 createShip("Choice", "Choice");
49 createShip("Choice", "Choice");
50 createShip("Stack", "Stack");
54 public Ship createShip(String type, String name) {
56 ShipDescription sd = new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
57 SlipwayShip ship = new SlipwayShip(this, name, type, sd);
58 ships.put(name, ship);
61 } catch (IOException e) { throw new RuntimeException(e); }
64 public FleetProcess run(final byte[] instructions) {
66 return new Client(bitfile, instructions);
67 } catch (IOException e) { throw new RuntimeException(e); }
70 public void dumpFabric(boolean quiet) {
71 // FIXME: this is really ugly: the order of port declarations in
72 // the XXXShip.java file must match the order in the .balsa file!
74 ArrayList instructionports = new ArrayList<SlipwayBenkoBox>();
75 for(SlipwayShip ship : shiplist)
76 for(BenkoBox port : ship.getBenkoBoxes())
77 if (!((SlipwayBenkoBox)port).special())
78 instructionports.add(port);
79 FabricTree instructions =
80 new FabricTree((SlipwayBenkoBox[])instructionports.toArray(new SlipwayBenkoBox[0]),
84 ArrayList inputports = new ArrayList<SlipwayBenkoBox>();
85 for(SlipwayShip ship : shiplist)
86 for(BenkoBox port : ship.getBenkoBoxes())
87 if (!((SlipwayBenkoBox)port).special())
90 new FabricTree((SlipwayBenkoBox[])inputports.toArray(new SlipwayBenkoBox[0]),
94 ArrayList outputports = new ArrayList<SlipwayBenkoBox>();
95 for(SlipwayShip ship : shiplist)
96 for(BenkoBox port : ship.getBenkoBoxes())
97 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
98 outputports.add(port);
100 new FabricTree((SlipwayBenkoBox[])outputports.toArray(new SlipwayBenkoBox[0]),
104 ArrayList ihornports = new ArrayList<SlipwayBenkoBox>();
105 for(SlipwayShip ship : shiplist)
106 for(BenkoBox port : ship.getBenkoBoxes())
107 if (((SlipwayBenkoBox)port).ihorn())
108 ihornports.add(port);
110 new FabricTree((SlipwayBenkoBox[])ihornports.toArray(new SlipwayBenkoBox[0]),
115 System.out.println("`include \"macros.v\"");
116 System.out.println("module fabric(clk, data_Memory0_command_r, data_Memory0_command_a, data_Memory0_command,");
117 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
118 System.out.println(" input clk;");
119 System.out.println(" input data_Memory0_command_r;");
120 System.out.println(" output data_Memory0_command_a;");
121 System.out.println(" output data_Debug0_out_r;");
122 System.out.println(" input data_Debug0_out_a;");
123 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
124 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Memory0_command;");
125 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Memory0_ihorn;");
126 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Memory0_dhorn;");
127 System.out.println();
129 System.out.println();
131 instructions.dumpChannels(true);
132 outputs.dumpChannels(true);
133 inputs.dumpChannels(true);
134 ihorns.dumpChannels(true);
135 for(SlipwayShip ship : shiplist)
136 for(BenkoBox port : ship.getBenkoBoxes())
137 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
138 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
139 +getUniqueName(ship)+"_"+port.getName()+";");
141 System.out.println("");
142 instructions.dumpChannels(false);
143 System.out.println("");
144 outputs.dumpChannels(false);
145 System.out.println("");
146 inputs.dumpChannels(false);
147 System.out.println("");
148 ihorns.dumpChannels(false);
149 System.out.println("");
150 for(SlipwayShip ship : shiplist) {
151 System.out.print(ship.getType().toLowerCase());
152 System.out.print(" ");
153 System.out.print("krunk"+(krunk++));
154 System.out.print("(clk, ");
155 boolean first = true;
156 for(BenkoBox port : ship.getBenkoBoxes()) {
157 if (!first) System.out.print(", ");
159 String prefix = "data_";
160 if (((SlipwayBenkoBox)port).ihorn()) prefix = "ihorn_";
161 if (((SlipwayBenkoBox)port).dhorn()) prefix = "source_";
162 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
163 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
164 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
165 System.out.print(" ");
167 System.out.println(");");
169 for(BenkoBox port : ship.getBenkoBoxes()) {
170 if (((SlipwayBenkoBox)port).special()) continue;
171 if (((SlipwayBenkoBox)port).inbox) {
172 System.out.print("inbox");
174 System.out.print("outbox");
176 System.out.print(" krunk"+(krunk++)+"(clk, ");
177 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
178 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
179 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
180 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
181 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
182 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
183 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
184 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
185 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
186 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
187 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
188 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
189 System.out.print(");");
190 System.out.println();
195 System.out.println("funnel topfun(clk,"+
196 " dest_r, dest_a, dest,"+
197 " source_r, source_a, source,"+
198 " data_Memory0_dhorn_r, data_Memory0_dhorn_a, data_Memory0_dhorn);");
200 System.out.println("assign instruction_r = ihorn_r;");
201 System.out.println("assign ihorn_a = instruction_a;");
202 System.out.println("assign instruction = ihorn;");
203 System.out.println("assign dest_r = source_r;");
204 System.out.println("assign source_a = dest_a;");
205 System.out.println("assign dest = source;");
206 System.out.println("endmodule");
209 private static class FabricTree {
213 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
214 public FabricTree(SlipwayBenkoBox[] ports, String component, String prefix) {
215 this.prefix = prefix;
216 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
218 private Object mkNode(String name, String component, SlipwayBenkoBox[] ports,
219 int start, int end, int addr, int bits) {
220 if (end-start == 0) return null;
221 if (end-start == 1) {
222 SlipwayBenkoBox p = ports[start];
223 if (prefix.equals("instruction")) {
226 } else if (prefix.equals("dest")) {
230 throw new RuntimeException("too many benkoboxen!");
232 for(Destination d : p.getDestinations()) {
233 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
234 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
235 vp.addr = p.addr | (count << bits);
245 if (end-start <= 2) {
246 breakpt = (start+end)/2;
248 for(int i=start; i<end; i++)
249 count += count(ports[i].getDestinations());
250 for(int i=start; i<end-1; i++) {
251 count2 += count(ports[i].getDestinations());
253 if (i>start && count2 >= count/2) break;
256 return new Node(name,
258 mkNode(name+"_0", component, ports, start, breakpt, addr, bits+1),
259 mkNode(name+"_1", component, ports, breakpt, end, addr | (1 << bits), bits+1),
263 private String describe(String prefix, Object o) {
264 if (o==null) return null;
265 if (o instanceof SlipwayBenkoBox) {
266 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
267 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
269 if (o instanceof Node) {
270 return ((Node)o).describe(prefix);
281 public Node(String name, String component, Object left, Object right, int addr, int bits) {
285 this.component = component;
289 public void dumpChannels(int indentamount, boolean decl) {
291 for(int i=0; i<indentamount; i++) indent += " ";
293 String n = describe(prefix).startsWith("instruction")
294 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
295 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
297 System.out.println(" "+indent+
299 "krunk"+(krunk++)+"(clk, "+
300 describe(prefix)+"_r, "+
301 describe(prefix)+"_a, "+
302 describe(prefix)+", "+
303 FabricTree.this.describe(prefix, left)+"_r, "+
304 FabricTree.this.describe(prefix, left)+"_a, "+
305 FabricTree.this.describe(prefix, left)+", "+
306 FabricTree.this.describe(prefix, right)+"_r, "+
307 FabricTree.this.describe(prefix, right)+"_a, "+
308 FabricTree.this.describe(prefix, right)+
311 dumpChannels(left, indentamount+1, decl);
312 dumpChannels(right, indentamount+1, decl);
314 public void dumpChannels(Object o, int indentamount, boolean decl) {
316 if (o instanceof Node) {
317 ((Node)o).dumpChannels(indentamount, decl);
320 for(int i=0; i<indentamount; i++) indent += " ";
322 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
323 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
324 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
328 public String describe(String prefix) {
333 public static int krunk=0;
335 private static String getUniqueName(Ship ship) {
336 return ship.getType() + ship.getOrdinal();
339 private static int count(Iterable<Destination> it) {
341 for(Destination d : it)
346 public void expand(ShipDescription sd) {
348 String filename = sd.getName().toLowerCase();
349 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
350 new File(outf.getParent()).mkdirs();
351 System.err.println("writing to " + outf);
352 FileOutputStream out = new FileOutputStream(outf);
353 PrintWriter pw = new PrintWriter(out);
355 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice") || filename.equals("alu3") || filename.equals("stack");
357 pw.println("`include \"macros.v\"");
359 pw.println("module " + filename + "( clk");
360 for(BenkoBoxDescription bb : sd) {
361 String bb_name = bb.getName();
364 pw.print(", " + bb_name+"_r");
365 pw.print(", " + bb_name+"_a_");
366 pw.print(", " + bb_name+"_d");
368 pw.print(", " + bb_name+"_r_");
369 pw.print(", " + bb_name+"_a");
370 pw.print(", " + bb_name+"_d_");
376 pw.println(" input clk;");
377 for(BenkoBoxDescription bb : sd) {
378 String bb_name = bb.getName();
381 pw.println("`input(" +
385 "[(`PACKET_WIDTH-1):0],"+
389 pw.println("`output(" +
393 "[(`PACKET_WIDTH-1):0],"+
396 pw.println("`defreg(" +
398 "[(`PACKET_WIDTH-1):0],"+
406 pw.println(sd.getSection("fpga"));
409 pw.println("endmodule");
413 } catch (Exception e) { throw new RuntimeException(e); }
416 public int computeOffset(int origin, int target) { return (target - origin)/6; }
417 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
419 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
420 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
421 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
422 public long writeInstruction(Instruction d) { return writeInstruction(d); }
423 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
425 private class SlipwayInstructionEncoder extends InstructionEncoder {
426 public long getDestAddr(Destination box) { return ((SlipwayBenkoBox.VirtualPort)box).addr; }
427 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
428 public Destination getDestByAddr(long dest) {
429 for(Ship ship : Slipway.this)
430 for(BenkoBox bb : ship.getBenkoBoxes())
431 for(Destination d : bb.getDestinations())
432 if (getDestAddr(d)==dest)
436 public BenkoBox getBoxByInstAddr(long dest) {
437 for(Ship ship : Slipway.this)
438 for(BenkoBox bb : ship.getBenkoBoxes())
439 if (((SlipwayBenkoBox)bb).instr_addr == dest)