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