X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMerger.java;h=c5d8fdbaa87ef2c2690e1b2831b181a63235194f;hb=daa558fbbd87e334d370f1a9d791adc6a3c14ca6;hp=534b2ca6b5a966a2e2b00d123020bc2fa7fe6c18;hpb=c7f37223215b5f0b6de2119b84018cca41b8f7f9;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/components/Merger.java b/era.mi/src/era/mi/logic/components/Merger.java index 534b2ca6..c5d8fdba 100644 --- a/era.mi/src/era/mi/logic/components/Merger.java +++ b/era.mi/src/era/mi/logic/components/Merger.java @@ -1,68 +1,85 @@ -package era.mi.logic.components; - -import era.mi.logic.wires.WireArray; -import era.mi.logic.wires.WireArray.WireArrayInput; -import era.mi.logic.wires.WireArrayObserver; - -public class Merger implements WireArrayObserver -{ - private WireArrayInput outI; - private WireArray[] inputs; - private int[] beginningIndex; - - /** - * - * @param union The output of merging n {@link WireArray}s into one. Must have - * length = a1.length() + a2.length() + ... + an.length(). - * @param inputs The inputs to be merged into the union - */ - public Merger(WireArray union, WireArray... inputs) - { - this.inputs = inputs; - this.outI = union.createInput(); - 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 WireArray getInput(int index) - { - return inputs[index]; - } - - public WireArray getUnion() - { - return outI.owner; - } - - @Override - public void update(WireArray initiator) - { - int index = find(initiator); - int beginning = beginningIndex[index]; - outI.feedSignals(beginning, initiator.getValues()); - } - - private int find(WireArray w) - { - for (int i = 0; i < inputs.length; i++) - if (inputs[i] == w) - return i; - return -1; - } - - public WireArray[] getInputs() - { - return inputs.clone(); - } -} +package era.mi.logic.components; + +import java.util.List; + +import era.mi.logic.timeline.Timeline; +import era.mi.logic.types.BitVector; +import era.mi.logic.wires.Wire; +import era.mi.logic.wires.Wire.ReadEnd; +import era.mi.logic.wires.Wire.ReadWriteEnd; +import era.mi.logic.wires.WireObserver; + +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); + } +}