make getDestAddr() return a BitVector
[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         long val = peekDataForShip();
74         dataReadyForShip = false;
75         return val;
76     }
77     public long peekDataForShip() {
78         if (!dataReadyForShip) throw new RuntimeException();
79         BitVector bv = dataLatch;
80         long val = 0;
81         for(int i=0; i<bv.length(); i++)
82             if (bv.get(i))
83                 val |= (1L << i);
84         return val;
85     }
86     public void addDataFromShip(long data) {
87         if (!readyForDataFromShip()) throw new RuntimeException();
88         readyForDataFromShip = false;
89         dataFromShip = data;
90     }
91
92     protected final void service() {
93
94         if (instructionDestination.packets.size() > 0) {
95             Packet p = instructionDestination.packets.remove();
96             if (p.isToken) {
97                 if (torpedoWaiting) throw new RuntimeException("two torpedoes collided!");
98                 torpedoWaiting = true;
99             } else {
100                 BitVector bv = p.value;
101                 long val = 0;
102                 for(int i=0; i<bv.length(); i++)
103                     if (bv.get(i))
104                         val |= (1L << i);
105                 epilogue.add(getInterpreter().readInstruction(val, this));
106             }
107         }
108
109         if (hatchIsOpen && epilogue.size() > 0) {
110             Instruction inst = epilogue.remove();
111             if (inst instanceof Instruction.Tail)
112                 hatchIsOpen = false;
113             else
114                 instructions.add(inst);
115         }
116
117         if (dataReadyForShip) return;
118
119         if (executing==null && instructions.size() > 0) executing = instructions.remove();
120         if (executing==null) return;
121
122         if (executing.looping && hatchIsOpen && olc>0) return;
123
124         boolean enabled = true;
125         switch(executing.predicate) {
126             case IgnoreOLC:  enabled =  true;   break;
127             case Default:    enabled =  olc>0;  break;
128             case FlagA:      enabled =  flag_a; break;
129             case FlagB:      enabled =  flag_b; break;
130             case FlagC:      enabled =  flag_c; break;
131             case NotFlagA:   enabled = !flag_a; break;
132             case NotFlagB:   enabled = !flag_b; break;
133             case NotFlagC:   enabled = !flag_c; break;
134             default: throw new RuntimeException();
135         }
136         if (!enabled) {
137             if (executing.looping && olc>0)
138                 instructions.add(executing);
139             executing = null;
140             return;
141         }
142
143         if (executing instanceof Instruction.Move) {
144             Instruction.Move move = (Instruction.Move)executing;
145
146             if (move.interruptible && torpedoWaiting) {
147                 torpedoWaiting = false;
148                 executing = null;
149                 ilc = 1;
150                 olc = 0;
151                 if (tapl != null)
152                     new Packet(tapl, new BitVector(getInterpreter().getWordWidth()), true).send();
153                 hatchIsOpen = true;
154                 return;
155             }
156
157             if (move.dataIn  && !isInputDock() && readyForDataFromShip) return;
158             if (move.dataIn  &&  isInputDock() && dataDestination.packets.size()==0) return;
159             if (move.tokenIn &&                   dataDestination.packets.size()==0) return;
160         }
161
162         if (executing.looping && olc>0)
163             instructions.add(executing);
164
165         if (executing instanceof Instruction.Shift) {
166             /*
167             Instruction.Shift shift = (Instruction.Shift)executing;
168             for(int i=dataLatch.length()-1; i>=getShip().getFleet().getShiftWidth(); i--)
169                 dataLatch.set(i, dataLatch.get(i-getShip().getFleet().getShiftWidth()));
170             for(int i=getShip().getFleet().getShiftWidth()-1; i>=0; i--)
171                 dataLatch.set(i, shift.immediate.get(i));
172             executing = null;
173             return;
174             */
175             throw new RuntimeException("FIXME");
176         }
177
178         if (executing instanceof Instruction.Set) {
179             Instruction.Set set = (Instruction.Set)executing;
180             switch(set.dest) {
181                 case DataLatch:
182                     dataLatch = new BitVector(getInterpreter().getWordWidth()).setAndSignExtend(set.immediate);
183                     break;
184                 case InnerLoopCounter:
185                     switch(set.source) {
186                         case Infinity:
187                             ilc = -1;
188                             break;
189                         case DataLatch:
190                             ilc = 0;
191                             for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_ILC_FROM_IMMEDIATE.valmaskwidth-1; i++)
192                                 if (dataLatch.get(i))
193                                     ilc |= (1 << i);
194                             break;
195                         case Immediate:
196                             ilc = (int)set.immediate;
197                             break;
198                         default:
199                             throw new RuntimeException("FIXME!");
200                     }
201                     break;
202                 case OuterLoopCounter:
203                     switch(set.source) {
204                         case Decrement:
205                             olc = Math.max(0,olc-1);
206                             if (olc==0) hatchIsOpen = true;
207                             break;
208                         case DataLatch:
209                             olc = 0;
210                             for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_OLC_FROM_IMMEDIATE.valmaskwidth-1; i++)
211                                 if (dataLatch.get(i))
212                                     olc |= (1 << i);
213                             if (olc==0) hatchIsOpen = true;
214                             break;
215                         case Immediate:
216                             olc = (int)set.immediate;
217                             if (olc==0) hatchIsOpen = true;
218                             break;
219                         default:
220                             throw new RuntimeException("FIXME!");
221                     }
222                     break;
223
224                 case Flags: {
225                     boolean new_flag_a = false;
226                     boolean new_flag_b = false;
227                     for(Predicate p : set.newFlagA)
228                         switch(p) {
229                             case FlagA: new_flag_a |= flag_a; break;
230                             case FlagB: new_flag_a |= flag_b; break;
231                             case FlagC: new_flag_a |= flag_c; break;
232                             case NotFlagA: new_flag_a |= !flag_a; break;
233                             case NotFlagB: new_flag_a |= !flag_b; break;
234                             case NotFlagC: new_flag_a |= !flag_c; break;
235                         }
236                     for(Predicate p : set.newFlagB)
237                         switch(p) {
238                             case FlagA: new_flag_b |= flag_a; break;
239                             case FlagB: new_flag_b |= flag_b; break;
240                             case FlagC: new_flag_b |= flag_c; break;
241                             case NotFlagA: new_flag_b |= !flag_a; break;
242                             case NotFlagB: new_flag_b |= !flag_b; break;
243                             case NotFlagC: new_flag_b |= !flag_c; break;
244                         }
245                     flag_a = new_flag_a;
246                     flag_b = new_flag_b;
247                     break;
248                 }
249                 default:
250                     throw new RuntimeException("FIXME!");
251             }
252             executing = null;
253             return;
254         }
255
256         Instruction.Move move = (Instruction.Move)executing;
257
258         // if ILC==0, don't even bother
259         if (ilc==0) { ilc = 1; executing = null; return; }
260
261         if (ilc==-1)     { }
262         else if (ilc==1) executing = null;
263         else             ilc--;
264
265         Packet p = null;
266         if (move.tokenIn) {
267             p = dataDestination.packets.remove();
268             if (p.path.signal != null) flag_c = p.path.signal.get(0);
269         }
270         if (move.dataIn) {
271             BitVector bv = null;
272             if (isInputDock()) {
273                 p = dataDestination.packets.remove();
274                 bv = new BitVector(p.value);
275                 if (p.path.signal != null) flag_c = p.path.signal.get(0);
276             } else {
277                 bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
278                 readyForDataFromShip = true;
279             }
280             if (move.latchData) dataLatch = bv;
281             if (move.latchPath)
282                 pathLatch = (InterpreterPath)getInterpreter().getPathByAddr(this, ((FleetTwoFleet)getShip().getFleet()).DISPATCH_PATH.getvalAsBitVector(bv.toLong()));
283             // FIXME: c-flag at output docks
284         }
285
286         if (move.path != null) pathLatch = (InterpreterPath)move.path;
287
288         if (move.dataOut && isInputDock()) dataReadyForShip = true;
289         if (move.dataOut && !isInputDock())
290             new Packet(pathLatch, new BitVector(dataLatch), true).send();
291         if (move.tokenOut)
292             new Packet(pathLatch, new BitVector(getInterpreter().getWordWidth()), true).send();
293
294         return;
295     }
296 }