a43a65914d3f2260aa5d11360f0afa9205e3b348
[fleet.git] / src / edu / berkeley / fleet / slipway / SlipwayBenkoBox.java
1 package edu.berkeley.fleet.slipway;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.api.BenkoBox;
4 import java.util.*;
5
6 /** anything that has a destination address on the switch fabric */
7 public class SlipwayBenkoBox extends BenkoBox {
8         
9     private final String name;
10     private final SlipwayShip ship;
11     private  Destination[] ports;
12         
13     public Iterable<Destination> getDestinations() {
14         ArrayList<Destination> ret = new ArrayList<Destination>();
15         for(Destination d : ports) ret.add(d);
16         return ret;
17     }
18     private static int default_addr;
19     private static int default_instr_addr;
20
21     public int bits;
22     public int addr = (default_addr++);
23     public int instr_bits;
24     public int instr_addr = (default_instr_addr++);
25     protected boolean special = false;
26     protected boolean ihorn = false;
27     protected boolean dhorn = false;
28     void service() { }
29     public boolean special() { return special; }
30     public boolean ihorn() { return ihorn; }
31     public boolean dhorn() { return dhorn; }
32     public boolean inbox;
33     public boolean isInbox() { return inbox; }
34     public boolean isOutbox() { return !inbox; }
35     public SlipwayBenkoBox(boolean inbox, SlipwayShip ship, String name) {
36         this(inbox, ship, name, false, false, false);
37     }
38     public SlipwayBenkoBox(boolean inbox, SlipwayShip ship, String name, boolean special) {
39         this(inbox, ship, name, special, false, false);
40     }
41     public SlipwayBenkoBox(boolean inbox, SlipwayShip ship, String name, boolean special, boolean ihorn, boolean dhorn) {
42         this.inbox = inbox;
43         this.special = special;
44         this.dhorn = dhorn;
45         this.ihorn = ihorn;
46         this.ship = ship;
47         this.name = name;
48         String[] ports = new String[] { "" };
49         this.ports = new Destination[ports.length];
50         for(int i=0; i<ports.length; i++)
51             this.ports[i] = new VirtualPort(ports[i]);
52         ship.addBenkoBox(name, this);
53     }
54
55     public void addDestination(String dest) {
56         Destination[] newports = new Destination[ports.length+1];
57         System.arraycopy(ports, 0, newports, 0, ports.length);
58         newports[newports.length-1] = new VirtualPort(dest);
59         ports = newports;
60     }
61
62     public class VirtualPort extends Destination {
63         public String name;
64         public VirtualPort(String name) { this.name = name; }
65         public String getDestinationName() { return name; }
66         public Ship getShip() { return SlipwayBenkoBox.this.getShip(); }
67         public void addDataFromFabric(long data) { SlipwayBenkoBox.this.addDataFromFabric(name, (int)data); }
68         public String toString() { return getShip()+"."+getName(); }
69         // fixme
70         public long addr = (default_addr++);
71     }
72         
73     /** adds one token to the port from the switch fabric side */
74     void addTokenFromFabric() { addDataFromFabric(0); }
75         
76     /** adds the included datum to the port from the switch fabric  side */
77     void addDataFromFabric(int datum)  { throw new RuntimeException("this should never happen!"); }
78     void addDataFromFabric(String name, int datum)  {
79         addDataFromFabric(datum);   
80     }
81
82     /** adds one token to the port from the ship side */
83     public void addTokenFromShip() { addDataFromShip(0); }
84         
85     /** adds the included datum to the port from the switch fabric side */
86     public void addDataFromShip(int datum) { throw new RuntimeException("this should never happen!"); }
87
88     public Ship getShip() { return ship; }
89
90     public Fleet getFleet() { return getShip().getFleet(); }
91
92     public String toString() { return ship+"."+name; }
93
94     public String getName() { return name; }
95
96     public int getInstructionFifoLength() { return 4; }
97
98 }