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;
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;
30 boolean torpedoWaiting = false;
32 protected void reset() {
38 dataLatch = new BitVector(getShip().getFleet().getWordWidth());
44 dataReadyForShip = false;
45 readyForDataFromShip = true;
47 torpedoWaiting = false;
50 // Destinations //////////////////////////////////////////////////////////////////////////////
52 /** includes the epilogue fifo */
53 public InterpreterDestination instructionDestination = new InterpreterDestination(this, true);
54 public InterpreterDestination dataDestination = new InterpreterDestination(this, false);
56 InterpreterDock(InterpreterShip ship, DockDescription bbd) {
58 ship.ports.put(bbd.getName(), this);
61 public Path getPath(Destination d, BitVector signal) { return new InterpreterPath(this, (InterpreterDestination)d, signal); }
62 public Destination getInstructionDestination() { return instructionDestination; }
63 public Destination getDataDestination() { return dataDestination; }
64 public int getInstructionFifoSize() { return Integer.MAX_VALUE; }
65 public Interpreter getInterpreter() { return ((InterpreterShip)getShip()).getInterpreter(); }
67 // interface to subclass ///////////////////////////////////////////////////////////////////////
69 /** this will be invoked periodically; should return true to "consume" an instruction, false to leave it executing */
71 public boolean dataReadyForShip() { return dataReadyForShip; }
72 public final boolean readyForDataFromShip() { return readyForDataFromShip; }
74 public long removeDataForShip() {
75 long val = peekDataForShip();
76 dataReadyForShip = false;
79 public long peekDataForShip() {
80 if (!dataReadyForShip) throw new RuntimeException();
81 BitVector bv = dataLatch;
83 for(int i=0; i<bv.length(); i++)
88 public void addDataFromShip(long data) {
89 if (!readyForDataFromShip()) throw new RuntimeException();
90 readyForDataFromShip = false;
94 protected final void service() {
96 if (instructionDestination.packets.size() > 0) {
97 Packet p = instructionDestination.packets.remove();
99 if (torpedoWaiting) throw new RuntimeException("two torpedoes collided!");
100 torpedoWaiting = true;
102 BitVector bv = p.getValue();
104 for(int i=0; i<bv.length(); i++)
107 epilogue.add(getInterpreter().readInstruction(val, this));
111 if (hatchIsOpen && epilogue.size() > 0) {
112 Instruction inst = epilogue.remove();
113 if (inst instanceof Instruction.Head) {
117 if (inst instanceof Instruction.Tail)
120 instructions.add(inst);
123 if (dataReadyForShip) return;
125 if (executing==null && instructions.size() > 0) {
126 executing = instructions.remove();
130 if (executing==null) return;
132 if (executing.looping && hatchIsOpen && olc>0) return;
134 boolean enabled = true;
135 switch(executing.predicate) {
136 case IgnoreFlagD: enabled = true; break;
137 case Default: enabled = olc>0; break;
138 case FlagA: enabled = flag_a; break;
139 case FlagB: enabled = flag_b; break;
140 case FlagC: enabled = flag_c; break;
141 case NotFlagA: enabled = !flag_a; break;
142 case NotFlagB: enabled = !flag_b; break;
143 case NotFlagC: enabled = !flag_c; break;
144 default: throw new RuntimeException();
147 if (executing.looping && olc>0)
148 instructions.add(executing);
153 if (executing instanceof Instruction.Move) {
154 Instruction.Move move = (Instruction.Move)executing;
156 if (move.interruptible && torpedoWaiting) {
157 torpedoWaiting = false;
162 new Packet(tapl, new BitVector(getInterpreter().getWordWidth()), true).send();
167 if (move.dataIn && !isInputDock() && readyForDataFromShip) return;
168 if (move.dataIn && isInputDock() && dataDestination.packets.size()==0) return;
169 if (move.tokenIn && dataDestination.packets.size()==0) return;
172 if (executing.looping && olc>0)
173 instructions.add(executing);
175 if (executing instanceof Instruction.Shift) {
177 Instruction.Shift shift = (Instruction.Shift)executing;
178 for(int i=dataLatch.length()-1; i>=getShip().getFleet().getShiftWidth(); i--)
179 dataLatch.set(i, dataLatch.get(i-getShip().getFleet().getShiftWidth()));
180 for(int i=getShip().getFleet().getShiftWidth()-1; i>=0; i--)
181 dataLatch.set(i, shift.immediate.get(i));
185 throw new RuntimeException("FIXME");
188 if (executing instanceof Instruction.Set) {
189 Instruction.Set set = (Instruction.Set)executing;
192 dataLatch = new BitVector(getInterpreter().getWordWidth()).setAndSignExtend(set.immediate);
194 case InnerLoopCounter:
201 for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_ILC_FROM_IMMEDIATE.valmaskwidth-1; i++)
202 if (dataLatch.get(i))
206 ilc = (int)set.immediate;
209 throw new RuntimeException("FIXME!");
212 case OuterLoopCounter:
215 olc = Math.max(0,olc-1);
216 if (olc==0) hatchIsOpen = true;
220 for(int i=0; i<((FleetTwoFleet)getShip().getFleet()).SET_OLC_FROM_IMMEDIATE.valmaskwidth-1; i++)
221 if (dataLatch.get(i))
223 if (olc==0) hatchIsOpen = true;
226 olc = (int)set.immediate;
227 if (olc==0) hatchIsOpen = true;
230 throw new RuntimeException("FIXME!");
235 boolean new_flag_a = false;
236 boolean new_flag_b = false;
237 for(Predicate p : set.newFlagA)
239 case FlagA: new_flag_a |= flag_a; break;
240 case FlagB: new_flag_a |= flag_b; break;
241 case FlagC: new_flag_a |= flag_c; break;
242 case NotFlagA: new_flag_a |= !flag_a; break;
243 case NotFlagB: new_flag_a |= !flag_b; break;
244 case NotFlagC: new_flag_a |= !flag_c; break;
246 for(Predicate p : set.newFlagB)
248 case FlagA: new_flag_b |= flag_a; break;
249 case FlagB: new_flag_b |= flag_b; break;
250 case FlagC: new_flag_b |= flag_c; break;
251 case NotFlagA: new_flag_b |= !flag_a; break;
252 case NotFlagB: new_flag_b |= !flag_b; break;
253 case NotFlagC: new_flag_b |= !flag_c; break;
260 throw new RuntimeException("FIXME!");
266 Instruction.Move move = (Instruction.Move)executing;
268 // if ILC==0, don't even bother
269 if (ilc==0) { ilc = 1; executing = null; return; }
272 else if (ilc==1) executing = null;
277 p = dataDestination.packets.remove();
278 if (p.getSignal() != null) flag_c = p.getSignal().get(0);
283 p = dataDestination.packets.remove();
284 bv = new BitVector(p.getValue());
285 if (p.getSignal() != null) flag_c = p.getSignal().get(0);
287 bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
288 readyForDataFromShip = true;
290 if (move.latchData) dataLatch = bv;
292 pathLatch = (InterpreterPath)getInterpreter().getPathByAddr(this, ((FleetTwoFleet)getShip().getFleet()).DISPATCH_PATH.getvalAsBitVector(bv.toLong()));
293 // FIXME: c-flag at output docks
296 if (move.path != null) pathLatch = (InterpreterPath)move.path;
298 if (move.dataOut && isInputDock()) dataReadyForShip = true;
299 if (move.dataOut && !isInputDock())
300 new Packet(pathLatch, new BitVector(dataLatch), true).send();
302 new Packet(pathLatch, new BitVector(getInterpreter().getWordWidth()), true).send();