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