overhaul of interpreter, update ships to match; "make test" works now
[fleet.git] / src / edu / berkeley / fleet / interpreter / Interpreter.java
1 package edu.berkeley.fleet.interpreter;
2 import java.io.*;
3 import java.util.*;
4 import java.util.concurrent.*;
5 import java.lang.reflect.*;
6 import edu.berkeley.sbp.util.ANSI;
7 import edu.berkeley.fleet.api.*;
8 import edu.berkeley.fleet.two.*;
9 import edu.berkeley.fleet.assembler.*;
10 import edu.berkeley.fleet.util.*;
11
12 public class Interpreter extends FleetTwoFleet {
13
14     /** used to allocate serial numbers; see InterpreterDestination for further detail */
15     int maxAllocatedDestinationSerialNumber = 0;
16
17     private InterpreterShip debugShip = null;
18     private BlockingQueue<BitVector> debugStream = new LinkedBlockingQueue<BitVector>();
19     private LinkedHashMap<String,InterpreterShip> ships = new LinkedHashMap<String,InterpreterShip>();
20     public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
21     public Ship getShip(String type, int ordinal) {
22         for(Ship s : this)
23             if (s.getType().equals(type))
24                 if (ordinal-- <= 0)
25                     return s;
26         return null;
27     }
28
29     /** do not use this; it is going to go away */
30     public Interpreter() { this(true); }
31     public Interpreter(boolean logging) {
32         this(new String[] {
33                 "Debug",
34                 "Memory",
35                 "Memory",
36                 "Memory",
37                 "Alu",
38                 "Alu",
39                 "Alu",
40                 "Alu",
41                 "Alu",
42                 "Alu",
43                 "Alu",
44                 "Fifo",
45                 "Fifo",
46                 "Counter",
47                 "Counter",
48                 "Counter",
49                 "Counter",
50                 "Counter",
51                 "Counter",
52                 "Counter",
53                 "Counter",
54                 "Counter",
55                 "Counter",
56                 "Counter",
57                 "Counter",
58                 "Counter",
59                 "Counter",
60                 "Lut3",
61                 "CarrySaveAdder",
62                 "Rotator",
63             }, logging);
64     }
65
66     public Interpreter(String[] ships, boolean logging) {
67         int i=0;
68         Log.quiet = !logging;
69         for(String s : ships) {
70             createShip(ships[i], ships[i]+"_"+i);
71             i++;
72         }
73     }
74
75     void dispatch(Instruction i) {
76         Log.dispatch(i);
77         long il = writeInstruction(i, debugShip.getDock("in"));
78         Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
79         new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
80     }
81
82     private Ship createShip(String shipType, String shipname) {
83         try {
84             if (ships.get(shipname)!=null) return ships.get(shipname);
85             Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
86             Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
87             String src = "/ships/" + shipType + ".ship";
88             InputStream is = getClass().getResourceAsStream(src);
89             BufferedReader br = new BufferedReader(new InputStreamReader(is));
90             ShipDescription sd = new ShipDescription(shipType, br);
91             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
92             ships.put(shipname, ret);
93             if (shipType.equals("Debug") && debugShip == null)
94                 debugShip = ret;
95             return ret;
96         } catch (Exception e) {
97             e.printStackTrace();
98             return null;
99         }
100     }
101
102     void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
103     void debug(BitVector data) {
104         try {
105             if (debugStream != null) debugStream.put(data);
106             else Log.println(ANSI.invert("   DEBUG: got a datum: " +  data+ANSI.clreol()));
107         } catch (Exception e) {
108             throw new RuntimeException(e);
109         }
110     }
111
112     // Instruction Encoding /////////////////////////////////////////////////////////////////////////
113
114     public BitVector getDestAddr(Path path) {
115         long ret = ((InterpreterDestination)path.getDestination()).getSerialNumber();
116         BitVector sig = path.getSignal();
117         BitVector bv = new BitVector(DISPATCH_PATH.valmaskwidth+1);
118         bv.set(ret);
119         if (sig != null) {
120             if (sig.length() > 1) throw new RuntimeException("signal was " + sig.length() + " bits long!");
121             if (sig.length() > 0 && sig.get(0)) bv.set(bv.length()-1,true);
122         }
123         return bv;
124     }
125
126
127     // ShipDescription //////////////////////////////////////////////////////////////////////////////
128
129     public void expand(ShipDescription sd) {
130         try {
131             String filename = sd.getName();
132             //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
133             File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
134             new File(outf.getParent()).mkdirs();
135             System.err.println("writing to " + outf);
136             FileOutputStream out = new FileOutputStream(outf);
137             PrintWriter pw = new PrintWriter(out);
138
139             pw.println("package edu.berkeley.fleet.interpreter;");
140             pw.println("import edu.berkeley.sbp.util.ANSI;");
141             pw.println("import edu.berkeley.fleet.api.*;");
142             pw.println("import edu.berkeley.fleet.two.*;");
143             pw.println("import edu.berkeley.fleet.*;");
144             pw.println("import java.util.*;");
145             pw.println("import java.io.*;");
146             pw.println("");
147             pw.println("public class "+filename+" extends InterpreterShip {");
148             pw.println("");
149             pw.println("    public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
150             pw.println("       super(fleet, sd);");
151             pw.println("    }");
152             pw.println("");
153             for(DockDescription b : sd) {
154                 String name = b.getName();
155                 pw.print("    InterpreterDock box_");
156                 pw.print(name);
157                 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
158             }
159             pw.println("");
160             pw.println(sd.getSection("fleeterpreter"));
161             pw.println("}");
162             pw.flush();
163             pw.close();
164         } catch (Exception e) { throw new RuntimeException(e); }
165     }
166
167     // Run //////////////////////////////////////////////////////////////////////////////
168
169     public FleetProcess run(final Instruction[] instructions) {
170         InterpreterProcess ip = initialize(instructions);
171         new Thread(ip).start();
172         return ip;
173     }
174
175     public InterpreterProcess initialize(Instruction[] instr) {
176         return new InterpreterProcess(instr);
177     }
178
179     public class InterpreterProcess extends FleetProcess implements Runnable {
180         private Instruction[] instructions;
181         public void flush() { }
182         public void sendWord(Destination d, BitVector word) {
183             InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
184             ((InterpreterDestination)d).
185                 addDataFromFabric(new Packet(path, word, false));
186         }
187         public void sendToken(Destination d) {
188             InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
189             ((InterpreterDestination)d).
190                 addDataFromFabric(new Packet(path, new BitVector(getWordWidth()), true));
191         }
192         public InterpreterProcess(Instruction[] instructions) {
193             this.instructions = instructions;
194             for(Instruction i : instructions)
195                 sendInstruction(i);
196         }
197         public Fleet getFleet() { return Interpreter.this; }
198         public void sendInstruction(Instruction i) { dispatch(i); }
199         public Dock getDebugInputDock() { return debugShip.getDock("in"); }
200         public BitVector recvWord() {
201             try {
202                 return debugStream.take();
203             } catch (Exception e) { throw new RuntimeException(e); }
204         }
205         protected void _terminate() { }
206         public void run() {
207             try {
208                 while(!isTerminated())
209                     for(InterpreterShip ship : ships.values())
210                         for(int j=0; j<10; j++)
211                             ship._service();
212                 for(InterpreterShip ship : ships.values())
213                     ship.reset();
214                 debugStream.clear();
215             } catch (Exception e) {
216                 if (isTerminated()) return;
217                 throw new RuntimeException(e);
218             }
219         }
220
221         public void step(Dock d) {
222             ((InterpreterDock)d).service();
223         }
224         
225         public void step(Ship s) {
226             ((InterpreterShip)s).service();
227         }
228
229     }
230 }