added am10 inbox/outbox support
[fleet.git] / src / edu / berkeley / fleet / OutboxPort.java
1 package edu.berkeley.fleet;
2 import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
3
4 public class OutboxPort extends InstructionPort {
5
6     public OutboxPort(Ship ship, String name) {
7         super(ship, name);
8     }
9
10     // data in from the ship
11     private boolean empty = true;
12     private int     dataIn;
13
14     public boolean empty() { return empty; }
15
16     /** number of tokens queued on the trigger input */
17     private int trigger = 0;
18     public void addToken() { trigger++; }
19
20     public void addData(int data) {
21         if (!empty)
22             throw new RuntimeException("tried to add data to a non-empty outbox!  you have a buggy ship!");
23         empty = false;
24         dataIn = data;
25     }
26
27     public boolean service(Instruction instruction) {
28
29         // if no instruction waiting, do nothing
30         if (instruction == null) return false;
31
32         // check firing conditions
33         if (instruction.trigger && trigger <= 0) return false;
34         if (instruction.dataIn != IGNORE && empty) return false;
35         
36         // consume inbound data+token
37         if (instruction.trigger) trigger--;
38         int data = 0;
39         switch(instruction.dataIn) {
40             case COPY:
41                 data = dataIn;
42                 break;
43             case TAKE:
44                 data = dataIn;
45                 empty = true;
46                 break;
47         }
48
49         if (instruction.dataOut) {
50             Port port = instruction.destination.resolve(getShip().getFleet());
51             Log.data(data+"", this, port);
52             port.addData(data);
53         }
54         if (instruction.ack)
55             throw new RuntimeException("outboxes may not send acks!");
56
57         return true;
58     }
59 }