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.ReadEnd;
10 import era.mi.logic.wires.Wire.ReadWriteEnd;
13 * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
16 * @author Fabian Stemmler
19 public class Mux extends BasicComponent
21 private ReadEnd select;
22 private ReadWriteEnd out;
23 private ReadEnd[] inputs;
24 private final int outputSize;
27 * Input {@link Wire}s and out must be of uniform length
29 * @param out Must be of uniform length with all inputs.
30 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
31 * @param inputs One of these inputs is mapped to the output, depending on the select bits
33 public Mux(int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs)
36 outputSize = out.length();
38 this.inputs = inputs.clone();
39 for (int i = 0; i < this.inputs.length; i++)
41 if (inputs[i].length() != outputSize)
42 throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
43 inputs[i].addObserver(this);
47 select.addObserver(this);
49 int maxInputs = 1 << select.length();
50 if (this.inputs.length > maxInputs)
51 throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "
52 + select.length() + " select bits (" + maxInputs + ").");
57 public ReadEnd getOut()
62 public ReadEnd getSelect()
71 if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
77 ReadEnd active = inputs[selectValue];
78 out.feedSignals(active.getValues());
82 public List<ReadEnd> getAllInputs()
84 ArrayList<ReadEnd> wires = new ArrayList<ReadEnd>(Arrays.asList(inputs));
86 return Collections.unmodifiableList(wires);
90 public List<ReadWriteEnd> getAllOutputs()