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.wires.Wire;
10 import net.mograsim.logic.core.wires.Wire.ReadEnd;
11 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
14 * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
17 * @author Fabian Stemmler
20 public class Mux extends BasicComponent
22 private ReadEnd select;
23 private ReadWriteEnd out;
24 private ReadEnd[] inputs;
25 private final int outputSize;
28 * Input {@link Wire}s and out must be of uniform length
30 * @param out Must be of uniform length with all inputs.
31 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
32 * @param inputs One of these inputs is mapped to the output, depending on the select bits
34 public Mux(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs)
36 super(timeline, processTime);
37 outputSize = out.length();
39 this.inputs = inputs.clone();
40 for (int i = 0; i < this.inputs.length; i++)
42 if (inputs[i].length() != outputSize)
43 throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
44 inputs[i].registerObserver(this);
48 select.registerObserver(this);
50 int maxInputs = 1 << select.length();
51 if (this.inputs.length > maxInputs)
52 throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "
53 + select.length() + " select bits (" + maxInputs + ").");
58 public ReadEnd getOut()
63 public ReadEnd getSelect()
72 if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
78 ReadEnd active = inputs[selectValue];
79 out.feedSignals(active.getValues());
83 public List<ReadEnd> getAllInputs()
85 ArrayList<ReadEnd> wires = new ArrayList<>(Arrays.asList(inputs));
87 return Collections.unmodifiableList(wires);
91 public List<ReadWriteEnd> getAllOutputs()