1 package era.mi.logic.components;
5 import era.mi.logic.wires.Wire;
6 import era.mi.logic.wires.Wire.WireEnd;
9 * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
12 * @author Fabian Stemmler
15 public class Demux extends BasicComponent
17 private final WireEnd select, in;
18 private final WireEnd[] outputs;
19 private final int outputSize;
20 private int selected = -1;
23 * Output {@link Wire}s and in must be of uniform length
25 * @param in Must be of uniform length with all outputs.
26 * @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
27 * @param outputs One of these outputs receives the input signal, depending on the select bits
29 public Demux(int processTime, WireEnd in, WireEnd select, WireEnd... outputs)
32 outputSize = in.length();
35 this.outputs = outputs;
36 for (int i = 0; i < this.outputs.length; i++)
38 if (outputs[i].length() != outputSize)
39 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
40 this.outputs[i] = outputs[i];
44 select.addObserver(this);
46 int maxInputs = 1 << select.length();
47 if (this.outputs.length > maxInputs)
48 throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "
49 + select.length() + " select bits (" + maxInputs + ").");
56 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
57 if (selectValue >= outputs.length)
60 if (selected != selectValue && selected != -1)
61 outputs[selected].clearSignals();
63 selected = selectValue;
65 if (selectValue != -1)
66 outputs[selectValue].feedSignals(in.getValues());
70 public List<WireEnd> getAllInputs()
72 return List.of(in, select);
76 public List<WireEnd> getAllOutputs()
78 return List.of(outputs);