5c02a4e347b1ba984a67d6c0788943d9ee09610f
[Mograsim.git] / era.mi / src / era / mi / logic / components / TriStateBuffer.java
1 package era.mi.logic.components;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 import era.mi.logic.Bit;
8 import era.mi.logic.wires.WireArray;
9 import era.mi.logic.wires.WireArray.WireArrayEnd;
10
11 public class TriStateBuffer extends BasicComponent
12 {
13         WireArray in, enable;
14         WireArrayEnd outI;
15
16         public TriStateBuffer(int processTime, WireArray in, WireArray out, WireArray enable)
17         {
18                 super(processTime);
19                 if (in.length != out.length)
20                         throw new IllegalArgumentException(
21                                         "Tri-state output must have the same amount of bits as the input. Input: " + in.length + " Output: " + out.length);
22                 if (enable.length != 1)
23                         throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length + ".");
24                 this.in = in;
25                 in.addObserver(this);
26                 this.enable = enable;
27                 enable.addObserver(this);
28                 outI = out.createInput();
29         }
30
31         @Override
32         protected void compute()
33         {
34                 if (enable.getValue() == Bit.ONE)
35                         outI.feedSignals(in.getValues());
36                 else
37                         outI.clearSignals();
38         }
39
40         @Override
41         public List<WireArray> getAllInputs()
42         {
43                 return Collections.unmodifiableList(Arrays.asList(in, enable));
44         }
45
46         @Override
47         public List<WireArray> getAllOutputs()
48         {
49                 return Collections.unmodifiableList(Arrays.asList(outI.owner));
50         }
51
52 }