1 package era.mi.logic.components;
3 import java.util.Arrays;
4 import java.util.Collections;
7 import era.mi.logic.wires.Wire;
8 import era.mi.logic.wires.Wire.WireEnd;
11 * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
14 * @author Fabian Stemmler
17 public class Demux extends BasicComponent
19 private final WireEnd select, in;
20 private final WireEnd[] outputs;
21 private final int outputSize;
22 private int selected = -1;
25 * Input {@link Wire}s and out must be of uniform length
27 * @param out Must be of uniform length with all inputs.
28 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
29 * @param outputs One of these inputs is mapped to the output, depending on the select bits
31 public Demux(int processTime, WireEnd in, WireEnd select, WireEnd... outputs)
34 outputSize = in.length();
37 this.outputs = outputs;
38 for (int i = 0; i < this.outputs.length; i++)
40 if (outputs[i].length() != outputSize)
41 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
42 this.outputs[i] = outputs[i];
46 select.addObserver(this);
48 int maxInputs = 1 << select.length();
49 if (this.outputs.length > maxInputs)
50 throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "
51 + select.length() + " select bits (" + maxInputs + ").");
58 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
59 if (selectValue >= outputs.length)
62 if (selected != selectValue && selected != -1)
63 outputs[selected].clearSignals();
65 selected = selectValue;
67 if (selectValue != -1)
68 outputs[selectValue].feedSignals(in.getValues());
72 public List<WireEnd> getAllInputs()
74 return Collections.unmodifiableList(Arrays.asList(in, select));
78 public List<WireEnd> getAllOutputs()
80 return Collections.unmodifiableList(Arrays.asList(outputs));