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("valentine.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 // FIXME cant have less than two feeding the instruction horn or BUG
42 createShip("Iscratch", "iscratch1");
43 createShip("Iscratch", "iscratch2");
44 createShip("Dscratch", "dscratch1");
45 createShip("Dscratch", "dscratch2");
46 createShip("Lut3", "lut3");
47 createShip("Alu1", "alu1");
51 public Ship createShip(String type, String name) {
53 ShipDescription sd = new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
54 SlipwayShip ship = new SlipwayShip(this, name, type, sd);
55 ships.put(name, ship);
58 } catch (IOException e) { throw new RuntimeException(e); }
61 public FleetProcess run(final byte[] instructions) {
63 return new Client(bitfile, instructions);
64 } catch (IOException e) { throw new RuntimeException(e); }
67 public void dumpFabric(boolean quiet) {
68 // FIXME: this is really ugly: the order of port declarations in
69 // the XXXShip.java file must match the order in the .balsa file!
71 ArrayList instructionports = new ArrayList<SlipwayBenkoBox>();
72 for(SlipwayShip ship : shiplist)
73 for(BenkoBox port : ship.getBenkoBoxes())
74 if (!((SlipwayBenkoBox)port).special())
75 instructionports.add(port);
76 FabricTree instructions =
77 new FabricTree((SlipwayBenkoBox[])instructionports.toArray(new SlipwayBenkoBox[0]),
81 ArrayList inputports = new ArrayList<SlipwayBenkoBox>();
82 for(SlipwayShip ship : shiplist)
83 for(BenkoBox port : ship.getBenkoBoxes())
84 if (!((SlipwayBenkoBox)port).special())
87 new FabricTree((SlipwayBenkoBox[])inputports.toArray(new SlipwayBenkoBox[0]),
91 ArrayList outputports = new ArrayList<SlipwayBenkoBox>();
92 for(SlipwayShip ship : shiplist)
93 for(BenkoBox port : ship.getBenkoBoxes())
94 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
95 outputports.add(port);
97 new FabricTree((SlipwayBenkoBox[])outputports.toArray(new SlipwayBenkoBox[0]),
101 ArrayList ihornports = new ArrayList<SlipwayBenkoBox>();
102 for(SlipwayShip ship : shiplist)
103 for(BenkoBox port : ship.getBenkoBoxes())
104 if (((SlipwayBenkoBox)port).ihorn())
105 ihornports.add(port);
107 new FabricTree((SlipwayBenkoBox[])ihornports.toArray(new SlipwayBenkoBox[0]),
112 System.out.println("`include \"macros.v\"");
113 System.out.println("module fabric(clk, data_Iscratch0_command_r, data_Iscratch0_command_a, data_Iscratch0_command,");
114 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
115 System.out.println(" input clk;");
116 System.out.println(" input data_Iscratch0_command_r;");
117 System.out.println(" output data_Iscratch0_command_a;");
118 System.out.println(" output data_Debug0_out_r;");
119 System.out.println(" input data_Debug0_out_a;");
120 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
121 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Iscratch0_command;");
122 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Iscratch0_ihorn;");
123 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Iscratch0_dhorn;");
124 System.out.println();
126 System.out.println();
128 instructions.dumpChannels(true);
129 outputs.dumpChannels(true);
130 inputs.dumpChannels(true);
131 ihorns.dumpChannels(true);
132 for(SlipwayShip ship : shiplist)
133 for(BenkoBox port : ship.getBenkoBoxes())
134 if (!((SlipwayBenkoBox)port).special() || ((SlipwayBenkoBox)port).dhorn())
135 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
136 +getUniqueName(ship)+"_"+port.getName()+";");
138 System.out.println("");
139 instructions.dumpChannels(false);
140 System.out.println("");
141 outputs.dumpChannels(false);
142 System.out.println("");
143 inputs.dumpChannels(false);
144 System.out.println("");
145 ihorns.dumpChannels(false);
146 System.out.println("");
147 for(SlipwayShip ship : shiplist) {
148 System.out.print(ship.getType().toLowerCase());
149 System.out.print(" ");
150 System.out.print("krunk"+(krunk++));
151 System.out.print("(clk, ");
152 boolean first = true;
153 for(BenkoBox port : ship.getBenkoBoxes()) {
154 if (!first) System.out.print(", ");
156 String prefix = "data_";
157 if (((SlipwayBenkoBox)port).ihorn()) prefix = "ihorn_";
158 if (((SlipwayBenkoBox)port).dhorn()) prefix = "source_";
159 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
160 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
161 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
162 System.out.print(" ");
164 System.out.println(");");
166 for(BenkoBox port : ship.getBenkoBoxes()) {
167 if (((SlipwayBenkoBox)port).special()) continue;
168 if (((SlipwayBenkoBox)port).inbox) {
169 System.out.print("inbox");
171 System.out.print("outbox");
173 System.out.print(" krunk"+(krunk++)+"(clk, ");
174 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
175 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
176 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
177 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
178 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
179 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
180 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
181 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
182 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
183 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
184 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
185 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
186 System.out.print(");");
187 System.out.println();
192 System.out.println("funnel topfun(clk,"+
193 " dest_r, dest_a, dest,"+
194 " source_r, source_a, source,"+
195 " data_Iscratch0_dhorn_r, data_Iscratch0_dhorn_a, data_Iscratch0_dhorn);");
197 System.out.println("assign instruction_r = ihorn_r;");
198 System.out.println("assign ihorn_a = instruction_a;");
199 System.out.println("assign instruction = ihorn;");
200 System.out.println("assign dest_r = source_r;");
201 System.out.println("assign source_a = dest_a;");
202 System.out.println("assign dest = source;");
203 System.out.println("endmodule");
206 private static class FabricTree {
210 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
211 public FabricTree(SlipwayBenkoBox[] ports, String component, String prefix) {
212 this.prefix = prefix;
213 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
215 private Object mkNode(String name, String component, SlipwayBenkoBox[] ports,
216 int start, int end, int addr, int bits) {
217 if (end-start == 0) return null;
218 if (end-start == 1) {
219 SlipwayBenkoBox p = ports[start];
220 if (prefix.equals("instruction")) {
223 } else if (prefix.equals("dest")) {
227 for(Destination d : p.getDestinations()) {
228 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
229 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
230 vp.addr = p.addr | (count << (bits+1));
237 return new Node(name,
239 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
240 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
244 private String describe(String prefix, Object o) {
245 if (o==null) return null;
246 if (o instanceof SlipwayBenkoBox) {
247 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
248 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
250 if (o instanceof Node) {
251 return ((Node)o).describe(prefix);
262 public Node(String name, String component, Object left, Object right, int addr, int bits) {
266 this.component = component;
270 public void dumpChannels(int indentamount, boolean decl) {
272 for(int i=0; i<indentamount; i++) indent += " ";
274 String n = describe(prefix).startsWith("instruction")
275 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
276 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
278 System.out.println(" "+indent+
280 "krunk"+(krunk++)+"(clk, "+
281 describe(prefix)+"_r, "+
282 describe(prefix)+"_a, "+
283 describe(prefix)+", "+
284 FabricTree.this.describe(prefix, left)+"_r, "+
285 FabricTree.this.describe(prefix, left)+"_a, "+
286 FabricTree.this.describe(prefix, left)+", "+
287 FabricTree.this.describe(prefix, right)+"_r, "+
288 FabricTree.this.describe(prefix, right)+"_a, "+
289 FabricTree.this.describe(prefix, right)+
292 dumpChannels(left, indentamount+1, decl);
293 dumpChannels(right, indentamount+1, decl);
295 public void dumpChannels(Object o, int indentamount, boolean decl) {
297 if (o instanceof Node) {
298 ((Node)o).dumpChannels(indentamount, decl);
301 for(int i=0; i<indentamount; i++) indent += " ";
303 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
304 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
305 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
309 public String describe(String prefix) {
314 public static int krunk=0;
316 private static String getUniqueName(Ship ship) {
317 return ship.getType() + ship.getOrdinal();
320 public void expand(ShipDescription sd) {
322 String filename = sd.getName().toLowerCase();
323 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
324 new File(outf.getParent()).mkdirs();
325 System.err.println("writing to " + outf);
326 FileOutputStream out = new FileOutputStream(outf);
327 PrintWriter pw = new PrintWriter(out);
329 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3");
331 pw.println("`include \"macros.v\"");
333 pw.println("module " + filename + "( clk");
334 for(BenkoBoxDescription bb : sd) {
335 String bb_name = bb.getName();
338 pw.print(", " + bb_name+"_r");
339 pw.print(", " + bb_name+"_a_");
340 pw.print(", " + bb_name+"_d");
342 pw.print(", " + bb_name+"_r_");
343 pw.print(", " + bb_name+"_a");
344 pw.print(", " + bb_name+"_d_");
350 pw.println(" input clk;");
351 for(BenkoBoxDescription bb : sd) {
352 String bb_name = bb.getName();
355 pw.println("`input(" +
359 "[(`DATAWIDTH-1):0],"+
363 pw.println("`output(" +
367 "[(`DATAWIDTH-1):0],"+
370 pw.println("`defreg(" +
372 "[(`DATAWIDTH-1):0],"+
380 pw.println(sd.getSection("fpga"));
383 pw.println("endmodule");
387 } catch (Exception e) { throw new RuntimeException(e); }
390 public int computeOffset(int origin, int target) { return (target - origin)/6; }
391 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
393 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
394 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
395 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
396 public long writeInstruction(Instruction d) { return writeInstruction(d); }
397 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
399 private class SlipwayInstructionEncoder extends InstructionEncoder {
400 public long getDestAddr(Destination box) {
402 if (box instanceof SlipwayBenkoBox)
403 ret = ((SlipwayBenkoBox)box).addr;
405 ret = ((SlipwayBenkoBox.VirtualPort)box).addr;
408 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
409 public Destination getDestByAddr(long dest) {
410 for(Ship ship : Slipway.this)
411 for(BenkoBox bb : ship.getBenkoBoxes()) {
412 if (((SlipwayBenkoBox)bb).addr == dest)
414 for(Destination d : bb.getDestinations())
415 if (getDestAddr(d)==dest)
420 public BenkoBox getBoxByInstAddr(long dest) {
421 for(Ship ship : Slipway.this)
422 for(BenkoBox bb : ship.getBenkoBoxes())
423 if (((SlipwayBenkoBox)bb).instr_addr == dest)