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.WireArrayInput;
12 * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which,
13 * as determined by select, is put through to the output.
14 * @author Fabian Stemmler
17 public class Mux extends BasicComponent
19 private WireArray select;
20 private WireArrayInput outI;
21 private WireArray[] inputs;
22 private final int outputSize;
24 * Input {@link WireArray}s and out must be of uniform length
25 * @param out Must be of uniform length with all inputs.
26 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits
27 * to index all inputs.
28 * @param inputs One of these inputs is mapped to the output, depending on the select bits
30 public Mux(int processTime, WireArray out, WireArray select, WireArray... inputs)
33 outputSize = out.length;
35 this.inputs = inputs.clone();
36 for(int i = 0; i < this.inputs.length; i++)
38 if(inputs[i].length != outputSize)
39 throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
40 inputs[i].addObserver(this);
44 select.addObserver(this);
46 int maxInputs = 1 << select.length;
47 if(this.inputs.length > maxInputs)
48 throw new IllegalArgumentException("There are more inputs ("
49 + this.inputs.length + ") to the MUX than supported by "
50 + select.length + " select bits (" + maxInputs + ").");
52 outI = out.createInput();
55 public WireArray getOut()
60 public WireArray getSelect()
66 public void compute() {
68 if(!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
74 WireArray active = inputs[selectValue];
75 outI.feedSignals(active.getValues());
79 public List<WireArray> getAllInputs()
81 ArrayList<WireArray> wires = new ArrayList<WireArray>(Arrays.asList(inputs));
83 return Collections.unmodifiableList(wires);
87 public List<WireArray> getAllOutputs()
89 return Collections.unmodifiableList(Arrays.asList(outI.owner));