1 package edu.berkeley.fleet.interpreter;
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.*;
9 /** anything that has a source (instruction horn) address on the switch fabric */
10 class InterpreterDock extends FleetTwoDock {
12 // Dock State //////////////////////////////////////////////////////////////////////////////
14 public boolean flag_a = false;
15 public boolean flag_b = false;
16 public boolean flag_c = false;
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;
31 protected void reset() {
37 dataLatch = new BitVector(getShip().getFleet().getWordWidth());
43 dataReadyForShip = false;
44 readyForDataFromShip = true;
46 torpedoWaiting = false;
49 // Destinations //////////////////////////////////////////////////////////////////////////////
51 /** includes the epilogue fifo */
52 public InterpreterDestination instructionDestination = new InterpreterDestination(this, true);
53 public InterpreterDestination dataDestination = new InterpreterDestination(this, false);
55 InterpreterDock(InterpreterShip ship, DockDescription bbd) {
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(); }
65 // interface to subclass ///////////////////////////////////////////////////////////////////////
67 /** this will be invoked periodically; should return true to "consume" an instruction, false to leave it executing */
69 public boolean dataReadyForShip() { return dataReadyForShip; }
70 public final boolean readyForDataFromShip() { return readyForDataFromShip; }
72 public long removeDataForShip() {
73 long val = peekDataForShip();
74 dataReadyForShip = false;
77 public long peekDataForShip() {
78 if (!dataReadyForShip) throw new RuntimeException();
79 BitVector bv = dataLatch;
81 for(int i=0; i<bv.length(); i++)
86 public void addDataFromShip(long data) {
87 if (!readyForDataFromShip()) throw new RuntimeException();
88 readyForDataFromShip = false;
92 protected final void service() {
94 if (instructionDestination.packets.size() > 0) {
95 Packet p = instructionDestination.packets.remove();
97 if (torpedoWaiting) throw new RuntimeException("two torpedoes collided!");
98 torpedoWaiting = true;
100 BitVector bv = p.value;
102 for(int i=0; i<bv.length(); i++)
105 epilogue.add(getInterpreter().readInstruction(val, this));
109 if (hatchIsOpen && epilogue.size() > 0) {
110 Instruction inst = epilogue.remove();
111 if (inst instanceof Instruction.Tail)
114 instructions.add(inst);
117 if (dataReadyForShip) return;
119 if (executing==null && instructions.size() > 0) executing = instructions.remove();
120 if (executing==null) return;
122 if (executing.looping && hatchIsOpen && olc>0) return;
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();
137 if (executing.looping && olc>0)
138 instructions.add(executing);
143 if (executing instanceof Instruction.Move) {
144 Instruction.Move move = (Instruction.Move)executing;
146 if (move.interruptible && torpedoWaiting) {
147 torpedoWaiting = false;
152 new Packet(tapl, new BitVector(getInterpreter().getWordWidth()), true).send();
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;
162 if (executing.looping && olc>0)
163 instructions.add(executing);
165 if (executing instanceof Instruction.Shift) {
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));
175 throw new RuntimeException("FIXME");
178 if (executing instanceof Instruction.Set) {
179 Instruction.Set set = (Instruction.Set)executing;
182 dataLatch = new BitVector(getInterpreter().getWordWidth()).setAndSignExtend(set.immediate);
184 case InnerLoopCounter:
191 for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_ILC_FROM_IMMEDIATE.valmaskwidth-1; i++)
192 if (dataLatch.get(i))
196 ilc = (int)set.immediate;
199 throw new RuntimeException("FIXME!");
202 case OuterLoopCounter:
205 olc = Math.max(0,olc-1);
206 if (olc==0) hatchIsOpen = true;
210 for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_OLC_FROM_IMMEDIATE.valmaskwidth-1; i++)
211 if (dataLatch.get(i))
213 if (olc==0) hatchIsOpen = true;
216 olc = (int)set.immediate;
217 if (olc==0) hatchIsOpen = true;
220 throw new RuntimeException("FIXME!");
225 boolean new_flag_a = false;
226 boolean new_flag_b = false;
227 for(Predicate p : set.newFlagA)
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;
236 for(Predicate p : set.newFlagB)
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;
250 throw new RuntimeException("FIXME!");
256 Instruction.Move move = (Instruction.Move)executing;
258 // if ILC==0, don't even bother
259 if (ilc==0) { ilc = 1; executing = null; return; }
262 else if (ilc==1) executing = null;
267 p = dataDestination.packets.remove();
268 if (p.path.signal != null) flag_c = p.path.signal.get(0);
273 p = dataDestination.packets.remove();
274 bv = new BitVector(p.value);
275 if (p.path.signal != null) flag_c = p.path.signal.get(0);
277 bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
278 readyForDataFromShip = true;
280 if (move.latchData) dataLatch = bv;
282 pathLatch = (InterpreterPath)getInterpreter().getPathByAddr(this, ((FleetTwoFleet)getShip().getFleet()).DISPATCH_PATH.getvalAsBitVector(bv.toLong()));
283 // FIXME: c-flag at output docks
286 if (move.path != null) pathLatch = (InterpreterPath)move.path;
288 if (move.dataOut && isInputDock()) dataReadyForShip = true;
289 if (move.dataOut && !isInputDock())
290 new Packet(pathLatch, new BitVector(dataLatch), true).send();
292 new Packet(pathLatch, new BitVector(getInterpreter().getWordWidth()), true).send();