X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMerger.java;h=534b2ca6b5a966a2e2b00d123020bc2fa7fe6c18;hb=49f569b513c36e8ad421fd5a547bf34bd830652a;hp=eb910a4f87caf0e79a8d71eafcd4c7305a4fdf16;hpb=670a82cafe6435b2cdaf02e86701fedd14970c1c;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..534b2ca6 100644 --- a/era.mi/src/era/mi/logic/components/Merger.java +++ b/era.mi/src/era/mi/logic/components/Merger.java @@ -1,61 +1,68 @@ 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; +import era.mi.logic.wires.WireArray; +import era.mi.logic.wires.WireArray.WireArrayInput; +import era.mi.logic.wires.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()."); - } + private WireArrayInput outI; + private WireArray[] inputs; + private int[] beginningIndex; - 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); - } + /** + * + * @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]; - public WireArray getInput(int index) - { - return inputs[index]; - } - - public WireArray getUnion() - { - return out; - } - - @Override - public void update(WireArray initiator) + int length = 0; + for (int i = 0; i < inputs.length; i++) { - compute(); //No inner delay + 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(); + } }