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