X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2FMux.java;fp=era.mi%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2FMux.java;h=bbb10f5e8db9fc5df6561b1861f4823135e4ef8e;hb=0009789a8df6b8d4562b6e1cbfa75102a7516ea8;hp=0000000000000000000000000000000000000000;hpb=a28f7aa0dab4248e99159c5a647676170cb17a4e;p=Mograsim.git diff --git a/era.mi/src/net/mograsim/logic/core/components/Mux.java b/era.mi/src/net/mograsim/logic/core/components/Mux.java new file mode 100644 index 00000000..bbb10f5e --- /dev/null +++ b/era.mi/src/net/mograsim/logic/core/components/Mux.java @@ -0,0 +1,95 @@ +package net.mograsim.logic.core.components; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +/** + * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the + * output. + * + * @author Fabian Stemmler + * + */ +public class Mux extends BasicComponent +{ + private ReadEnd select; + private ReadWriteEnd out; + private ReadEnd[] inputs; + private final int outputSize; + + /** + * Input {@link Wire}s and out must be of uniform length + * + * @param out Must be of uniform length with all inputs. + * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs. + * @param inputs One of these inputs is mapped to the output, depending on the select bits + */ + public Mux(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs) + { + super(timeline, processTime); + outputSize = out.length(); + + this.inputs = inputs.clone(); + for (int i = 0; i < this.inputs.length; i++) + { + if (inputs[i].length() != outputSize) + throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!"); + inputs[i].addObserver(this); + } + + this.select = select; + select.addObserver(this); + + int maxInputs = 1 << select.length(); + if (this.inputs.length > maxInputs) + throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by " + + select.length() + " select bits (" + maxInputs + ")."); + + this.out = out; + } + + public ReadEnd getOut() + { + return out; + } + + public ReadEnd getSelect() + { + return select; + } + + @Override + public void compute() + { + int selectValue; + if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length) + { + out.clearSignals(); + return; + } + + ReadEnd active = inputs[selectValue]; + out.feedSignals(active.getValues()); + } + + @Override + public List getAllInputs() + { + ArrayList wires = new ArrayList(Arrays.asList(inputs)); + wires.add(select); + return Collections.unmodifiableList(wires); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } +}