1 package net.mograsim.logic.core.components;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
8 import net.mograsim.logic.core.timeline.Timeline;
9 import net.mograsim.logic.core.timeline.TimelineEventHandler;
10 import net.mograsim.logic.core.types.BitVector;
11 import net.mograsim.logic.core.wires.CoreWire;
12 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
13 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
16 * Models a multiplexer. Takes an arbitrary amount of input {@link CoreWire}s, one of which, as determined by select, is put through to the
19 * @author Fabian Stemmler
22 public class CoreMux extends BasicCoreComponent
24 private ReadEnd select;
25 private ReadWriteEnd out;
26 private ReadEnd[] inputs;
27 private final int outputSize;
30 * Input {@link CoreWire}s and out must be of uniform width
32 * @param out Must be of uniform width with all inputs.
33 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
34 * @param inputs One of these inputs is mapped to the output, depending on the select bits
36 public CoreMux(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs)
38 super(timeline, processTime);
39 outputSize = out.width();
41 this.inputs = inputs.clone();
42 for (int i = 0; i < this.inputs.length; i++)
44 if (inputs[i].width() != outputSize)
45 throw new IllegalArgumentException("All MUX wire arrays must be of uniform width!");
46 inputs[i].registerObserver(this);
50 select.registerObserver(this);
52 int maxInputs = 1 << select.width();
53 if (this.inputs.length > maxInputs)
54 throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "
55 + select.width() + " select bits (" + maxInputs + ").");
60 public ReadEnd getOut()
65 public ReadEnd getSelect()
71 public TimelineEventHandler compute()
74 if (!select.getValues().isBinary() || (selectValue = (int) select.getValues().getUnsignedValueLong()) >= inputs.length)
76 return e -> out.clearSignals();
79 BitVector activeValues = inputs[selectValue].getValues();
80 return e -> out.feedSignals(activeValues);
84 public List<ReadEnd> getAllInputs()
86 ArrayList<ReadEnd> wires = new ArrayList<>(Arrays.asList(inputs));
88 return Collections.unmodifiableList(wires);
92 public List<ReadWriteEnd> getAllOutputs()