The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / 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.timeline.TimelineEventHandler;
7 import net.mograsim.logic.core.types.Bit;
8 import net.mograsim.logic.core.types.BitVector;
9 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
10 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
11
12 public class CoreTriStateBuffer extends BasicCoreComponent
13 {
14         ReadEnd in, enable;
15         ReadWriteEnd out;
16
17         public CoreTriStateBuffer(Timeline timeline, int processTime, ReadEnd in, ReadWriteEnd out, ReadEnd enable)
18         {
19                 super(timeline, processTime);
20                 if (in.width() != out.width())
21                         throw new IllegalArgumentException(
22                                         "Tri-state output must have the same amount of bits as the input. Input: " + in.width() + " Output: " + out.width());
23                 if (enable.width() != 1)
24                         throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.width() + ".");
25                 this.in = in;
26                 in.registerObserver(this);
27                 this.enable = enable;
28                 enable.registerObserver(this);
29                 this.out = out;
30         }
31
32         @Override
33         protected TimelineEventHandler compute()
34         {
35                 if (enable.getValue() == Bit.ONE)
36                 {
37                         BitVector inValues = in.getValues();
38                         return e -> out.feedSignals(inValues);
39                 }
40                 return e -> out.clearSignals();
41         }
42
43         @Override
44         public List<ReadEnd> getAllInputs()
45         {
46                 return List.of(in, enable);
47         }
48
49         @Override
50         public List<ReadWriteEnd> getAllOutputs()
51         {
52                 return List.of(out);
53         }
54
55 }