X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMerger.java;h=1efcf8ad276cf0d6d4922ed92bda4a466af458fc;hb=74aebd92f41d03f4a44c9a455ef8c05465136412;hp=eb910a4f87caf0e79a8d71eafcd4c7305a4fdf16;hpb=a4c5cfb856026771dfcf31eb22434b8b6ff20ad4;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..1efcf8ad 100644 --- a/era.mi/src/era/mi/logic/components/Merger.java +++ b/era.mi/src/era/mi/logic/components/Merger.java @@ -1,61 +1,74 @@ package era.mi.logic.components; -import era.mi.logic.Util; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + 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; +public class Merger implements WireArrayObserver, Component { + private WireArrayInput outI; private WireArray[] inputs; - - //TODO: General problem with this concept; New inputs coming in at the same time override each other - + 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 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) - { + public Merger(WireArray union, WireArray... inputs) { this.inputs = inputs; - this.out = union; - + this.outI = union.createInput(); + this.beginningIndex = new int[inputs.length]; + int length = 0; - for(WireArray input : inputs) - { - length += input.length(); - input.addObserver(this); + 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()."); - } - 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); + 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) - { + public WireArray getInput(int index) { return inputs[index]; } - - public WireArray getUnion() - { - return out; + + public WireArray getUnion() { + return outI.owner; + } + + @Override + public void update(WireArray initiator, Bit[] oldValues) { + 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(); + } + + @Override + public List getAllInputs() { + return Collections.unmodifiableList(Arrays.asList(inputs)); } - + @Override - public void update(WireArray initiator) - { - compute(); //No inner delay + public List getAllOutputs() { + return Collections.unmodifiableList(Arrays.asList(outI.owner)); } }