3cacdf13260f64ff94bb17eb47fb19d0356a1929
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterDock.java
1 package edu.berkeley.fleet.interpreter;
2 import java.util.*;
3 import edu.berkeley.fleet.two.*;
4 import edu.berkeley.fleet.api.*;
5 import edu.berkeley.sbp.util.ANSI;
6
7 /** anything that has a source (instruction horn) address on the switch fabric */
8 class InterpreterDock extends FleetTwoDock {
9
10     // Dock State //////////////////////////////////////////////////////////////////////////////
11     
12     boolean         flag_a = false;
13     boolean         flag_b = false;
14     boolean         flag_c = false;
15     boolean         flag_d = false;
16     int             ilc    = 1;
17     int             olc    = 1;
18     final BitVector dataLatch = new BitVector(getShip().getFleet().getWordWidth());
19     InterpreterPath pathLatch = null;
20     boolean         requeueStageInCirculatingState = false;
21     boolean         requeueStageHasTailInstruction = false;
22     boolean         torpedoWaiting = false;
23     boolean         flushing = false;
24
25     LinkedList<Instruction> instructions = new LinkedList<Instruction>();
26     LinkedList<Packet> dataPackets = new LinkedList<Packet>();
27
28     // HACK
29     private LinkedList<Instruction> instructionsBackedUpIntoSwitchFabric = new LinkedList<Instruction>();
30
31     boolean dataReadyForShip = false;
32     boolean readyForDataFromShip = true;
33
34     // FIXME: should be a BitVector
35     long dataFromShip;
36     boolean flagCFromShip;
37
38     protected void reset() {
39         ilc = 1;
40         olc = 1;
41         flag_a = false;
42         flag_b = false;
43         flag_c = false;
44         flag_d = false;
45         dataLatch.set(0);
46         pathLatch = null;
47         requeueStageInCirculatingState = false;
48         requeueStageHasTailInstruction = false;
49         instructions.clear();
50         dataPackets.clear();
51         instructionsBackedUpIntoSwitchFabric.clear();
52         dataReadyForShip = false;
53         readyForDataFromShip = true;
54         torpedoWaiting = false;
55         flushing = false;
56     }
57
58     // Destinations //////////////////////////////////////////////////////////////////////////////
59
60     /** includes the epilogue fifo */
61     public InterpreterDestination instructionDestination = new InterpreterDestination(this) {
62             public String toString() { return getDock()+":i"; }
63             public void addDataFromFabric(Packet p) {
64                 if (p.isToken()) {
65                     if (instructionsBackedUpIntoSwitchFabric.size()!=0)
66                         throw new RuntimeException("torpedo arrived while instructions were backed up into switch fabric");
67                     if (torpedoWaiting) throw new RuntimeException("two torpedoes collided at dock "+this);
68                     torpedoWaiting = true;
69                     return;
70                 }
71
72                 Instruction inst =
73                     getInterpreter().decodeInstruction(p.getValue(),
74                                                        InterpreterDock.this /* this is wrong, but harmless */);
75                 addInstruction(inst);
76             }
77         };
78     public InterpreterDestination dataDestination = new InterpreterDestination(this) {
79             public String toString() { return getDock()+""; }
80             public void addDataFromFabric(Packet packet) { dataPackets.add(packet); }
81         };
82
83     InterpreterDock(InterpreterShip ship, DockDescription bbd) {
84         super(ship, bbd);
85         ship.docks.put(bbd.getName(), this);
86     }
87
88     public Path getPath(Destination d, BitVector signal) { return new InterpreterPath(this, (InterpreterDestination)d, signal); }
89     public Destination getInstructionDestination() { return instructionDestination; }
90     public Destination getDataDestination() { return dataDestination; }
91     public int getInstructionFifoSize() { return Integer.MAX_VALUE; }
92     public Interpreter getInterpreter() { return ((InterpreterShip)getShip()).getInterpreter(); }
93
94     boolean trace = false;
95
96     private void addInstruction(Instruction inst) {
97         if (requeueStageInCirculatingState || requeueStageHasTailInstruction) {
98             // GROSS HACK
99             instructionsBackedUpIntoSwitchFabric.add(inst);
100             return;
101         }
102         if (inst instanceof Instruction.Tail) {
103             requeueStageHasTailInstruction = true;
104             return;
105         }
106         instructions.add(inst);
107     }
108     
109     protected final void service() {
110
111         if (dataReadyForShip || flushing) return;
112         if (instructions.size()==0) return;
113
114         if (instructions.peek() instanceof Instruction.Head) {
115             if (requeueStageInCirculatingState) { instructions.remove(); return; }
116             if (!requeueStageHasTailInstruction) return;
117             requeueStageHasTailInstruction = false;
118             requeueStageInCirculatingState = true;
119             instructions.remove();
120             return;
121         }
122
123         // in the while..false idiom block below, use "break" to
124         // consume the instruction at instructions.peek(), or "return"
125         // to leave it and retry on the next call.
126         do {
127             if (!instructions.peek().predicate.evaluate(flag_a, flag_b, flag_c, flag_d))
128                 break;
129
130             if (instructions.peek() instanceof Instruction.Move) {
131                 Instruction.Move move = (Instruction.Move)instructions.peek();
132
133                 if (ilc==0) { ilc = 1; break; }
134
135                 if (move.interruptible && torpedoWaiting) {
136                     torpedoWaiting = false;
137                     ilc = 1;
138                     flag_d = true;
139                     break;
140                 }
141
142                 if (move.dataIn  && !isInputDock() && readyForDataFromShip)  return;
143                 if (move.dataIn  &&  isInputDock() && dataPackets.size()==0) return;
144                 if (move.tokenIn &&                   dataPackets.size()==0) return;
145
146                 if (move.tokenIn) {
147                     Packet p = dataPackets.remove();
148                     flag_c = p.getSignal().get(0);
149                 }
150                 if (move.dataIn) {
151                     BitVector bv = null;
152                     if (isInputDock()) {
153                         Packet p = dataPackets.remove();
154                         bv = new BitVector(p.getValue());
155                         flag_c = p.getSignal().get(0);
156                     } else {
157                         bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
158                         readyForDataFromShip = true;
159                         if (move.latchData) flag_c = flagCFromShip;
160                     }
161                     if (move.latchData) dataLatch.set(bv);
162                     if (move.latchPath) {
163                         BitVector bvp = ((FleetTwoFleet)getShip().getFleet()).DISPATCH_PATH.getvalAsBitVector(bv);
164                         pathLatch = (InterpreterPath)getInterpreter().getPathByAddr(this, bvp);
165                     }
166                 }
167                 
168                 if (move.path != null) pathLatch = (InterpreterPath)move.path;
169                 
170                 if (move.dataOut && isInputDock())  dataReadyForShip = true;
171                 if (move.dataOut && !isInputDock()) new Packet(pathLatch, new BitVector(dataLatch), false).send();
172                 if (move.tokenOut)                  new Packet(pathLatch, new BitVector(getInterpreter().getWordWidth()), true).send();
173                 
174                 if (ilc==1)  break;
175                 if (ilc!=-1) ilc--;
176                 return;
177
178             } else if (instructions.peek() instanceof Instruction.Abort) {
179                 requeueStageInCirculatingState = false;
180                 while (instructionsBackedUpIntoSwitchFabric.size()!=0)
181                     addInstruction(instructionsBackedUpIntoSwitchFabric.remove());
182                 break;
183
184             } else if (instructions.peek() instanceof Instruction.Flush) {
185                 flushing = true;
186                 break;
187
188             } else if (instructions.peek() instanceof Instruction.Shift) {
189                 Instruction.Shift shift = (Instruction.Shift)instructions.peek();
190                 for(int i=dataLatch.length()-1; i>=getShip().getFleet().getShiftWidth(); i--)
191                     dataLatch.set(i, dataLatch.get(i-getShip().getFleet().getShiftWidth()));
192                 BitVector shift_immediate = shift.immediate.getBitVector();
193                 for(int i=getShip().getFleet().getShiftWidth()-1; i>=0; i--)
194                     dataLatch.set(i, shift_immediate.get(i));
195                 break;
196
197             } else if (instructions.peek() instanceof Instruction.Set) {
198                 Instruction.Set set = (Instruction.Set)instructions.peek();
199                 switch(set.dest) {
200                     case DataLatch: dataLatch.setAndSignExtend(set.immediate);
201                     break;
202                     case InnerLoopCounter:
203                         switch(set.source) {
204                             case Infinity:  ilc = -1; break;
205                             case Immediate: ilc = (int)set.immediate; break;
206                             case DataLatch:
207                                 ilc = 0;
208                                 for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_ILC_FROM_IMMEDIATE.valmaskwidth-1; i++)
209                                     if (dataLatch.get(i))
210                                         ilc |= (1 << i);
211                                 break;
212                             default: throw new RuntimeException("impossible");
213                         }
214                         break;
215                     case OuterLoopCounter:
216                         switch(set.source) {
217                             case Decrement: olc = Math.max(0,olc-1); break;
218                             case Immediate: olc = (int)set.immediate; break;
219                             case DataLatch:
220                                 olc = 0;
221                                 for(int i=0; i<getShip().getFleet().getWordWidth(); i++)
222                                     if (dataLatch.get(i))
223                                         olc |= (1 << i);
224                                 break;
225                             default: throw new RuntimeException("impossible");
226                         }
227                         flag_d = olc==0;
228                         break;
229                         
230                     case Flags: {
231                         boolean new_flag_a = set.newFlagA.evaluate(flag_a, flag_b, flag_c, flag_d);
232                         boolean new_flag_b = set.newFlagB.evaluate(flag_a, flag_b, flag_c, flag_d);
233                         flag_a = new_flag_a;
234                         flag_b = new_flag_b;
235                         break;
236                     }
237                     default: throw new RuntimeException("FIXME!");
238                 }
239             } else {
240                 throw new RuntimeException("unimplemented instruction: " + instructions.peek());
241             }
242         } while(false);
243
244         if (requeueStageInCirculatingState)
245             instructions.add(instructions.peek());
246         instructions.remove();
247         return;
248     }
249
250     // Interface for use by Subclasses ///////////////////////////////////////////////////////////////////////
251
252     // all the methods below convert 64-bit longs to/from
253     // getWordWidth()-bit BitVectors by truncation and sign-extension.
254
255     protected boolean dataReadyForShip() { return dataReadyForShip; }
256     protected final boolean readyForDataFromShip() { return readyForDataFromShip; }
257     protected long removeDataForShip() {
258         long val = peekDataForShip();
259         dataReadyForShip = false;
260         return val;
261     }
262     protected long peekDataForShip() {
263         if (!dataReadyForShip)
264             throw new RuntimeException("peekDataForShip() invoked when dataReadyForShip()==false");
265         return dataLatch.toLong();
266     }
267     protected void addDataFromShip(long data) { addDataFromShip(data, false); }
268     protected void addDataFromShip(long data, boolean pending_flag_c) {
269         if (!readyForDataFromShip())
270             throw new RuntimeException("addDataFromShip() invoked when readyForDataFromShip()");
271         readyForDataFromShip = false;
272         dataFromShip = data;
273         flagCFromShip = pending_flag_c;
274     }
275
276
277     // Debugging //////////////////////////////////////////////////////////////////////////////
278
279     public void dumpState() {
280         if (instructions.size()==0 &&
281             dataPackets.size()==0 &&
282             instructionsBackedUpIntoSwitchFabric.size()==0 &&
283             !requeueStageHasTailInstruction &&
284             !requeueStageInCirculatingState &&
285             !torpedoWaiting &&
286             !flushing &&
287             !dataReadyForShip &&
288             readyForDataFromShip)
289             return;
290         System.out.println("state of "+ANSI.green(this)+": "+
291                            (ilc==1?"":("[ilc="+(ilc==-1 ? "*" : (ilc+""))+"] "))+
292                            (olc==1?"":("[olc="+olc+"] "))+
293                            (flag_a?"[a] ":"")+
294                            (flag_b?"[b] ":"")+
295                            (flag_c?"[c] ":"")+
296                            (flag_d?"[d] ":"")+
297                            (requeueStageInCirculatingState?"[recirculating] ":"")+
298                            (requeueStageHasTailInstruction?"[tail waiting] ":"")+
299                            (torpedoWaiting?"[torpedo waiting] ":"")+
300                            (flushing?"[flushing] ":"")
301                            );
302         if (!readyForDataFromShip)
303             System.out.println(ANSI.cyan("  ship has proffered: " + dataFromShip));
304         if (dataReadyForShip)
305             System.out.println(ANSI.cyan("  waiting for ship to accept: " + dataLatch.toLong()));
306         for(Instruction i : instructions)
307             System.out.println(ANSI.red("  "+i));
308         for(Instruction i : instructionsBackedUpIntoSwitchFabric)
309             System.out.println(ANSI.red(ANSI.bold("  "+i+" BACKED UP")));
310         for(Packet p : dataPackets)
311             System.out.println(ANSI.cyan("  "+p));
312     }
313
314 }