1 package edu.berkeley.fleet.slipway;
2 import edu.berkeley.fleet.interpreter.*;
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.*;
15 import edu.berkeley.fleet.interpreter.ships.*;
17 public class Slipway extends Interpreter {
20 createShip("Alu2", "alu2a");
21 createShip("Alu2", "alu2b");
22 createShip("Alu2", "alu2c");
23 createShip("Alu2", "alu2d");
24 createShip("Debug", "debug");
25 //createShip("Execute", "execute");
26 createShip("Fifo", "fifo1");
27 createShip("Fifo", "fifo2");
28 createShip("Fifo", "fifo3");
29 createShip("Fifo", "fifo4");
30 createShip("Iscratch", "iscratch1");
31 createShip("Iscratch", "iscratch2");
32 createShip("Dscratch", "dscratch1");
33 createShip("Dscratch", "dscratch2");
37 public Ship createShip(String type, String name) {
38 InterpreterShip ship = (InterpreterShip)super.createShip(type, name);
39 if (ship.getClass().getSimpleName().equals("Debug")) {
40 new DataOutbox(ship, "out", true);
42 } else if (ship.getClass().getSimpleName().equals("Execute")) {
43 new DataOutbox(ship, "ihorn", true, true, false);
44 new DataOutbox(ship, "dhorn", true, false, true);
46 } else if (ship.getClass().getSimpleName().equals("Iscratch")) {
47 new DataInbox(ship, "command", true);
48 new DataOutbox(ship, "ihorn", true, true, false);
49 new DataOutbox(ship, "dhorn", true, false, true);
54 public FleetProcess run(final byte[] instructions) {
56 return new Client(instructions);
57 } catch (IOException e) { throw new RuntimeException(e); }
60 public void dumpFabric(boolean quiet) {
61 // FIXME: this is really ugly: the order of port declarations in
62 // the XXXShip.java file must match the order in the .balsa file!
64 ArrayList instructionports = new ArrayList<InterpreterBenkoBox>();
65 for(InterpreterShip ship : shiplist)
66 for(BenkoBox port : ship.getBenkoBoxes())
67 if (!((InterpreterBenkoBox)port).special())
68 instructionports.add(port);
69 FabricTree instructions =
70 new FabricTree((InterpreterBenkoBox[])instructionports.toArray(new InterpreterBenkoBox[0]),
74 ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
75 for(InterpreterShip ship : shiplist)
76 for(BenkoBox port : ship.getBenkoBoxes())
77 if (!((InterpreterBenkoBox)port).special())
80 new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
84 ArrayList outputports = new ArrayList<InterpreterBenkoBox>();
85 for(InterpreterShip ship : shiplist)
86 for(BenkoBox port : ship.getBenkoBoxes())
87 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
88 outputports.add(port);
90 new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
94 ArrayList ihornports = new ArrayList<InterpreterBenkoBox>();
95 for(InterpreterShip ship : shiplist)
96 for(BenkoBox port : ship.getBenkoBoxes())
97 if (((InterpreterBenkoBox)port).ihorn())
100 new FabricTree((InterpreterBenkoBox[])ihornports.toArray(new InterpreterBenkoBox[0]),
105 System.out.println("`include \"macros.v\"");
106 System.out.println("module fabric(clk, data_Iscratch0_command_r, data_Iscratch0_command_a, data_Iscratch0_command,");
107 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
108 System.out.println(" input clk;");
109 System.out.println(" input data_Iscratch0_command_r;");
110 System.out.println(" output data_Iscratch0_command_a;");
111 System.out.println(" output data_Debug0_out_r;");
112 System.out.println(" input data_Debug0_out_a;");
113 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
114 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Iscratch0_command;");
115 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Iscratch0_ihorn;");
116 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Iscratch0_dhorn;");
117 System.out.println();
119 System.out.println();
121 instructions.dumpChannels(true);
122 outputs.dumpChannels(true);
123 inputs.dumpChannels(true);
124 ihorns.dumpChannels(true);
125 for(InterpreterShip ship : shiplist)
126 for(BenkoBox port : ship.getBenkoBoxes())
127 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
128 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
129 +getUniqueName(ship)+"_"+port.getName()+";");
131 System.out.println("");
132 instructions.dumpChannels(false);
133 System.out.println("");
134 outputs.dumpChannels(false);
135 System.out.println("");
136 inputs.dumpChannels(false);
137 System.out.println("");
138 ihorns.dumpChannels(false);
139 System.out.println("");
140 for(InterpreterShip ship : shiplist) {
141 System.out.print(ship.getClass().getSimpleName().toLowerCase());
142 System.out.print(" ");
143 System.out.print("krunk"+(krunk++));
144 System.out.print("(clk, ");
145 boolean first = true;
146 for(BenkoBox port : ship.getBenkoBoxes()) {
147 if (!first) System.out.print(", ");
149 String prefix = "data_";
150 if (((InterpreterBenkoBox)port).ihorn()) prefix = "ihorn_";
151 if (((InterpreterBenkoBox)port).dhorn()) prefix = "source_";
152 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
153 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
154 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
155 System.out.print(" ");
157 System.out.println(");");
159 for(BenkoBox port : ship.getBenkoBoxes()) {
160 if (((InterpreterBenkoBox)port).special()) continue;
161 if (port instanceof Inbox) {
162 System.out.print("inbox");
164 System.out.print("outbox");
166 System.out.print(" krunk"+(krunk++)+"(clk, ");
167 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
168 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
169 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
170 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
171 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
172 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
173 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
174 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
175 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
176 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
177 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
178 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
179 System.out.print(");");
180 System.out.println();
185 System.out.println("funnel topfun(clk,"+
186 " dest_r, dest_a, dest,"+
187 " source_r, source_a, source,"+
188 " data_Iscratch0_dhorn_r, data_Iscratch0_dhorn_a, data_Iscratch0_dhorn);");
190 System.out.println("assign instruction_r = ihorn_r;");
191 System.out.println("assign ihorn_a = instruction_a;");
192 System.out.println("assign instruction = ihorn;");
193 System.out.println("assign dest_r = source_r;");
194 System.out.println("assign source_a = dest_a;");
195 System.out.println("assign dest = source;");
196 System.out.println("endmodule");
199 private static class FabricTree {
203 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
204 public FabricTree(InterpreterBenkoBox[] ports, String component, String prefix) {
205 this.prefix = prefix;
206 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
208 private Object mkNode(String name, String component, InterpreterBenkoBox[] ports,
209 int start, int end, int addr, int bits) {
210 if (end-start == 0) return null;
211 if (end-start == 1) {
212 InterpreterBenkoBox p = ports[start];
213 if (prefix.equals("instruction")) {
216 } else if (prefix.equals("dest")) {
223 return new Node(name,
225 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
226 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
230 private String describe(String prefix, Object o) {
231 if (o==null) return null;
232 if (o instanceof InterpreterBenkoBox) {
233 InterpreterBenkoBox p = (InterpreterBenkoBox)o;
234 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
236 if (o instanceof Node) {
237 return ((Node)o).describe(prefix);
248 public Node(String name, String component, Object left, Object right, int addr, int bits) {
252 this.component = component;
256 public void dumpChannels(int indentamount, boolean decl) {
258 for(int i=0; i<indentamount; i++) indent += " ";
260 String n = describe(prefix).startsWith("instruction")
261 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
262 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
264 System.out.println(" "+indent+
266 "krunk"+(krunk++)+"(clk, "+
267 describe(prefix)+"_r, "+
268 describe(prefix)+"_a, "+
269 describe(prefix)+", "+
270 FabricTree.this.describe(prefix, left)+"_r, "+
271 FabricTree.this.describe(prefix, left)+"_a, "+
272 FabricTree.this.describe(prefix, left)+", "+
273 FabricTree.this.describe(prefix, right)+"_r, "+
274 FabricTree.this.describe(prefix, right)+"_a, "+
275 FabricTree.this.describe(prefix, right)+
278 dumpChannels(left, indentamount+1, decl);
279 dumpChannels(right, indentamount+1, decl);
281 public void dumpChannels(Object o, int indentamount, boolean decl) {
283 if (o instanceof Node) {
284 ((Node)o).dumpChannels(indentamount, decl);
287 for(int i=0; i<indentamount; i++) indent += " ";
289 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
290 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
291 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
295 public String describe(String prefix) {
300 public static int krunk=0;
302 private static String getUniqueName(Ship ship) {
303 return ship.getType() + ship.getOrdinal();
306 public void expand(ShipDescription sd) {
308 String filename = sd.name.toLowerCase();
309 File outf = new File("src/edu/berkeley/fleet/slipway/"+filename+".v");
310 new File(outf.getParent()).mkdirs();
311 System.err.println("writing to " + outf);
312 FileOutputStream out = new FileOutputStream(outf);
313 PrintWriter pw = new PrintWriter(out);
315 pw.println(sd.sections.get("fpga"));
318 } catch (Exception e) { throw new RuntimeException(e); }