322c9dc587332a4ad48649cf885ece15588efc59
[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 final Destination[] ports;
12         
13     public Iterable<Destination> getDestinations() {
14         HashSet<Destination> ret = new HashSet<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] =
52                 ports[i].equals("")
53                 ? this
54                 : new VirtualPort(ports[i]);
55         ship.addBenkoBox(name, this);
56     }
57
58
59     public class VirtualPort extends Destination {
60         public String name;
61         public VirtualPort(String name) { this.name = name; }
62         public String getDestinationName() { return name; }
63         public Ship getShip() { return SlipwayBenkoBox.this.getShip(); }
64         public void addDataFromFabric(long data) { SlipwayBenkoBox.this.addDataFromFabric(name, (int)data); }
65         public String toString() { return getShip()+"."+getName(); }
66         // fixme
67         public long addr = (default_addr++);
68     }
69         
70     /** adds one token to the port from the switch fabric side */
71     void addTokenFromFabric() { addDataFromFabric(0); }
72         
73     /** adds the included datum to the port from the switch fabric  side */
74     void addDataFromFabric(int datum)  { throw new RuntimeException("this should never happen!"); }
75     void addDataFromFabric(String name, int datum)  {
76         addDataFromFabric(datum);   
77     }
78
79     /** adds one token to the port from the ship side */
80     public void addTokenFromShip() { addDataFromShip(0); }
81         
82     /** adds the included datum to the port from the switch fabric side */
83     public void addDataFromShip(int datum) { throw new RuntimeException("this should never happen!"); }
84
85     public Ship getShip() { return ship; }
86
87     public Fleet getFleet() { return getShip().getFleet(); }
88
89     public String toString() { return ship+"."+name; }
90
91     public String getName() { return name; }
92
93     public int getInstructionFifoLength() { return 4; }
94
95 }