major fpga code reorg; added icache+dcache
[fleet.git] / src / edu / berkeley / fleet / slipway / Slipway.java
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.*;
12 import java.util.*;
13 import java.io.*;
14 import edu.berkeley.fleet.ships.*;
15
16 public class Slipway extends Interpreter {
17
18     public Slipway() {
19         createShip("Alu2",    "alu2");
20         createShip("Debug",   "debug");
21         createShip("Execute", "execute");
22         createShip("Fifo",    "fifo");
23         createShip("Icache",  "icache");
24         createShip("Dcache",  "dcache");
25         /*
26         createShip("Alu2",    "alu2a");
27         createShip("Alu2",    "alu2b");
28         createShip("Alu2",    "alu2c");
29         createShip("Alu2",    "alu2d");
30         createShip("Debug",   "debug");
31         createShip("Execute", "execute");
32         createShip("Fifo",    "fifo1");
33         createShip("Fifo",    "fifo2");
34         createShip("Fifo",    "fifo3");
35         createShip("Fifo",    "fifo4");
36         createShip("Icache",  "icache1");
37         //createShip("Icache",  "icache2");                                                                                 
38         createShip("Dcache",  "dcache1");
39         createShip("Dcache",  "dcache2");
40          */
41     }
42
43     public void dumpFabric(boolean quiet) {
44         // FIXME: this is really ugly: the order of port declarations in
45         //        the XXXShip.java file must match the order in the .balsa file!
46
47         ArrayList instructionports = new ArrayList<InterpreterBenkoBox>();
48         for(InterpreterShip ship : shiplist)
49             for(BenkoBox port : ship.getBenkoBoxes())
50                 if (!((InterpreterBenkoBox)port).special())
51                     instructionports.add(port);
52         FabricTree instructions =
53             new FabricTree((InterpreterBenkoBox[])instructionports.toArray(new InterpreterBenkoBox[0]),
54                            "ihorn",
55                            "instruction");
56
57         ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
58         for(InterpreterShip ship : shiplist)
59             for(BenkoBox port : ship.getBenkoBoxes())
60                 if (!((InterpreterBenkoBox)port).special())
61                     inputports.add(port);
62         FabricTree inputs =
63             new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
64                            "horn",
65                            "dest");
66
67         ArrayList outputports = new ArrayList<InterpreterBenkoBox>();
68         for(InterpreterShip ship : shiplist)
69             for(BenkoBox port : ship.getBenkoBoxes())
70                 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
71                     outputports.add(port);
72         FabricTree outputs =
73             new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
74                            "funnel",
75                            "source");
76
77         ArrayList ihornports = new ArrayList<InterpreterBenkoBox>();
78         for(InterpreterShip ship : shiplist)
79             for(BenkoBox port : ship.getBenkoBoxes())
80                 if (((InterpreterBenkoBox)port).ihorn())
81                     ihornports.add(port);
82         FabricTree ihorns =
83             new FabricTree((InterpreterBenkoBox[])ihornports.toArray(new InterpreterBenkoBox[0]),
84                            "funnel",
85                            "ihorn");
86         
87         if (quiet) return;
88         System.out.println("`include \"macros.v\"");
89         System.out.println("module fabric(clk, data_Icache0_command_r, data_Icache0_command_a, data_Icache0_command,");
90         System.out.println("                   data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
91         System.out.println("  input  clk;");
92         System.out.println("  input  data_Icache0_command_r;");
93         System.out.println("  output data_Icache0_command_a;");
94         System.out.println("  output data_Debug0_out_r;");
95         System.out.println("  input  data_Debug0_out_a;");
96         System.out.println("  output [(`PACKET_WIDTH-1):0]      data_Debug0_out;");
97         System.out.println("  input  [(`PACKET_WIDTH-1):0]      data_Icache0_command;");
98         //System.out.println("  wire   [(`INSTRUCTION_WIDTH-1):0] data_Icache0_ihorn;");
99         //System.out.println("  wire   [(`PACKET_WIDTH-1):0]      data_Icache0_dhorn;");
100         System.out.println();
101         
102         System.out.println();
103
104         instructions.dumpChannels(true);
105         outputs.dumpChannels(true);
106         inputs.dumpChannels(true);
107         ihorns.dumpChannels(true);
108         for(InterpreterShip ship : shiplist)
109             for(BenkoBox port : ship.getBenkoBoxes())
110                 if (!((InterpreterBenkoBox)port).special() || ((InterpreterBenkoBox)port).dhorn())
111                     System.out.println("  wire [(`PACKET_WIDTH-1):0] data_"
112                                        +getUniqueName(ship)+"_"+port.getName()+";");
113
114         System.out.println("");
115         instructions.dumpChannels(false);
116         System.out.println("");
117         outputs.dumpChannels(false);
118         System.out.println("");
119         inputs.dumpChannels(false);
120         System.out.println("");
121         ihorns.dumpChannels(false);
122         System.out.println("");
123         for(InterpreterShip ship : shiplist) {
124             System.out.print(ship.getClass().getSimpleName().toLowerCase());
125             System.out.print(" ");
126             System.out.print("krunk"+(krunk++));
127             System.out.print("(clk, ");
128             boolean first = true;
129             for(BenkoBox port : ship.getBenkoBoxes()) {
130                 if (!first) System.out.print(", ");
131                 first = false;
132                 String prefix = "data_";
133                 if (((InterpreterBenkoBox)port).ihorn()) prefix = "ihorn_";
134                 if (((InterpreterBenkoBox)port).dhorn()) prefix = "source_";
135                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
136                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
137                 System.out.print(prefix+getUniqueName(port.getShip())+"_"+port.getName());
138                 System.out.print(" ");
139             }
140             System.out.println(");");
141
142             for(BenkoBox port : ship.getBenkoBoxes()) {
143                 if (((InterpreterBenkoBox)port).special()) continue;
144                 if (port instanceof Inbox) {
145                     System.out.print("inbox");
146                 } else {
147                     System.out.print("outbox");
148                 }
149                 System.out.print(" krunk"+(krunk++)+"(clk, ");
150                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
151                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
152                 System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
153                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
154                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
155                 System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
156                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
157                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
158                 System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
159                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
160                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
161                 System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
162                 System.out.print(");");
163                 System.out.println();
164             }
165
166         }
167         /*
168         System.out.println("funnel topfun(clk,"+
169                            "              dest_r, dest_a, dest,"+
170                            "              source_r, source_a, source,"+
171                            "              data_Icache0_dhorn_r, data_Icache0_dhorn_a, data_Icache0_dhorn);");
172         */
173         System.out.println("assign instruction_r = ihorn_r;");
174         System.out.println("assign ihorn_a = instruction_a;");
175         System.out.println("assign instruction = ihorn;");
176         System.out.println("assign dest_r = source_r;");
177         System.out.println("assign source_a = dest_a;");
178         System.out.println("assign dest = source;");
179         System.out.println("endmodule");
180     }
181
182     private static class FabricTree {
183         int master_idx = 1;
184         String prefix;
185         Node root;
186         public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
187         public FabricTree(InterpreterBenkoBox[] ports, String component, String prefix) {
188             this.prefix = prefix;
189             root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
190         }
191         private Object mkNode(String name, String component, InterpreterBenkoBox[] ports,
192                               int start, int end, int addr, int bits) {
193             if (end-start == 0) return null;
194             if (end-start == 1) {
195                 InterpreterBenkoBox p = ports[start];
196                 if (prefix.equals("instruction")) {
197                     p.instr_addr = addr;
198                     p.instr_bits = bits;
199                 } else if (prefix.equals("dest")) {
200                     p.addr = addr;
201                     p.bits = bits;
202                 }
203                 return p;
204             }
205             int len = end-start;
206             return new Node(name,
207                             component,
208                             mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
209                             mkNode(name+"_1", component, ports, start+len/2, end,   addr | (1 << bits), bits+1),
210                             addr,
211                             bits);
212         }
213         private String describe(String prefix, Object o) {
214             if (o==null) return null;
215             if (o instanceof InterpreterBenkoBox) {
216                 InterpreterBenkoBox p = (InterpreterBenkoBox)o;
217                 return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
218             }
219             if (o instanceof Node) {
220                 return ((Node)o).describe(prefix);
221             }
222             return null;
223         }
224         private class Node {
225             Object left;
226             Object right;
227             String name;
228             String component;
229             int addr;
230             int bits;
231             public Node(String name, String component, Object left, Object right, int addr, int bits) {
232                 this.left = left;
233                 this.right = right;
234                 this.name = name;
235                 this.component = component;
236                 this.addr = addr;
237                 this.bits = bits;
238             }
239             public void dumpChannels(int indentamount, boolean decl) {
240                 String indent = "";
241                 for(int i=0; i<indentamount; i++) indent += "  ";
242                 if (decl) {
243                     String n = describe(prefix).startsWith("instruction")
244                         ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
245                     System.out.println("  wire "+n+" "+indent+describe(prefix)+";");
246                 } else {
247                     System.out.println("     "+indent+
248                                        component+" "+
249                                        "krunk"+(krunk++)+"(clk, "+
250                                        describe(prefix)+"_r, "+
251                                        describe(prefix)+"_a, "+
252                                        describe(prefix)+", "+
253                                        FabricTree.this.describe(prefix, left)+"_r, "+
254                                        FabricTree.this.describe(prefix, left)+"_a, "+
255                                        FabricTree.this.describe(prefix, left)+", "+
256                                        FabricTree.this.describe(prefix, right)+"_r, "+
257                                        FabricTree.this.describe(prefix, right)+"_a, "+
258                                        FabricTree.this.describe(prefix, right)+
259                                        ");");
260                 }
261                 dumpChannels(left, indentamount+1, decl);
262                 dumpChannels(right, indentamount+1, decl);
263             }
264             public void dumpChannels(Object o, int indentamount, boolean decl) {
265                 if (o==null) return;
266                 if (o instanceof Node) {
267                     ((Node)o).dumpChannels(indentamount, decl);
268                 } else {
269                     String indent = "";
270                     for(int i=0; i<indentamount; i++) indent += "  ";
271                     if (decl) {
272                         String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
273                             ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
274                         System.out.println("  wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
275                     }
276                 }
277             }
278             public String describe(String prefix) {
279                 return prefix+name;
280             }
281         }
282     }
283     public static int krunk=0;
284
285     private static String getUniqueName(Ship ship) {
286         return ship.getType() + ship.getOrdinal();
287     }
288 }