added am10 inbox/outbox support
[fleet.git] / src / edu / berkeley / fleet / Instruction.java
1 package edu.berkeley.fleet;
2
3 /** an instruction within a codebag, as specified in am10 */
4 public class Instruction extends Dispatchable {
5
6     public final PortReference source;
7     public final PortReference destination;
8     public final int count;
9     public final IgnoreCopyTake dataIn;
10     public final boolean ack;
11     public final boolean trigger;
12     public final boolean dataOut;
13
14     public static enum IgnoreCopyTake { IGNORE, COPY, TAKE };
15
16     public Instruction(PortReference source,
17                        PortReference destination,
18                        int count,
19                        IgnoreCopyTake dataIn,
20                        boolean ack,
21                        boolean trigger,
22                        boolean dataOut) {
23         this.source = source;
24         this.destination = destination;
25         this.count = count;
26         this.dataIn = dataIn;
27         this.ack = ack;
28         this.trigger = trigger;
29         this.dataOut = dataOut;
30         if (count <= 0)
31             throw new RuntimeException("count field of an instruction must be >0");
32     }
33
34     public void dispatch(Fleet fleet) {
35         Port sourcePort = source.resolve(fleet);
36         if (!(sourcePort instanceof InstructionPort))
37             throw new RuntimeException(sourcePort + " is not an InstructionPort!");
38         ((InstructionPort)sourcePort).addInstruction(this);
39     }
40
41     public String toString() {
42         StringBuffer ret = new StringBuffer();
43         if (trigger) ret.append("triggered ");
44         switch(dataIn) {
45             case IGNORE:
46                 ret.append("nop");
47                 break;
48             case COPY:
49                 ret.append(dataOut ? "copy" : "wait");
50                 break;
51             case TAKE:
52                 ret.append(dataOut ? "move" : "discard");
53                 break;
54         }
55         if (ack) ret.append("+ack");
56         ret.append(" ");
57         ret.append(source);
58         ret.append(" ");
59         switch(count) {
60             case 1:                 ret.append("->"); break;
61             case Integer.MAX_VALUE: ret.append("-[*]->"); break;
62             default:                ret.append("-["+count+"]->"); break;
63         }
64         ret.append(" ");
65         ret.append(destination);
66         return ret.toString();
67     }
68 }