1 package era.mi.logic.components;
5 import era.mi.logic.wires.Wire;
6 import era.mi.logic.wires.Wire.ReadEnd;
7 import era.mi.logic.wires.Wire.ReadWriteEnd;
10 * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
13 * @author Fabian Stemmler
16 public class Demux extends BasicComponent
18 private final ReadEnd select, in;
19 private final ReadWriteEnd[] outputs;
20 private final int outputSize;
21 private int selected = -1;
24 * Output {@link Wire}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, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)
33 outputSize = in.length();
36 this.outputs = outputs;
37 for (int i = 0; i < this.outputs.length; i++)
39 if (outputs[i].length() != outputSize)
40 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
41 this.outputs[i] = outputs[i];
45 select.addObserver(this);
47 int maxInputs = 1 << select.length();
48 if (this.outputs.length > maxInputs)
49 throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "
50 + select.length() + " select bits (" + maxInputs + ").");
57 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
58 if (selectValue >= outputs.length)
61 if (selected != selectValue && selected != -1)
62 outputs[selected].clearSignals();
64 selected = selectValue;
66 if (selectValue != -1)
67 outputs[selectValue].feedSignals(in.getValues());
71 public List<ReadEnd> getAllInputs()
73 return List.of(in, select);
77 public List<ReadWriteEnd> getAllOutputs()
79 return List.of(outputs);