1 package era.mi.logic.components;
\r
3 import java.util.List;
\r
5 import era.mi.logic.timeline.Timeline;
\r
6 import era.mi.logic.wires.Wire;
\r
7 import era.mi.logic.wires.Wire.ReadEnd;
\r
8 import era.mi.logic.wires.Wire.ReadWriteEnd;
\r
11 * 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
14 * @author Fabian Stemmler
\r
17 public class Demux extends BasicComponent
\r
19 private final ReadEnd select, in;
\r
20 private final ReadWriteEnd[] outputs;
\r
21 private final int outputSize;
\r
22 private int selected = -1;
\r
25 * Output {@link Wire}s and in must be of uniform length
\r
27 * @param in Must be of uniform length with all outputs.
\r
28 * @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
\r
29 * @param outputs One of these outputs receives the input signal, depending on the select bits
\r
31 public Demux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)
\r
33 super(timeline, processTime);
\r
34 outputSize = in.length();
\r
37 this.outputs = outputs;
\r
38 for (int i = 0; i < this.outputs.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.outputs[i] = outputs[i];
\r
45 this.select = select;
\r
46 select.addObserver(this);
\r
48 int maxInputs = 1 << select.length();
\r
49 if (this.outputs.length > maxInputs)
\r
50 throw new IllegalArgumentException("There are more outputs (" + this.outputs.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 >= outputs.length)
\r
62 if (selected != selectValue && selected != -1)
\r
63 outputs[selected].clearSignals();
\r
65 selected = selectValue;
\r
67 if (selectValue != -1)
\r
68 outputs[selectValue].feedSignals(in.getValues());
\r
72 public List<ReadEnd> getAllInputs()
\r
74 return List.of(in, select);
\r
78 public List<ReadWriteEnd> getAllOutputs()
\r
80 return List.of(outputs);
\r