1 package edu.berkeley.fleet.slipway;
2 import edu.berkeley.fleet.interpreter.*;
3 import edu.berkeley.fleet.api.*;
4 import edu.berkeley.fleet.ies44.*;
5 import edu.berkeley.fleet.*;
6 import java.lang.reflect.*;
7 import edu.berkeley.sbp.chr.*;
8 import edu.berkeley.sbp.misc.*;
9 import edu.berkeley.sbp.meta.*;
10 import edu.berkeley.sbp.bind.*;
11 import edu.berkeley.sbp.util.*;
14 import edu.berkeley.fleet.ships.*;
16 public class Slipway extends Interpreter {
19 createShip("Alu2", "alu2a");
20 createShip("Alu2", "alu2b");
21 createShip("Alu2", "alu2c");
22 createShip("Alu2", "alu2d");
23 createShip("Debug", "debug");
24 //createShip("Execute", "execute");
25 createShip("Fifo", "fifo1");
26 createShip("Fifo", "fifo2");
27 createShip("Fifo", "fifo3");
28 createShip("Fifo", "fifo4");
29 createShip("Iscratch", "iscratch1");
30 createShip("Iscratch", "iscratch2");
31 createShip("Dscratch", "dscratch1");
32 createShip("Dscratch", "dscratch2");
36 public Ship createShip(String type, String name) {
37 InterpreterShip ship = (InterpreterShip)super.createShip(type, name);
38 if (ship.getClass().getSimpleName().equals("Debug")) {
39 new DataOutbox(ship, "out", true);
41 } else if (ship.getClass().getSimpleName().equals("Execute")) {
42 new DataOutbox(ship, "ihorn", true, true, false);
43 new DataOutbox(ship, "dhorn", true, false, true);
45 } else if (ship.getClass().getSimpleName().equals("Iscratch")) {
46 new DataInbox(ship, "command", true);
47 new DataOutbox(ship, "ihorn", true, true, false);
48 new DataOutbox(ship, "dhorn", true, false, true);
53 public FleetProcess run(final byte[] instructions) {
55 return new Client(instructions);
56 } catch (IOException e) { throw new RuntimeException(e); }
59 public void dumpFabric(boolean quiet) {
60 // FIXME: this is really ugly: the order of port declarations in
61 // the XXXShip.java file must match the order in the .balsa file!
63 ArrayList instructionports = new ArrayList<InterpreterBenkoBox>();
64 for(InterpreterShip ship : shiplist)
65 for(BenkoBox port : ship.getBenkoBoxes())
66 if (!((InterpreterBenkoBox)port).special())
67 instructionports.add(port);
68 FabricTree instructions =
69 new FabricTree((InterpreterBenkoBox[])instructionports.toArray(new InterpreterBenkoBox[0]),
73 ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
74 for(InterpreterShip ship : shiplist)
75 for(BenkoBox port : ship.getBenkoBoxes())
76 if (!((InterpreterBenkoBox)port).special())
79 new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
83 ArrayList outputports = new ArrayList<InterpreterBenkoBox>();
84 for(InterpreterShip ship : shiplist)
85 for(BenkoBox port : ship.getBenkoBoxes())
86 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
87 outputports.add(port);
89 new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
93 ArrayList ihornports = new ArrayList<InterpreterBenkoBox>();
94 for(InterpreterShip ship : shiplist)
95 for(BenkoBox port : ship.getBenkoBoxes())
96 if (((InterpreterBenkoBox)port).ihorn())
99 new FabricTree((InterpreterBenkoBox[])ihornports.toArray(new InterpreterBenkoBox[0]),
104 System.out.println("`include \"macros.v\"");
105 System.out.println("module fabric(clk, data_Iscratch0_command_r, data_Iscratch0_command_a, data_Iscratch0_command,");
106 System.out.println(" data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
107 System.out.println(" input clk;");
108 System.out.println(" input data_Iscratch0_command_r;");
109 System.out.println(" output data_Iscratch0_command_a;");
110 System.out.println(" output data_Debug0_out_r;");
111 System.out.println(" input data_Debug0_out_a;");
112 System.out.println(" output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
113 System.out.println(" input [(`PACKET_WIDTH-1):0] data_Iscratch0_command;");
114 //System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_Iscratch0_ihorn;");
115 //System.out.println(" wire [(`PACKET_WIDTH-1):0] data_Iscratch0_dhorn;");
116 System.out.println();
118 System.out.println();
120 instructions.dumpChannels(true);
121 outputs.dumpChannels(true);
122 inputs.dumpChannels(true);
123 ihorns.dumpChannels(true);
124 for(InterpreterShip ship : shiplist)
125 for(BenkoBox port : ship.getBenkoBoxes())
126 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
127 System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"
128 +getUniqueName(ship)+"_"+port.getName()+";");
130 System.out.println("");
131 instructions.dumpChannels(false);
132 System.out.println("");
133 outputs.dumpChannels(false);
134 System.out.println("");
135 inputs.dumpChannels(false);
136 System.out.println("");
137 ihorns.dumpChannels(false);
138 System.out.println("");
139 for(InterpreterShip ship : shiplist) {
140 System.out.print(ship.getClass().getSimpleName().toLowerCase());
141 System.out.print(" ");
142 System.out.print("krunk"+(krunk++));
143 System.out.print("(clk, ");
144 boolean first = true;
145 for(BenkoBox port : ship.getBenkoBoxes()) {
146 if (!first) System.out.print(", ");
148 String prefix = "data_";
149 if (((InterpreterBenkoBox)port).ihorn()) prefix = "ihorn_";
150 if (((InterpreterBenkoBox)port).dhorn()) prefix = "source_";
151 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
152 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
153 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
154 System.out.print(" ");
156 System.out.println(");");
158 for(BenkoBox port : ship.getBenkoBoxes()) {
159 if (((InterpreterBenkoBox)port).special()) continue;
160 if (port instanceof Inbox) {
161 System.out.print("inbox");
163 System.out.print("outbox");
165 System.out.print(" krunk"+(krunk++)+"(clk, ");
166 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
167 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
168 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
169 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
170 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
171 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
172 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
173 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
174 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
175 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
176 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
177 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
178 System.out.print(");");
179 System.out.println();
184 System.out.println("funnel topfun(clk,"+
185 " dest_r, dest_a, dest,"+
186 " source_r, source_a, source,"+
187 " data_Iscratch0_dhorn_r, data_Iscratch0_dhorn_a, data_Iscratch0_dhorn);");
189 System.out.println("assign instruction_r = ihorn_r;");
190 System.out.println("assign ihorn_a = instruction_a;");
191 System.out.println("assign instruction = ihorn;");
192 System.out.println("assign dest_r = source_r;");
193 System.out.println("assign source_a = dest_a;");
194 System.out.println("assign dest = source;");
195 System.out.println("endmodule");
198 private static class FabricTree {
202 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
203 public FabricTree(InterpreterBenkoBox[] ports, String component, String prefix) {
204 this.prefix = prefix;
205 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
207 private Object mkNode(String name, String component, InterpreterBenkoBox[] ports,
208 int start, int end, int addr, int bits) {
209 if (end-start == 0) return null;
210 if (end-start == 1) {
211 InterpreterBenkoBox p = ports[start];
212 if (prefix.equals("instruction")) {
215 } else if (prefix.equals("dest")) {
222 return new Node(name,
224 mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
225 mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
229 private String describe(String prefix, Object o) {
230 if (o==null) return null;
231 if (o instanceof InterpreterBenkoBox) {
232 InterpreterBenkoBox p = (InterpreterBenkoBox)o;
233 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
235 if (o instanceof Node) {
236 return ((Node)o).describe(prefix);
247 public Node(String name, String component, Object left, Object right, int addr, int bits) {
251 this.component = component;
255 public void dumpChannels(int indentamount, boolean decl) {
257 for(int i=0; i<indentamount; i++) indent += " ";
259 String n = describe(prefix).startsWith("instruction")
260 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
261 System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
263 System.out.println(" "+indent+
265 "krunk"+(krunk++)+"(clk, "+
266 describe(prefix)+"_r, "+
267 describe(prefix)+"_a, "+
268 describe(prefix)+", "+
269 FabricTree.this.describe(prefix, left)+"_r, "+
270 FabricTree.this.describe(prefix, left)+"_a, "+
271 FabricTree.this.describe(prefix, left)+", "+
272 FabricTree.this.describe(prefix, right)+"_r, "+
273 FabricTree.this.describe(prefix, right)+"_a, "+
274 FabricTree.this.describe(prefix, right)+
277 dumpChannels(left, indentamount+1, decl);
278 dumpChannels(right, indentamount+1, decl);
280 public void dumpChannels(Object o, int indentamount, boolean decl) {
282 if (o instanceof Node) {
283 ((Node)o).dumpChannels(indentamount, decl);
286 for(int i=0; i<indentamount; i++) indent += " ";
288 String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
289 ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
290 System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
294 public String describe(String prefix) {
299 public static int krunk=0;
301 private static String getUniqueName(Ship ship) {
302 return ship.getType() + ship.getOrdinal();