finish overhaul of interpreter
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterDock.java
1 package edu.berkeley.fleet.interpreter;
2 import java.util.*;
3 import edu.berkeley.sbp.util.ANSI;
4 import edu.berkeley.fleet.two.*;
5 import edu.berkeley.fleet.api.*;
6 import edu.berkeley.fleet.api.Instruction;
7 import static edu.berkeley.fleet.api.Predicate.*;
8
9 /** anything that has a source (instruction horn) address on the switch fabric */
10 class InterpreterDock extends FleetTwoDock {
11
12     // Dock State //////////////////////////////////////////////////////////////////////////////
13     
14     public boolean flag_a = false;
15     public boolean flag_b = false;
16     public boolean flag_c = false;
17     public int ilc = 1;
18     public int olc = 1;
19     public BitVector dataLatch = new BitVector(getShip().getFleet().getWordWidth());
20     public InterpreterPath pathLatch = null;
21     public InterpreterPath tapl = null;
22     public boolean hatchIsOpen = true;
23     private Instruction executing = null;
24     private Queue<Instruction> instructions = new LinkedList<Instruction>();
25     private Queue<Instruction> epilogue = new LinkedList<Instruction>();
26     private boolean dataReadyForShip = false;
27     private boolean readyForDataFromShip = true;
28     private long dataFromShip;
29     private boolean torpedoWaiting = false;
30
31     protected void reset() {
32         ilc = 1;
33         olc = 1;
34         flag_a = false;
35         flag_b = false;
36         flag_c = false;
37         dataLatch = new BitVector(getShip().getFleet().getWordWidth());
38         pathLatch = null;
39         hatchIsOpen = true;
40         executing = null;
41         instructions.clear();
42         epilogue.clear();
43         dataReadyForShip = false;
44         readyForDataFromShip = true;
45         tapl = null;
46         torpedoWaiting = false;
47     }
48
49     // Destinations //////////////////////////////////////////////////////////////////////////////
50
51     /** includes the epilogue fifo */
52     public InterpreterDestination instructionDestination = new InterpreterDestination(this, true);
53     public InterpreterDestination dataDestination = new InterpreterDestination(this, false);
54
55     InterpreterDock(InterpreterShip ship, DockDescription bbd) {
56         super(ship, bbd);
57     }
58
59     public Path getPath(Destination d, BitVector signal) { return new InterpreterPath(this, (InterpreterDestination)d, signal); }
60     public Destination getInstructionDestination() { return instructionDestination; }
61     public Destination getDataDestination() { return dataDestination; }
62     public int getInstructionFifoSize() { return Integer.MAX_VALUE; }
63     public Interpreter getInterpreter() { return ((InterpreterShip)getShip()).getInterpreter(); }
64
65     // interface to subclass ///////////////////////////////////////////////////////////////////////
66
67     /** this will be invoked periodically; should return true to "consume" an instruction, false to leave it executing */
68
69     public boolean dataReadyForShip() { return dataReadyForShip; }
70     public final boolean readyForDataFromShip() { return readyForDataFromShip; }
71
72     public long removeDataForShip() {
73         if (!dataReadyForShip) throw new RuntimeException();
74         dataReadyForShip = false;
75         BitVector bv = dataLatch;
76         long val = 0;
77         for(int i=0; i<bv.length(); i++)
78             if (bv.get(i))
79                 val |= (1L << i);
80         return val;
81     }
82     public void addDataFromShip(long data) {
83         if (!readyForDataFromShip()) throw new RuntimeException();
84         readyForDataFromShip = false;
85         dataFromShip = data;
86     }
87
88     protected final void service() {
89
90         if (instructionDestination.packets.size() > 0) {
91             Packet p = instructionDestination.packets.remove();
92             if (p.isToken) {
93                 if (torpedoWaiting) throw new RuntimeException("two torpedoes collided!");
94                 torpedoWaiting = true;
95             } else {
96                 BitVector bv = p.value;
97                 long val = 0;
98                 for(int i=0; i<bv.length(); i++)
99                     if (bv.get(i))
100                         val |= (1L << i);
101                 epilogue.add(getInterpreter().readInstruction(val, this));
102             }
103         }
104
105         if (hatchIsOpen && epilogue.size() > 0) {
106             Instruction inst = epilogue.remove();
107             if (inst instanceof Instruction.Tail)
108                 hatchIsOpen = false;
109             else
110                 instructions.add(inst);
111         }
112
113         if (dataReadyForShip) return;
114
115         if (executing==null && instructions.size() > 0) executing = instructions.remove();
116         if (executing==null) return;
117
118         if (executing.looping && hatchIsOpen && olc>0) return;
119
120         boolean enabled = true;
121         switch(executing.predicate) {
122             case IgnoreOLC:  enabled =  true;   break;
123             case Default:    enabled =  olc>0;  break;
124             case FlagA:      enabled =  flag_a; break;
125             case FlagB:      enabled =  flag_b; break;
126             case FlagC:      enabled =  flag_c; break;
127             case NotFlagA:   enabled = !flag_a; break;
128             case NotFlagB:   enabled = !flag_b; break;
129             case NotFlagC:   enabled = !flag_c; break;
130             default: throw new RuntimeException();
131         }
132         if (!enabled) {
133             if (executing.looping && olc>0)
134                 instructions.add(executing);
135             executing = null;
136             return;
137         }
138
139         if (executing instanceof Instruction.Move) {
140             Instruction.Move move = (Instruction.Move)executing;
141
142             if (move.interruptible && torpedoWaiting) {
143                 torpedoWaiting = false;
144                 executing = null;
145                 ilc = 1;
146                 olc = 0;
147                 if (tapl != null)
148                     new Packet(tapl, new BitVector(getInterpreter().getWordWidth()), true).send();
149                 hatchIsOpen = true;
150                 return;
151             }
152
153             if (move.dataIn  && !isInputDock() && readyForDataFromShip) return;
154             if (move.dataIn  &&  isInputDock() && dataDestination.packets.size()==0) return;
155             if (move.tokenIn &&                   dataDestination.packets.size()==0) return;
156         }
157
158         if (executing.looping && olc>0)
159             instructions.add(executing);
160
161         if (executing instanceof Instruction.Shift) {
162             Instruction.Shift shift = (Instruction.Shift)executing;
163             for(int i=dataLatch.length()-1; i>=19; i--)
164                 dataLatch.set(i, dataLatch.get(i-19));
165             for(int i=18; i>=0; i--)
166                 dataLatch.set(i, shift.immediate.get(i));
167             executing = null;
168             return;
169         }
170
171         if (executing instanceof Instruction.Set) {
172             Instruction.Set set = (Instruction.Set)executing;
173             switch(set.dest) {
174                 case DataLatch:
175                     dataLatch = new BitVector(getInterpreter().getWordWidth()).setAndSignExtend(set.immediate);
176                     break;
177                 case InnerLoopCounter:
178                     switch(set.source) {
179                         case Infinity:
180                             ilc = -1;
181                             break;
182                         case DataLatch:
183                             ilc = 0;
184                             for(int i=0; i<FleetTwoFleet.SET_ILC_FROM_IMMEDIATE.valmaskwidth-1; i++)
185                                 if (dataLatch.get(i))
186                                     ilc |= (1 << i);
187                             break;
188                         case Immediate:
189                             ilc = (int)set.immediate;
190                             break;
191                         default:
192                             throw new RuntimeException("FIXME!");
193                     }
194                     break;
195                 case OuterLoopCounter:
196                     switch(set.source) {
197                         case Decrement:
198                             olc = Math.max(0,olc-1);
199                             if (olc==0) hatchIsOpen = true;
200                             break;
201                         case DataLatch:
202                             olc = 0;
203                             for(int i=0; i<FleetTwoFleet.SET_OLC_FROM_IMMEDIATE.valmaskwidth-1; i++)
204                                 if (dataLatch.get(i))
205                                     olc |= (1 << i);
206                             if (olc==0) hatchIsOpen = true;
207                             break;
208                         case Immediate:
209                             olc = (int)set.immediate;
210                             if (olc==0) hatchIsOpen = true;
211                             break;
212                         default:
213                             throw new RuntimeException("FIXME!");
214                     }
215                     break;
216
217                 case TAPL:
218                     tapl = (InterpreterPath)set.path;
219                     break;
220
221                 case Flags: {
222                     boolean new_flag_a = false;
223                     boolean new_flag_b = false;
224                     for(Predicate p : set.newFlagA)
225                         switch(p) {
226                             case FlagA: new_flag_a |= flag_a; break;
227                             case FlagB: new_flag_a |= flag_b; break;
228                             case FlagC: new_flag_a |= flag_c; break;
229                             case NotFlagA: new_flag_a |= !flag_a; break;
230                             case NotFlagB: new_flag_a |= !flag_b; break;
231                             case NotFlagC: new_flag_a |= !flag_c; break;
232                         }
233                     for(Predicate p : set.newFlagB)
234                         switch(p) {
235                             case FlagA: new_flag_b |= flag_a; break;
236                             case FlagB: new_flag_b |= flag_b; break;
237                             case FlagC: new_flag_b |= flag_c; break;
238                             case NotFlagA: new_flag_b |= !flag_a; break;
239                             case NotFlagB: new_flag_b |= !flag_b; break;
240                             case NotFlagC: new_flag_b |= !flag_c; break;
241                         }
242                     flag_a = new_flag_a;
243                     flag_b = new_flag_b;
244                     break;
245                 }
246                 default:
247                     throw new RuntimeException("FIXME!");
248             }
249             executing = null;
250             return;
251         }
252
253         Instruction.Move move = (Instruction.Move)executing;
254
255         // if ILC==0, don't even bother
256         if (ilc==0) { ilc = 1; executing = null; return; }
257
258         if (ilc==-1)     { }
259         else if (ilc==1) executing = null;
260         else             ilc--;
261
262         Packet p = null;
263         if (move.tokenIn) {
264             p = dataDestination.packets.remove();
265             if (p.path.signal != null) flag_c = p.path.signal.get(0);
266         }
267         if (move.dataIn) {
268             BitVector bv = null;
269             if (isInputDock()) {
270                 p = dataDestination.packets.remove();
271                 bv = new BitVector(p.value);
272                 if (p.path.signal != null) flag_c = p.path.signal.get(0);
273             } else {
274                 bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
275                 readyForDataFromShip = true;
276             }
277             if (move.latchData) dataLatch = bv;
278             if (move.latchPath)
279                 pathLatch = (InterpreterPath)getInterpreter().getPathByAddr(this, FleetTwoFleet.DISPATCH_PATH.getval(bv));
280             // FIXME: c-flag at output docks
281         }
282
283         if (move.path != null) pathLatch = (InterpreterPath)move.path;
284
285         if (move.dataOut && isInputDock()) dataReadyForShip = true;
286         if (move.dataOut && !isInputDock())
287             new Packet(pathLatch, new BitVector(dataLatch), true).send();
288         if (move.tokenOut)
289             new Packet(pathLatch, new BitVector(getInterpreter().getWordWidth()), true).send();
290
291         return;
292     }
293 }