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 for(Destination d : p.getDestinations()) {
231 if (!(d instanceof SlipwayBenkoBox.VirtualPort)) continue;
232 SlipwayBenkoBox.VirtualPort vp = (SlipwayBenkoBox.VirtualPort)d;
233 vp.addr = p.addr | (count << bits);
240 return new Node(name,
242 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
243 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
247 private String describe(String prefix, Object o) {
248 if (o==null) return null;
249 if (o instanceof SlipwayBenkoBox) {
250 SlipwayBenkoBox p = (SlipwayBenkoBox)o;
251 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
253 if (o instanceof Node) {
254 return ((Node)o).describe(prefix);
265 public Node(String name, String component, Object left, Object right, int addr, int bits) {
269 this.component = component;
273 public void dumpChannels(int indentamount, boolean decl) {
275 for(int i=0; i<indentamount; i++) indent += " ";
277 String n = describe(prefix).startsWith("instruction")
278 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
279 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
281 System.out.println(" "+indent+
283 "krunk"+(krunk++)+"(clk, "+
284 describe(prefix)+"_r, "+
285 describe(prefix)+"_a, "+
286 describe(prefix)+", "+
287 FabricTree.this.describe(prefix, left)+"_r, "+
288 FabricTree.this.describe(prefix, left)+"_a, "+
289 FabricTree.this.describe(prefix, left)+", "+
290 FabricTree.this.describe(prefix, right)+"_r, "+
291 FabricTree.this.describe(prefix, right)+"_a, "+
292 FabricTree.this.describe(prefix, right)+
295 dumpChannels(left, indentamount+1, decl);
296 dumpChannels(right, indentamount+1, decl);
298 public void dumpChannels(Object o, int indentamount, boolean decl) {
300 if (o instanceof Node) {
301 ((Node)o).dumpChannels(indentamount, decl);
304 for(int i=0; i<indentamount; i++) indent += " ";
306 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
307 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
308 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
312 public String describe(String prefix) {
317 public static int krunk=0;
319 private static String getUniqueName(Ship ship) {
320 return ship.getType() + ship.getOrdinal();
323 public void expand(ShipDescription sd) {
325 String filename = sd.getName().toLowerCase();
326 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
327 new File(outf.getParent()).mkdirs();
328 System.err.println("writing to " + outf);
329 FileOutputStream out = new FileOutputStream(outf);
330 PrintWriter pw = new PrintWriter(out);
332 boolean auto = filename.equals("alu2") || filename.equals("alu1") || filename.equals("lut3") || filename.equals("choice") || filename.equals("alu3") || filename.equals("stack");
334 pw.println("`include \"macros.v\"");
336 pw.println("module " + filename + "( clk");
337 for(BenkoBoxDescription bb : sd) {
338 String bb_name = bb.getName();
341 pw.print(", " + bb_name+"_r");
342 pw.print(", " + bb_name+"_a_");
343 pw.print(", " + bb_name+"_d");
345 pw.print(", " + bb_name+"_r_");
346 pw.print(", " + bb_name+"_a");
347 pw.print(", " + bb_name+"_d_");
353 pw.println(" input clk;");
354 for(BenkoBoxDescription bb : sd) {
355 String bb_name = bb.getName();
358 pw.println("`input(" +
362 "[(`PACKET_WIDTH-1):0],"+
366 pw.println("`output(" +
370 "[(`PACKET_WIDTH-1):0],"+
373 pw.println("`defreg(" +
375 "[(`PACKET_WIDTH-1):0],"+
383 pw.println(sd.getSection("fpga"));
386 pw.println("endmodule");
390 } catch (Exception e) { throw new RuntimeException(e); }
393 public int computeOffset(int origin, int target) { return (target - origin)/6; }
394 public int computeTarget(int origin, int offset) { return origin + (offset*6); }
396 private SlipwayInstructionEncoder iie = new SlipwayInstructionEncoder();
397 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
398 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
399 public long writeInstruction(Instruction d) { return writeInstruction(d); }
400 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
402 private class SlipwayInstructionEncoder extends InstructionEncoder {
403 public long getDestAddr(Destination box) {
405 if (box instanceof SlipwayBenkoBox)
406 ret = ((SlipwayBenkoBox)box).addr;
408 ret = ((SlipwayBenkoBox.VirtualPort)box).addr;
411 public long getBoxInstAddr(BenkoBox box) { return ((SlipwayBenkoBox)box).instr_addr; }
412 public Destination getDestByAddr(long dest) {
413 for(Ship ship : Slipway.this)
414 for(BenkoBox bb : ship.getBenkoBoxes()) {
415 if (((SlipwayBenkoBox)bb).addr == dest)
417 for(Destination d : bb.getDestinations())
418 if (getDestAddr(d)==dest)
423 public BenkoBox getBoxByInstAddr(long dest) {
424 for(Ship ship : Slipway.this)
425 for(BenkoBox bb : ship.getBenkoBoxes())
426 if (((SlipwayBenkoBox)bb).instr_addr == dest)