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