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