1 package era.mi.logic.components;
\r
3 import java.util.Arrays;
\r
4 import java.util.Collections;
\r
5 import java.util.List;
\r
7 import era.mi.logic.wires.WireArray;
\r
8 import era.mi.logic.wires.WireArray.WireArrayInput;
\r
11 * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the
\r
14 * @author Fabian Stemmler
\r
17 public class Demux extends BasicComponent {
\r
18 private final WireArray select, in;
\r
19 private final WireArray[] outputs;
\r
20 private final WireArrayInput[] outputsI;
\r
21 private final int outputSize;
\r
22 private int selected = -1;
\r
25 * Input {@link WireArray}s and out must be of uniform length
\r
27 * @param out Must be of uniform length with all inputs.
\r
28 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
\r
29 * @param outputs One of these inputs is mapped to the output, depending on the select bits
\r
31 public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs) {
\r
33 outputSize = in.length;
\r
36 this.outputs = outputs;
\r
37 this.outputsI = new WireArrayInput[outputs.length];
\r
38 for (int i = 0; i < this.outputsI.length; i++) {
\r
39 if (outputs[i].length != outputSize)
\r
40 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
\r
41 this.outputsI[i] = outputs[i].createInput();
\r
44 this.select = select;
\r
45 select.addObserver(this);
\r
47 int maxInputs = 1 << select.length;
\r
48 if (this.outputsI.length > maxInputs)
\r
49 throw new IllegalArgumentException("There are more outputs (" + this.outputsI.length + ") to the DEMUX than supported by "
\r
50 + select.length + " select bits (" + maxInputs + ").");
\r
51 in.addObserver(this);
\r
55 public void compute() {
\r
56 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
\r
57 if (selectValue >= outputsI.length)
\r
60 if (selected != selectValue && selected != -1)
\r
61 outputsI[selected].clearSignals();
\r
63 selected = selectValue;
\r
65 if (selectValue != -1)
\r
66 outputsI[selectValue].feedSignals(in.getValues());
\r
70 public List<WireArray> getAllInputs() {
\r
71 return Collections.unmodifiableList(Arrays.asList(in, select));
\r
75 public List<WireArray> getAllOutputs() {
\r
76 return Collections.unmodifiableList(Arrays.asList(outputs));
\r