X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMerger2.java;fp=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMerger2.java;h=ca242544891d895517a7e8d9899e452198f2d613;hb=a4c5cfb856026771dfcf31eb22434b8b6ff20ad4;hp=0000000000000000000000000000000000000000;hpb=7199371e42ba04d2daab9f8512fdf25bd5f5ff92;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/components/Merger2.java b/era.mi/src/era/mi/logic/components/Merger2.java new file mode 100644 index 00000000..ca242544 --- /dev/null +++ b/era.mi/src/era/mi/logic/components/Merger2.java @@ -0,0 +1,70 @@ +package era.mi.logic.components; + +import era.mi.logic.WireArray; +import era.mi.logic.WireArrayObserver; + +public class Merger2 implements WireArrayObserver +{ + private WireArray out; + 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 Merger2(WireArray union, WireArray... 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 WireArray getInput(int index) + { + return inputs[index]; + } + + public WireArray getUnion() + { + return out; + } + + @Override + public void update(WireArray initiator) + { + int index = find(initiator); + int beginning = beginningIndex[index]; + out.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 getOut() + { + return out; + } + + public WireArray[] getInputs() + { + return inputs.clone(); + } +}