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<WireArray> getAllInputs() {
+ return Collections.unmodifiableList(Arrays.asList(inputs));
}
-
+
@Override
- public void update(WireArray initiator)
- {
- compute(); //No inner delay
+ public List<WireArray> getAllOutputs() {
+ return Collections.unmodifiableList(Arrays.asList(outI.owner));
}
}