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