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("gaspified.bit"); }
28 public Slipway(String bitfile) {
29 this.bitfile = bitfile;
30 createShip("Debug", "debug");
31 createShip("Memory", "Memory");
32 createShip("Fifo", "fifo1");
33 createShip("Fifo", "fifo2");
34 createShip("Fifo", "fifo3");
35 createShip("Execute", "execute");
36 createShip("Alu1", "alu1");
37 createShip("Alu2", "alu2a");
38 createShip("Lut3", "lut3");
39 createShip("Choice", "Choice");
40 createShip("Stack", "Stack");
41 createShip("Alu3", "alu3");
42 createShip("BitFifo", "bitfifo");
44 // above is the minimal ship set needed to run the regression suite
45 // below are extra bonush ships
47 createShip("Alu2", "alu2b");
48 createShip("Alu2", "alu2c");
49 createShip("Alu2", "alu2d");
50 createShip("Fifo", "fifo4");
51 createShip("Memory", "Memory");
52 createShip("Choice", "Choice");
53 createShip("Choice", "Choice");
54 createShip("Choice", "Choice");
58 public Ship createShip(String type, String name) {
60 ShipDescription sd = new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
61 SlipwayShip ship = new SlipwayShip(this, name, type, sd);
62 ships.put(name, ship);
65 } catch (IOException e) { throw new RuntimeException(e); }
68 public FleetProcess run(final byte[] instructions) {
70 return new Client(bitfile, instructions);
71 } catch (IOException e) { throw new RuntimeException(e); }
74 public void dumpFabric(boolean quiet) {
75 // FIXME: this is really ugly: the order of port declarations in
76 // the XXXShip.java file must match the order in the .balsa file!
78 ArrayList instructionports = new ArrayList<SlipwayBenkoBox>();
79 for(SlipwayShip ship : shiplist)
80 for(BenkoBox port : ship.getBenkoBoxes())
81 if (!((SlipwayBenkoBox)port).special())
82 instructionports.add(port);
83 FabricTree instructions =
84 new FabricTree((SlipwayBenkoBox[])instructionports.toArray(new SlipwayBenkoBox[0]),
88 ArrayList inputports = new ArrayList<SlipwayBenkoBox>();
89 for(SlipwayShip ship : shiplist)
90 for(BenkoBox port : ship.getBenkoBoxes())
91 if (!((SlipwayBenkoBox)port).special())
94 new FabricTree((SlipwayBenkoBox[])inputports.toArray(new SlipwayBenkoBox[0]),
98 ArrayList outputports = new ArrayList<SlipwayBenkoBox>();
99 for(SlipwayShip ship : shiplist)
100 for(BenkoBox port : ship.getBenkoBoxes())
101 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
102 outputports.add(port);
104 new FabricTree((SlipwayBenkoBox[])outputports.toArray(new SlipwayBenkoBox[0]),
108 ArrayList ihornports = new ArrayList<SlipwayBenkoBox>();
109 for(SlipwayShip ship : shiplist)
110 for(BenkoBox port : ship.getBenkoBoxes())
111 if (((SlipwayBenkoBox)port).ihorn())
112 ihornports.add(port);
114 new FabricTree((SlipwayBenkoBox[])ihornports.toArray(new SlipwayBenkoBox[0]),
119 System.out.println("`include \"macros.v\"");
120 System.out.println("module fabric(clk, data_Memory0_command_r, data_Memory0_command_a, data_Memory0_command,");
121 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
122 System.out.println(" input clk;");
123 System.out.println(" input data_Memory0_command_r;");
124 System.out.println(" output data_Memory0_command_a;");
125 System.out.println(" output data_Debug0_out_r;");
126 System.out.println(" input data_Debug0_out_a;");
127 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
128 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Memory0_command;");
129 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Memory0_ihorn;");
130 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Memory0_dhorn;");
131 System.out.println();
133 System.out.println();
135 instructions.dumpChannels(true);
136 outputs.dumpChannels(true);
137 inputs.dumpChannels(true);
138 ihorns.dumpChannels(true);
139 for(SlipwayShip ship : shiplist)
140 for(BenkoBox port : ship.getBenkoBoxes())
141 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
142 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
143 +getUniqueName(ship)+"_"+port.getName()+";");
145 System.out.println("");
146 instructions.dumpChannels(false);
147 System.out.println("");
148 outputs.dumpChannels(false);
149 System.out.println("");
150 inputs.dumpChannels(false);
151 System.out.println("");
152 ihorns.dumpChannels(false);
153 System.out.println("");
154 for(SlipwayShip ship : shiplist) {
155 System.out.print(ship.getType().toLowerCase());
156 System.out.print(" ");
157 System.out.print("krunk"+(krunk++));
158 System.out.print("(clk, ");
159 boolean first = true;
160 for(BenkoBox port : ship.getBenkoBoxes()) {
161 if (!first) System.out.print(", ");
163 String prefix = "data_";
164 if (((SlipwayBenkoBox)port).ihorn()) prefix = "ihorn_";
165 if (((SlipwayBenkoBox)port).dhorn()) prefix = "source_";
166 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
167 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
168 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
169 System.out.print(" ");
171 System.out.println(");");
173 for(BenkoBox port : ship.getBenkoBoxes()) {
174 if (((SlipwayBenkoBox)port).special()) continue;
175 if (((SlipwayBenkoBox)port).inbox) {
176 System.out.print("inbox");
178 System.out.print("outbox");
180 System.out.print(" krunk"+(krunk++)+"(clk, ");
181 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
182 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
183 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
184 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
185 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
186 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
187 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
188 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
189 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
190 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
191 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
192 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
193 System.out.print(");");
194 System.out.println();
199 System.out.println("funnel topfun(clk,"+
200 " dest_r, dest_a, dest,"+
201 " source_r, source_a, source,"+
202 " data_Memory0_dhorn_r, data_Memory0_dhorn_a, data_Memory0_dhorn);");
204 System.out.println("assign instruction_r = ihorn_r;");
205 System.out.println("assign ihorn_a = instruction_a;");
206 System.out.println("assign instruction = ihorn;");
207 System.out.println("assign dest_r = source_r;");
208 System.out.println("assign source_a = dest_a;");
209 System.out.println("assign dest = source;");
210 System.out.println("endmodule");
213 private static class FabricTree {
217 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
218 public FabricTree(SlipwayBenkoBox[] ports, String component, String prefix) {
219 this.prefix = prefix;
220 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
222 private Object mkNode(String name, String component, SlipwayBenkoBox[] ports,
223 int start, int end, int addr, int bits) {
224 if (end-start == 0) return null;
225 if (end-start == 1) {
226 SlipwayBenkoBox p = ports[start];
227 if (prefix.equals("instruction")) {
230 } else if (prefix.equals("dest")) {
234 throw new RuntimeException("too many benkoboxen!");
236 for(Destination d : p.getDestinations()) {
237 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
238 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
239 vp.addr = p.addr | (count << bits);
249 if (end-start <= 2) {
250 breakpt = (start+end)/2;
252 for(int i=start; i<end; i++)
253 count += count(ports[i].getDestinations());
254 for(int i=start; i<end-1; i++) {
255 count2 += count(ports[i].getDestinations());
257 if (i>start && count2 >= count/2) break;
260 return new Node(name,
262 mkNode(name+"_0", component, ports, start, breakpt, addr, bits+1),
263 mkNode(name+"_1", component, ports, breakpt, end, addr | (1 << bits), bits+1),
267 private String describe(String prefix, Object o) {
268 if (o==null) return null;
269 if (o instanceof SlipwayBenkoBox) {
270 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
271 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
273 if (o instanceof Node) {
274 return ((Node)o).describe(prefix);
285 public Node(String name, String component, Object left, Object right, int addr, int bits) {
289 this.component = component;
293 public void dumpChannels(int indentamount, boolean decl) {
295 for(int i=0; i<indentamount; i++) indent += " ";
297 String n = describe(prefix).startsWith("instruction")
298 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
299 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
301 System.out.println(" "+indent+
303 "krunk"+(krunk++)+"(clk, "+
304 describe(prefix)+"_r, "+
305 describe(prefix)+"_a, "+
306 describe(prefix)+", "+
307 FabricTree.this.describe(prefix, left)+"_r, "+
308 FabricTree.this.describe(prefix, left)+"_a, "+
309 FabricTree.this.describe(prefix, left)+", "+
310 FabricTree.this.describe(prefix, right)+"_r, "+
311 FabricTree.this.describe(prefix, right)+"_a, "+
312 FabricTree.this.describe(prefix, right)+
315 dumpChannels(left, indentamount+1, decl);
316 dumpChannels(right, indentamount+1, decl);
318 public void dumpChannels(Object o, int indentamount, boolean decl) {
320 if (o instanceof Node) {
321 ((Node)o).dumpChannels(indentamount, decl);
324 for(int i=0; i<indentamount; i++) indent += " ";
326 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
327 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
328 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
332 public String describe(String prefix) {
337 public static int krunk=0;
339 private static String getUniqueName(Ship ship) {
340 return ship.getType() + ship.getOrdinal();
343 private static int count(Iterable<Destination> it) {
345 for(Destination d : it)
350 public void expand(ShipDescription sd) {
352 if (sd.getSection("fpga")==null) return;
353 String filename = sd.getName().toLowerCase();
354 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
355 new File(outf.getParent()).mkdirs();
356 System.err.println("writing to " + outf);
357 FileOutputStream out = new FileOutputStream(outf);
358 PrintWriter pw = new PrintWriter(out);
361 !"debug".equals(filename) &&
362 !"execute".equals(filename) &&
363 !"memory".equals(filename) &&
364 !"fifo".equals(filename);
366 pw.println("`include \"macros.v\"");
368 pw.println("module " + filename + "( clk");
369 for(BenkoBoxDescription bb : sd) {
370 String bb_name = bb.getName();
373 pw.print(", " + bb_name+"_r");
374 pw.print(", " + bb_name+"_a_");
375 pw.print(", " + bb_name+"_d");
377 pw.print(", " + bb_name+"_r_");
378 pw.print(", " + bb_name+"_a");
379 pw.print(", " + bb_name+"_d_");
385 pw.println(" input clk;");
386 for(BenkoBoxDescription bb : sd) {
387 String bb_name = bb.getName();
390 pw.println("`input(" +
394 "[(`PACKET_WIDTH-1):0],"+
398 pw.println("`output(" +
402 "[(`PACKET_WIDTH-1):0],"+
405 pw.println("`defreg(" +
407 "[(`PACKET_WIDTH-1):0],"+
415 pw.println(sd.getSection("fpga"));
418 pw.println("endmodule");
422 } catch (Exception e) { throw new RuntimeException(e); }
425 public int computeOffset(int origin, int target) { return (target - origin)/6; }
426 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
428 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
429 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
430 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
431 public long writeInstruction(Instruction d) { return writeInstruction(d); }
432 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
434 private class SlipwayInstructionEncoder extends InstructionEncoder {
435 public long getDestAddr(Destination box) { return ((SlipwayBenkoBox.VirtualPort)box).addr; }
436 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
437 public Destination getDestByAddr(long dest) {
438 for(Ship ship : Slipway.this)
439 for(BenkoBox bb : ship.getBenkoBoxes())
440 for(Destination d : bb.getDestinations())
441 if (getDestAddr(d)==dest)
445 public BenkoBox getBoxByInstAddr(long dest) {
446 for(Ship ship : Slipway.this)
447 for(BenkoBox bb : ship.getBenkoBoxes())
448 if (((SlipwayBenkoBox)bb).instr_addr == dest)