1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.sbp.util.ANSI;
3 import edu.berkeley.fleet.api.*;
4 import edu.berkeley.fleet.ies44.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.fleet.util.*;
7 import static edu.berkeley.fleet.util.BitManipulations.*;
8 import edu.berkeley.fleet.api.Instruction;
10 public class Outbox extends InstructionPump {
12 /** are we ready to accept another item from the ship? */
13 private boolean readyForDataFromShip = false;
14 private boolean haveDataFromShip = false;
16 /** data which has been presented by the ship and is waiting to depart */
17 private long itemPresentedByShip;
19 /** number of tokens queued on the trigger input */
20 private int triggersReceived = 0;
22 /** the latched value */
23 private long register;
25 public boolean isInbox() { return false; }
26 public boolean isOutbox() { return true; }
27 public Outbox(InterpreterShip ship, String name) { this(ship, name, new String[] { "" }); }
28 public Outbox(InterpreterShip ship, String name, String[] ports) { super(ship, name, ports); }
30 protected void setDataLatch(long value) { register = value; }
31 protected long peekDataLatch() { return register; }
33 protected final boolean service(Instruction.Executable instruction_) {
34 if (clogged>0) return false;
35 if (instruction_ instanceof Instruction.Clog) { clogged++; return true; }
36 if (instruction_ instanceof Instruction.LocalLiteral) {
37 Instruction.LocalLiteral ll = (Instruction.LocalLiteral)instruction_;
40 ? setField(36, 19, ll.literal, register)
41 : setField(18, 0, ll.literal, register);
44 Instruction.Move instruction = (Instruction.Move)instruction_;
46 // if no instruction waiting, do nothing
47 if (instruction == null) return false;
49 // check firing conditions
50 if (instruction.tokenIn && triggersReceived <= 0) return false;
51 if (instruction.dataIn) {
52 if (!haveDataFromShip) { readyForDataFromShip = true; return false; }
56 if (instruction.tokenIn) triggersReceived--;
59 if (instruction.dataIn) {
60 haveDataFromShip = false;
61 if (instruction.latch)
62 register = itemPresentedByShip;
65 if (instruction.dataOut) {
67 // if item to be transmitted, send it
68 InterpreterDestination dest = (InterpreterDestination)instruction.dest;
69 if (instruction.dataOutDest) {
70 // FIXME: still not supported
71 long bits = BitManipulations.getField(InstructionEncoder.OFFSET_PUMP_ADDR+InstructionEncoder.WIDTH_PUMP_ADDR-1,
72 InstructionEncoder.OFFSET_PUMP_ADDR,
74 getInterpreter().dispatch(((Interpreter)getInterpreter()).iie.readInstruction(register), bits);
76 dest = (InterpreterDestination)(((Interpreter)getInterpreter()).iie.getDestByAddr(bits));
80 Pump pump = ((Interpreter)getInterpreter()).iie.getDestByInstAddr(bits);
84 //throw new RuntimeException();
86 new Packet(getInterpreter(), this, register, dest).send();
88 if (instruction.tokenOut)
89 throw new RuntimeException("outboxes may not send acks!");
91 } else if (instruction.tokenOut) {
93 // if no item was sent, we might still send an ack
94 new Packet(getInterpreter(), this, 0, (InterpreterDestination)instruction.dest).send();
100 public final boolean readyForDataFromShip() { return readyForDataFromShip; }
101 public void addDataFromShip(long data) { addItemFromShip(data); }
102 public void addDataFromFabric(Packet packet) { triggersReceived++; }
104 /** subclass invokes this to add an item from the ship */
105 protected final void addItemFromShip(long data) {
106 if (!readyForDataFromShip)
107 throw new RuntimeException("tried to add an item to an outbox which was not ready! you have a buggy ship!");
108 readyForDataFromShip = false;
109 haveDataFromShip = true;
110 itemPresentedByShip = data;
114 if (haveDataFromShip)
115 Log.println(ANSI.red(" WARNING: you left a value ("+itemPresentedByShip+") on outbox port " + this));
116 if (triggersReceived > 0)
117 Log.println(ANSI.red(" WARNING: you left a token on the trigger input to port " + this));
118 super.shutdown(false);