X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2FMerger.java;h=06e064c93663efd5a4aabd30e425a98a08eb7a08;hb=ecc651406020231ff0ebc8d1bfc6f916693f69c6;hp=c4c7170144f4217f5d805de7e1efbf5a261fb6f2;hpb=67c1d352795802dae0c045cedeed82c883819d4e;p=Mograsim.git diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java index c4c71701..06e064c9 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java @@ -1,85 +1,85 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.Wire; -import net.mograsim.logic.core.wires.WireObserver; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public class Merger extends Component implements WireObserver -{ - private ReadWriteEnd out; - private ReadEnd[] inputs; - private int[] beginningIndex; - - /** - * - * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length(). - * @param inputs The inputs to be merged into the union - */ - public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs) - { - super(timeline); - this.inputs = inputs; - this.out = union; - this.beginningIndex = new int[inputs.length]; - - int length = 0; - for (int i = 0; i < inputs.length; i++) - { - beginningIndex[i] = length; - length += inputs[i].length(); - inputs[i].addObserver(this); - } - - if (length != union.length()) - throw new IllegalArgumentException( - "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length()."); - } - - public ReadEnd getInput(int index) - { - return inputs[index]; - } - - public ReadEnd getUnion() - { - return out; - } - - @Override - public void update(ReadEnd initiator, BitVector oldValues) - { - int index = find(initiator); - int beginning = beginningIndex[index]; - out.feedSignals(beginning, inputs[index].getValues()); - } - - private int find(ReadEnd r) - { - for (int i = 0; i < inputs.length; i++) - if (inputs[i] == r) - return i; - return -1; - } - - public ReadEnd[] getInputs() - { - return inputs.clone(); - } - - @Override - public List getAllInputs() - { - return List.of(inputs); - } - - @Override - public List getAllOutputs() - { - return List.of(out); - } -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class Merger extends Component implements LogicObserver +{ + private ReadWriteEnd out; + private ReadEnd[] inputs; + private int[] beginningIndex; + + /** + * + * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length(). + * @param inputs The inputs to be merged into the union + */ + public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs) + { + super(timeline); + this.inputs = inputs; + this.out = union; + this.beginningIndex = new int[inputs.length]; + + int length = 0; + for (int i = 0; i < inputs.length; i++) + { + beginningIndex[i] = length; + length += inputs[i].length(); + inputs[i].registerObserver(this); + } + + if (length != union.length()) + throw new IllegalArgumentException( + "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length()."); + } + + public ReadEnd getInput(int index) + { + return inputs[index]; + } + + public ReadEnd getUnion() + { + return out; + } + + @Override + public void update(LogicObservable initiator) + { + int index = find(initiator); + int beginning = beginningIndex[index]; + out.feedSignals(beginning, inputs[index].getValues()); + } + + private int find(LogicObservable r) + { + for (int i = 0; i < inputs.length; i++) + if (inputs[i] == r) + return i; + return -1; + } + + public ReadEnd[] getInputs() + { + return inputs.clone(); + } + + @Override + public List getAllInputs() + { + return List.of(inputs); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } +}