1 package era.mi.logic.components;
5 import era.mi.logic.wires.WireArray;
6 import era.mi.logic.wires.WireArray.WireArrayEnd;
9 * Models a multiplexer. Takes an arbitrary amount of outputs {@link WireArray}s, one of which, as determined by select, receives the input
12 * @author Fabian Stemmler
15 public class Demux extends BasicComponent
17 private final WireArray select, in;
18 private final WireArray[] outputs;
19 private final WireArrayEnd[] outputsI;
20 private final int outputSize;
21 private int selected = -1;
24 * Output {@link WireArray}s and in must be of uniform length
26 * @param in Must be of uniform length with all outputs.
27 * @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
28 * @param outputs One of these outputs receives the input signal, depending on the select bits
30 public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)
33 outputSize = in.length;
36 this.outputs = outputs;
37 this.outputsI = new WireArrayEnd[outputs.length];
38 for (int i = 0; i < this.outputsI.length; i++)
40 if (outputs[i].length != outputSize)
41 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
42 this.outputsI[i] = outputs[i].createInput();
46 select.addObserver(this);
48 int maxInputs = 1 << select.length;
49 if (this.outputsI.length > maxInputs)
50 throw new IllegalArgumentException("There are more outputs (" + this.outputsI.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 >= outputsI.length)
62 if (selected != selectValue && selected != -1)
63 outputsI[selected].clearSignals();
65 selected = selectValue;
67 if (selectValue != -1)
68 outputsI[selectValue].feedSignals(in.getValues());
72 public List<WireArray> getAllInputs()
74 return List.of(in, select);
78 public List<WireArray> getAllOutputs()
80 return List.of(outputs);