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("groundhog.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");
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 for(Destination d : p.getDestinations()) {
230 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
231 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
232 vp.addr = p.addr | (count << bits);
239 return new Node(name,
241 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
242 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
246 private String describe(String prefix, Object o) {
247 if (o==null) return null;
248 if (o instanceof SlipwayBenkoBox) {
249 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
250 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
252 if (o instanceof Node) {
253 return ((Node)o).describe(prefix);
264 public Node(String name, String component, Object left, Object right, int addr, int bits) {
268 this.component = component;
272 public void dumpChannels(int indentamount, boolean decl) {
274 for(int i=0; i<indentamount; i++) indent += " ";
276 String n = describe(prefix).startsWith("instruction")
277 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
278 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
280 System.out.println(" "+indent+
282 "krunk"+(krunk++)+"(clk, "+
283 describe(prefix)+"_r, "+
284 describe(prefix)+"_a, "+
285 describe(prefix)+", "+
286 FabricTree.this.describe(prefix, left)+"_r, "+
287 FabricTree.this.describe(prefix, left)+"_a, "+
288 FabricTree.this.describe(prefix, left)+", "+
289 FabricTree.this.describe(prefix, right)+"_r, "+
290 FabricTree.this.describe(prefix, right)+"_a, "+
291 FabricTree.this.describe(prefix, right)+
294 dumpChannels(left, indentamount+1, decl);
295 dumpChannels(right, indentamount+1, decl);
297 public void dumpChannels(Object o, int indentamount, boolean decl) {
299 if (o instanceof Node) {
300 ((Node)o).dumpChannels(indentamount, decl);
303 for(int i=0; i<indentamount; i++) indent += " ";
305 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
306 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
307 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
311 public String describe(String prefix) {
316 public static int krunk=0;
318 private static String getUniqueName(Ship ship) {
319 return ship.getType() + ship.getOrdinal();
322 public void expand(ShipDescription sd) {
324 String filename = sd.getName().toLowerCase();
325 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
326 new File(outf.getParent()).mkdirs();
327 System.err.println("writing to " + outf);
328 FileOutputStream out = new FileOutputStream(outf);
329 PrintWriter pw = new PrintWriter(out);
331 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice") || filename.equals("alu3") || filename.equals("stack");
333 pw.println("`include \"macros.v\"");
335 pw.println("module " + filename + "( clk");
336 for(BenkoBoxDescription bb : sd) {
337 String bb_name = bb.getName();
340 pw.print(", " + bb_name+"_r");
341 pw.print(", " + bb_name+"_a_");
342 pw.print(", " + bb_name+"_d");
344 pw.print(", " + bb_name+"_r_");
345 pw.print(", " + bb_name+"_a");
346 pw.print(", " + bb_name+"_d_");
352 pw.println(" input clk;");
353 for(BenkoBoxDescription bb : sd) {
354 String bb_name = bb.getName();
357 pw.println("`input(" +
361 "[(`PACKET_WIDTH-1):0],"+
365 pw.println("`output(" +
369 "[(`PACKET_WIDTH-1):0],"+
372 pw.println("`defreg(" +
374 "[(`PACKET_WIDTH-1):0],"+
382 pw.println(sd.getSection("fpga"));
385 pw.println("endmodule");
389 } catch (Exception e) { throw new RuntimeException(e); }
392 public int computeOffset(int origin, int target) { return (target - origin)/6; }
393 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
395 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
396 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
397 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
398 public long writeInstruction(Instruction d) { return writeInstruction(d); }
399 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
401 private class SlipwayInstructionEncoder extends InstructionEncoder {
402 public long getDestAddr(Destination box) {
404 if (box instanceof SlipwayBenkoBox)
405 ret = ((SlipwayBenkoBox)box).addr;
407 ret = ((SlipwayBenkoBox.VirtualPort)box).addr;
410 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
411 public Destination getDestByAddr(long dest) {
412 for(Ship ship : Slipway.this)
413 for(BenkoBox bb : ship.getBenkoBoxes()) {
414 if (((SlipwayBenkoBox)bb).addr == dest)
416 for(Destination d : bb.getDestinations())
417 if (getDestAddr(d)==dest)
422 public BenkoBox getBoxByInstAddr(long dest) {
423 for(Ship ship : Slipway.this)
424 for(BenkoBox bb : ship.getBenkoBoxes())
425 if (((SlipwayBenkoBox)bb).instr_addr == dest)