1 package net.mograsim.logic.core.components;
\r
3 import java.util.List;
\r
5 import net.mograsim.logic.core.LogicObservable;
\r
6 import net.mograsim.logic.core.LogicObserver;
\r
7 import net.mograsim.logic.core.timeline.Timeline;
\r
8 import net.mograsim.logic.core.wires.Wire;
\r
9 import net.mograsim.logic.core.wires.Wire.ReadEnd;
\r
10 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
\r
12 public class Merger extends Component implements LogicObserver
\r
14 private ReadWriteEnd out;
\r
15 private ReadEnd[] inputs;
\r
16 private int[] beginningIndex;
\r
20 * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
\r
21 * @param inputs The inputs to be merged into the union
\r
23 public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs)
\r
26 this.inputs = inputs;
\r
28 this.beginningIndex = new int[inputs.length];
\r
31 for (int i = 0; i < inputs.length; i++)
\r
33 beginningIndex[i] = length;
\r
34 length += inputs[i].length();
\r
35 inputs[i].registerObserver(this);
\r
38 if (length != union.length())
\r
39 throw new IllegalArgumentException(
\r
40 "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
\r
43 public ReadEnd getInput(int index)
\r
45 return inputs[index];
\r
48 public ReadEnd getUnion()
\r
54 public void update(LogicObservable initiator)
\r
56 int index = find(initiator);
\r
57 int beginning = beginningIndex[index];
\r
58 out.feedSignals(beginning, inputs[index].getValues());
\r
61 private int find(LogicObservable r)
\r
63 for (int i = 0; i < inputs.length; i++)
\r
69 public ReadEnd[] getInputs()
\r
71 return inputs.clone();
\r
75 public List<ReadEnd> getAllInputs()
\r
77 return List.of(inputs);
\r
81 public List<ReadWriteEnd> getAllOutputs()
\r
83 return List.of(out);
\r