X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMerger.java;h=32321597de835b11d3e4be82cb9884f193a25b5c;hb=70afb26181aba8de30ea6f796ea5c2a573d9ecab;hp=eb910a4f87caf0e79a8d71eafcd4c7305a4fdf16;hpb=7f0a08228f5c517aa1aa22453c1b0dd533e4cd04;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 eb910a4f..32321597 100644 --- a/era.mi/src/era/mi/logic/components/Merger.java +++ b/era.mi/src/era/mi/logic/components/Merger.java @@ -1,61 +1,82 @@ -package era.mi.logic.components; - -import era.mi.logic.Util; -import era.mi.logic.Bit; -import era.mi.logic.WireArray; -import era.mi.logic.WireArrayObserver; - -@Deprecated -public class Merger implements WireArrayObserver -{ - private WireArray out; - private WireArray[] inputs; - - //TODO: General problem with this concept; New inputs coming in at the same time override each other - - /** - * - * @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.out = union; - - int length = 0; - for(WireArray input : inputs) - { - length += input.length(); - input.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()."); - } - - protected void compute() - { - Bit[][] bits = new Bit[inputs.length][]; - for(int i = 0; i < inputs.length; i++) - bits[i] = inputs[i].getValues(); - Bit[] newOut = Util.concat(bits); - out.feedSignals(newOut); - } - - public WireArray getInput(int index) - { - return inputs[index]; - } - - public WireArray getUnion() - { - return out; - } - - @Override - public void update(WireArray initiator) - { - compute(); //No inner delay - } -} +package era.mi.logic.components; + +import java.util.List; + +import era.mi.logic.types.BitVector; +import era.mi.logic.wires.Wire; +import era.mi.logic.wires.Wire.WireEnd; +import era.mi.logic.wires.WireObserver; + +public class Merger implements WireObserver, Component +{ + private WireEnd out; + private WireEnd[] 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(WireEnd union, WireEnd... inputs) + { + 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 WireEnd getInput(int index) + { + return inputs[index]; + } + + public WireEnd getUnion() + { + return out; + } + + @Override + public void update(Wire initiator, BitVector oldValues) + { + int index = find(initiator); + int beginning = beginningIndex[index]; + out.feedSignals(beginning, inputs[index].getValues()); + } + + private int find(Wire w) + { + for (int i = 0; i < inputs.length; i++) + if (inputs[i].getWire() == w) + return i; + return -1; + } + + public WireEnd[] getInputs() + { + return inputs.clone(); + } + + @Override + public List getAllInputs() + { + return List.of(inputs); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } +}