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