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("Execute", "execute");
37 createShip("Fifo", "fifo1");
38 createShip("Fifo", "fifo2");
39 createShip("Fifo", "fifo3");
40 createShip("Fifo", "fifo4");
41 createShip("Memory", "Memory");
42 createShip("Memory", "Memory");
43 createShip("Lut3", "lut3");
44 createShip("Alu1", "alu1");
45 createShip("Choice", "Choice");
46 createShip("Choice", "Choice");
47 createShip("Choice", "Choice");
48 createShip("Choice", "Choice");
52 public Ship createShip(String type, String name) {
54 ShipDescription sd = new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
55 SlipwayShip ship = new SlipwayShip(this, name, type, sd);
56 ships.put(name, ship);
59 } catch (IOException e) { throw new RuntimeException(e); }
62 public FleetProcess run(final byte[] instructions) {
64 return new Client(bitfile, instructions);
65 } catch (IOException e) { throw new RuntimeException(e); }
68 public void dumpFabric(boolean quiet) {
69 // FIXME: this is really ugly: the order of port declarations in
70 // the XXXShip.java file must match the order in the .balsa file!
72 ArrayList instructionports = new ArrayList<SlipwayBenkoBox>();
73 for(SlipwayShip ship : shiplist)
74 for(BenkoBox port : ship.getBenkoBoxes())
75 if (!((SlipwayBenkoBox)port).special())
76 instructionports.add(port);
77 FabricTree instructions =
78 new FabricTree((SlipwayBenkoBox[])instructionports.toArray(new SlipwayBenkoBox[0]),
82 ArrayList inputports = new ArrayList<SlipwayBenkoBox>();
83 for(SlipwayShip ship : shiplist)
84 for(BenkoBox port : ship.getBenkoBoxes())
85 if (!((SlipwayBenkoBox)port).special())
88 new FabricTree((SlipwayBenkoBox[])inputports.toArray(new SlipwayBenkoBox[0]),
92 ArrayList outputports = new ArrayList<SlipwayBenkoBox>();
93 for(SlipwayShip ship : shiplist)
94 for(BenkoBox port : ship.getBenkoBoxes())
95 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
96 outputports.add(port);
98 new FabricTree((SlipwayBenkoBox[])outputports.toArray(new SlipwayBenkoBox[0]),
102 ArrayList ihornports = new ArrayList<SlipwayBenkoBox>();
103 for(SlipwayShip ship : shiplist)
104 for(BenkoBox port : ship.getBenkoBoxes())
105 if (((SlipwayBenkoBox)port).ihorn())
106 ihornports.add(port);
108 new FabricTree((SlipwayBenkoBox[])ihornports.toArray(new SlipwayBenkoBox[0]),
113 System.out.println("`include \"macros.v\"");
114 System.out.println("module fabric(clk, data_Memory0_command_r, data_Memory0_command_a, data_Memory0_command,");
115 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
116 System.out.println(" input clk;");
117 System.out.println(" input data_Memory0_command_r;");
118 System.out.println(" output data_Memory0_command_a;");
119 System.out.println(" output data_Debug0_out_r;");
120 System.out.println(" input data_Debug0_out_a;");
121 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
122 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Memory0_command;");
123 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Memory0_ihorn;");
124 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Memory0_dhorn;");
125 System.out.println();
127 System.out.println();
129 instructions.dumpChannels(true);
130 outputs.dumpChannels(true);
131 inputs.dumpChannels(true);
132 ihorns.dumpChannels(true);
133 for(SlipwayShip ship : shiplist)
134 for(BenkoBox port : ship.getBenkoBoxes())
135 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
136 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
137 +getUniqueName(ship)+"_"+port.getName()+";");
139 System.out.println("");
140 instructions.dumpChannels(false);
141 System.out.println("");
142 outputs.dumpChannels(false);
143 System.out.println("");
144 inputs.dumpChannels(false);
145 System.out.println("");
146 ihorns.dumpChannels(false);
147 System.out.println("");
148 for(SlipwayShip ship : shiplist) {
149 System.out.print(ship.getType().toLowerCase());
150 System.out.print(" ");
151 System.out.print("krunk"+(krunk++));
152 System.out.print("(clk, ");
153 boolean first = true;
154 for(BenkoBox port : ship.getBenkoBoxes()) {
155 if (!first) System.out.print(", ");
157 String prefix = "data_";
158 if (((SlipwayBenkoBox)port).ihorn()) prefix = "ihorn_";
159 if (((SlipwayBenkoBox)port).dhorn()) prefix = "source_";
160 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
161 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
162 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
163 System.out.print(" ");
165 System.out.println(");");
167 for(BenkoBox port : ship.getBenkoBoxes()) {
168 if (((SlipwayBenkoBox)port).special()) continue;
169 if (((SlipwayBenkoBox)port).inbox) {
170 System.out.print("inbox");
172 System.out.print("outbox");
174 System.out.print(" krunk"+(krunk++)+"(clk, ");
175 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
176 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
177 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
178 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
179 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
180 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
181 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
182 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
183 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
184 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
185 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
186 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
187 System.out.print(");");
188 System.out.println();
193 System.out.println("funnel topfun(clk,"+
194 " dest_r, dest_a, dest,"+
195 " source_r, source_a, source,"+
196 " data_Memory0_dhorn_r, data_Memory0_dhorn_a, data_Memory0_dhorn);");
198 System.out.println("assign instruction_r = ihorn_r;");
199 System.out.println("assign ihorn_a = instruction_a;");
200 System.out.println("assign instruction = ihorn;");
201 System.out.println("assign dest_r = source_r;");
202 System.out.println("assign source_a = dest_a;");
203 System.out.println("assign dest = source;");
204 System.out.println("endmodule");
207 private static class FabricTree {
211 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
212 public FabricTree(SlipwayBenkoBox[] ports, String component, String prefix) {
213 this.prefix = prefix;
214 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
216 private Object mkNode(String name, String component, SlipwayBenkoBox[] ports,
217 int start, int end, int addr, int bits) {
218 if (end-start == 0) return null;
219 if (end-start == 1) {
220 SlipwayBenkoBox p = ports[start];
221 if (prefix.equals("instruction")) {
224 } else if (prefix.equals("dest")) {
228 for(Destination d : p.getDestinations()) {
229 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
230 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
231 vp.addr = p.addr | (count << bits);
238 return new Node(name,
240 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
241 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
245 private String describe(String prefix, Object o) {
246 if (o==null) return null;
247 if (o instanceof SlipwayBenkoBox) {
248 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
249 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
251 if (o instanceof Node) {
252 return ((Node)o).describe(prefix);
263 public Node(String name, String component, Object left, Object right, int addr, int bits) {
267 this.component = component;
271 public void dumpChannels(int indentamount, boolean decl) {
273 for(int i=0; i<indentamount; i++) indent += " ";
275 String n = describe(prefix).startsWith("instruction")
276 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
277 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
279 System.out.println(" "+indent+
281 "krunk"+(krunk++)+"(clk, "+
282 describe(prefix)+"_r, "+
283 describe(prefix)+"_a, "+
284 describe(prefix)+", "+
285 FabricTree.this.describe(prefix, left)+"_r, "+
286 FabricTree.this.describe(prefix, left)+"_a, "+
287 FabricTree.this.describe(prefix, left)+", "+
288 FabricTree.this.describe(prefix, right)+"_r, "+
289 FabricTree.this.describe(prefix, right)+"_a, "+
290 FabricTree.this.describe(prefix, right)+
293 dumpChannels(left, indentamount+1, decl);
294 dumpChannels(right, indentamount+1, decl);
296 public void dumpChannels(Object o, int indentamount, boolean decl) {
298 if (o instanceof Node) {
299 ((Node)o).dumpChannels(indentamount, decl);
302 for(int i=0; i<indentamount; i++) indent += " ";
304 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
305 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
306 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
310 public String describe(String prefix) {
315 public static int krunk=0;
317 private static String getUniqueName(Ship ship) {
318 return ship.getType() + ship.getOrdinal();
321 public void expand(ShipDescription sd) {
323 String filename = sd.getName().toLowerCase();
324 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
325 new File(outf.getParent()).mkdirs();
326 System.err.println("writing to " + outf);
327 FileOutputStream out = new FileOutputStream(outf);
328 PrintWriter pw = new PrintWriter(out);
330 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice");
332 pw.println("`include \"macros.v\"");
334 pw.println("module " + filename + "( clk");
335 for(BenkoBoxDescription bb : sd) {
336 String bb_name = bb.getName();
339 pw.print(", " + bb_name+"_r");
340 pw.print(", " + bb_name+"_a_");
341 pw.print(", " + bb_name+"_d");
343 pw.print(", " + bb_name+"_r_");
344 pw.print(", " + bb_name+"_a");
345 pw.print(", " + bb_name+"_d_");
351 pw.println(" input clk;");
352 for(BenkoBoxDescription bb : sd) {
353 String bb_name = bb.getName();
356 pw.println("`input(" +
360 "[(`PACKET_WIDTH-1):0],"+
364 pw.println("`output(" +
368 "[(`PACKET_WIDTH-1):0],"+
371 pw.println("`defreg(" +
373 "[(`PACKET_WIDTH-1):0],"+
381 pw.println(sd.getSection("fpga"));
384 pw.println("endmodule");
388 } catch (Exception e) { throw new RuntimeException(e); }
391 public int computeOffset(int origin, int target) { return (target - origin)/6; }
392 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
394 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
395 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
396 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
397 public long writeInstruction(Instruction d) { return writeInstruction(d); }
398 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
400 private class SlipwayInstructionEncoder extends InstructionEncoder {
401 public long getDestAddr(Destination box) {
403 if (box instanceof SlipwayBenkoBox)
404 ret = ((SlipwayBenkoBox)box).addr;
406 ret = ((SlipwayBenkoBox.VirtualPort)box).addr;
409 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
410 public Destination getDestByAddr(long dest) {
411 for(Ship ship : Slipway.this)
412 for(BenkoBox bb : ship.getBenkoBoxes()) {
413 if (((SlipwayBenkoBox)bb).addr == dest)
415 for(Destination d : bb.getDestinations())
416 if (getDestAddr(d)==dest)
421 public BenkoBox getBoxByInstAddr(long dest) {
422 for(Ship ship : Slipway.this)
423 for(BenkoBox bb : ship.getBenkoBoxes())
424 if (((SlipwayBenkoBox)bb).instr_addr == dest)