The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.core / src / net / mograsim / logic / core / components / CoreUnidirectionalMerger.java
1 package net.mograsim.logic.core.components;
2
3 import java.util.List;
4
5 import net.mograsim.logic.core.LogicObservable;
6 import net.mograsim.logic.core.LogicObserver;
7 import net.mograsim.logic.core.timeline.Timeline;
8 import net.mograsim.logic.core.wires.CoreWire;
9 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
10 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
11
12 public class CoreUnidirectionalMerger extends CoreComponent implements LogicObserver
13 {
14         private ReadWriteEnd out;
15         private ReadEnd[] inputs;
16         private int[] beginningIndex;
17
18         /**
19          * 
20          * @param union  The output of merging n {@link CoreWire}s into one. Must have width = a1.width() + a2.width() + ... + an.width().
21          * @param inputs The inputs to be merged into the union
22          */
23         public CoreUnidirectionalMerger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs)
24         {
25                 super(timeline);
26                 this.inputs = inputs;
27                 this.out = union;
28                 this.beginningIndex = new int[inputs.length];
29
30                 int width = 0;
31                 for (int i = 0; i < inputs.length; i++)
32                 {
33                         beginningIndex[i] = width;
34                         width += inputs[i].width();
35                         inputs[i].registerObserver(this);
36                 }
37
38                 if (width != union.width())
39                         throw new IllegalArgumentException(
40                                         "The output of merging n WireArrays into one must have width = a1.width() + a2.width() + ... + an.width().");
41         }
42
43         public ReadEnd getInput(int index)
44         {
45                 return inputs[index];
46         }
47
48         public ReadEnd getUnion()
49         {
50                 return out;
51         }
52
53         @Override
54         public void update(LogicObservable initiator)
55         {
56                 int index = find(initiator);
57                 int beginning = beginningIndex[index];
58                 out.feedSignals(beginning, inputs[index].getValues());
59         }
60
61         private int find(LogicObservable r)
62         {
63                 for (int i = 0; i < inputs.length; i++)
64                         if (inputs[i] == r)
65                                 return i;
66                 return -1;
67         }
68
69         public ReadEnd[] getInputs()
70         {
71                 return inputs.clone();
72         }
73
74         @Override
75         public List<ReadEnd> getAllInputs()
76         {
77                 return List.of(inputs);
78         }
79
80         @Override
81         public List<ReadWriteEnd> getAllOutputs()
82         {
83                 return List.of(out);
84         }
85 }