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