Make several fields of the Interpreter non-private (for fgd)
[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
24     Instruction executing = null;
25     Queue<Instruction> instructions = new LinkedList<Instruction>();
26     Queue<Instruction> epilogue = new LinkedList<Instruction>();
27     boolean dataReadyForShip = false;
28     boolean readyForDataFromShip = true;
29     long dataFromShip;
30     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) {
121             executing = instructions.remove();
122             return;
123         }
124
125         if (executing==null) return;
126
127         if (executing.looping && hatchIsOpen && olc>0) return;
128
129         boolean enabled = true;
130         switch(executing.predicate) {
131             case IgnoreOLC:  enabled =  true;   break;
132             case Default:    enabled =  olc>0;  break;
133             case FlagA:      enabled =  flag_a; break;
134             case FlagB:      enabled =  flag_b; break;
135             case FlagC:      enabled =  flag_c; break;
136             case NotFlagA:   enabled = !flag_a; break;
137             case NotFlagB:   enabled = !flag_b; break;
138             case NotFlagC:   enabled = !flag_c; break;
139             default: throw new RuntimeException();
140         }
141         if (!enabled) {
142             if (executing.looping && olc>0)
143                 instructions.add(executing);
144             executing = null;
145             return;
146         }
147
148         if (executing instanceof Instruction.Move) {
149             Instruction.Move move = (Instruction.Move)executing;
150
151             if (move.interruptible && torpedoWaiting) {
152                 torpedoWaiting = false;
153                 executing = null;
154                 ilc = 1;
155                 olc = 0;
156                 if (tapl != null)
157                     new Packet(tapl, new BitVector(getInterpreter().getWordWidth()), true).send();
158                 hatchIsOpen = true;
159                 return;
160             }
161
162             if (move.dataIn  && !isInputDock() && readyForDataFromShip) return;
163             if (move.dataIn  &&  isInputDock() && dataDestination.packets.size()==0) return;
164             if (move.tokenIn &&                   dataDestination.packets.size()==0) return;
165         }
166
167         if (executing.looping && olc>0)
168             instructions.add(executing);
169
170         if (executing instanceof Instruction.Shift) {
171             /*
172             Instruction.Shift shift = (Instruction.Shift)executing;
173             for(int i=dataLatch.length()-1; i>=getShip().getFleet().getShiftWidth(); i--)
174                 dataLatch.set(i, dataLatch.get(i-getShip().getFleet().getShiftWidth()));
175             for(int i=getShip().getFleet().getShiftWidth()-1; i>=0; i--)
176                 dataLatch.set(i, shift.immediate.get(i));
177             executing = null;
178             return;
179             */
180             throw new RuntimeException("FIXME");
181         }
182
183         if (executing instanceof Instruction.Set) {
184             Instruction.Set set = (Instruction.Set)executing;
185             switch(set.dest) {
186                 case DataLatch:
187                     dataLatch = new BitVector(getInterpreter().getWordWidth()).setAndSignExtend(set.immediate);
188                     break;
189                 case InnerLoopCounter:
190                     switch(set.source) {
191                         case Infinity:
192                             ilc = -1;
193                             break;
194                         case DataLatch:
195                             ilc = 0;
196                             for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_ILC_FROM_IMMEDIATE.valmaskwidth-1; i++)
197                                 if (dataLatch.get(i))
198                                     ilc |= (1 << i);
199                             break;
200                         case Immediate:
201                             ilc = (int)set.immediate;
202                             break;
203                         default:
204                             throw new RuntimeException("FIXME!");
205                     }
206                     break;
207                 case OuterLoopCounter:
208                     switch(set.source) {
209                         case Decrement:
210                             olc = Math.max(0,olc-1);
211                             if (olc==0) hatchIsOpen = true;
212                             break;
213                         case DataLatch:
214                             olc = 0;
215                             for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_OLC_FROM_IMMEDIATE.valmaskwidth-1; i++)
216                                 if (dataLatch.get(i))
217                                     olc |= (1 << i);
218                             if (olc==0) hatchIsOpen = true;
219                             break;
220                         case Immediate:
221                             olc = (int)set.immediate;
222                             if (olc==0) hatchIsOpen = true;
223                             break;
224                         default:
225                             throw new RuntimeException("FIXME!");
226                     }
227                     break;
228
229                 case Flags: {
230                     boolean new_flag_a = false;
231                     boolean new_flag_b = false;
232                     for(Predicate p : set.newFlagA)
233                         switch(p) {
234                             case FlagA: new_flag_a |= flag_a; break;
235                             case FlagB: new_flag_a |= flag_b; break;
236                             case FlagC: new_flag_a |= flag_c; break;
237                             case NotFlagA: new_flag_a |= !flag_a; break;
238                             case NotFlagB: new_flag_a |= !flag_b; break;
239                             case NotFlagC: new_flag_a |= !flag_c; break;
240                         }
241                     for(Predicate p : set.newFlagB)
242                         switch(p) {
243                             case FlagA: new_flag_b |= flag_a; break;
244                             case FlagB: new_flag_b |= flag_b; break;
245                             case FlagC: new_flag_b |= flag_c; break;
246                             case NotFlagA: new_flag_b |= !flag_a; break;
247                             case NotFlagB: new_flag_b |= !flag_b; break;
248                             case NotFlagC: new_flag_b |= !flag_c; break;
249                         }
250                     flag_a = new_flag_a;
251                     flag_b = new_flag_b;
252                     break;
253                 }
254                 default:
255                     throw new RuntimeException("FIXME!");
256             }
257             executing = null;
258             return;
259         }
260
261         Instruction.Move move = (Instruction.Move)executing;
262
263         // if ILC==0, don't even bother
264         if (ilc==0) { ilc = 1; executing = null; return; }
265
266         if (ilc==-1)     { }
267         else if (ilc==1) executing = null;
268         else             ilc--;
269
270         Packet p = null;
271         if (move.tokenIn) {
272             p = dataDestination.packets.remove();
273             if (p.path.signal != null) flag_c = p.path.signal.get(0);
274         }
275         if (move.dataIn) {
276             BitVector bv = null;
277             if (isInputDock()) {
278                 p = dataDestination.packets.remove();
279                 bv = new BitVector(p.value);
280                 if (p.path.signal != null) flag_c = p.path.signal.get(0);
281             } else {
282                 bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
283                 readyForDataFromShip = true;
284             }
285             if (move.latchData) dataLatch = bv;
286             if (move.latchPath)
287                 pathLatch = (InterpreterPath)getInterpreter().getPathByAddr(this, ((FleetTwoFleet)getShip().getFleet()).DISPATCH_PATH.getvalAsBitVector(bv.toLong()));
288             // FIXME: c-flag at output docks
289         }
290
291         if (move.path != null) pathLatch = (InterpreterPath)move.path;
292
293         if (move.dataOut && isInputDock()) dataReadyForShip = true;
294         if (move.dataOut && !isInputDock())
295             new Packet(pathLatch, new BitVector(dataLatch), true).send();
296         if (move.tokenOut)
297             new Packet(pathLatch, new BitVector(getInterpreter().getWordWidth()), true).send();
298
299         return;
300     }
301 }