1 package era.mi.logic.components;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
8 import era.mi.logic.wires.WireArray;
9 import era.mi.logic.wires.WireArray.WireArrayEnd;
12 * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the
15 * @author Fabian Stemmler
18 public class Mux extends BasicComponent
20 private WireArray select;
21 private WireArrayEnd outI;
22 private WireArray[] inputs;
23 private final int outputSize;
26 * Input {@link WireArray}s and out must be of uniform length
28 * @param out Must be of uniform length with all inputs.
29 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
30 * @param inputs One of these inputs is mapped to the output, depending on the select bits
32 public Mux(int processTime, WireArray out, WireArray select, WireArray... inputs)
35 outputSize = out.length;
37 this.inputs = inputs.clone();
38 for (int i = 0; i < this.inputs.length; i++)
40 if (inputs[i].length != outputSize)
41 throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
42 inputs[i].addObserver(this);
46 select.addObserver(this);
48 int maxInputs = 1 << select.length;
49 if (this.inputs.length > maxInputs)
50 throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "
51 + select.length + " select bits (" + maxInputs + ").");
53 outI = out.createInput();
56 public WireArray getOut()
61 public WireArray getSelect()
70 if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
76 WireArray active = inputs[selectValue];
77 outI.feedSignals(active.getValues());
81 public List<WireArray> getAllInputs()
83 ArrayList<WireArray> wires = new ArrayList<WireArray>(Arrays.asList(inputs));
85 return Collections.unmodifiableList(wires);
89 public List<WireArray> getAllOutputs()
91 return Collections.unmodifiableList(Arrays.asList(outI.owner));