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