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