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