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.Wire;
9 import era.mi.logic.wires.Wire.WireEnd;
12 * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}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 WireEnd select;
22 private WireEnd[] inputs;
23 private final int outputSize;
26 * Input {@link Wire}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, WireEnd out, WireEnd select, WireEnd... 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 + ").");
56 public WireEnd getOut()
61 public WireEnd getSelect()
70 if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
76 WireEnd active = inputs[selectValue];
77 out.feedSignals(active.getValues());
81 public List<WireEnd> getAllInputs()
83 ArrayList<WireEnd> wires = new ArrayList<WireEnd>(Arrays.asList(inputs));
85 return Collections.unmodifiableList(wires);
89 public List<WireEnd> getAllOutputs()