X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FMux.java;h=6ac32c6aa1d9a8b666ced232956c3173ccfed716;hb=f2cd90fa2b844507bc9697d3af1f3e18aac80b37;hp=0b19e7c8d4a594a6c532bdd7b22855e9e7d2c1d2;hpb=4c8fb641dc21efe699f457dcd48fc3cf3660740d;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/components/Mux.java b/era.mi/src/era/mi/logic/components/Mux.java index 0b19e7c8..6ac32c6a 100644 --- a/era.mi/src/era/mi/logic/components/Mux.java +++ b/era.mi/src/era/mi/logic/components/Mux.java @@ -1,77 +1,93 @@ -package era.mi.logic.components; - -import era.mi.logic.Bit; -import era.mi.logic.wires.WireArray; - -/** - * Models a Multiplexer. A is selected when select bit is 1, B when select bit is 0. Outputs X otherwise. - * @author Fabian - * - */ -public class Mux extends BasicComponent -{ - private WireArray a, b, out; - private WireArray select; - private final int size; - - /** - * {@link WireArray}s a, b and out must be of uniform length, select - * @param a Must be of uniform length with b and out. - * @param b Must be of uniform length with a and out. - * @param select C - * @param out Must be of uniform length with a and b. - */ - public Mux(int processTime, WireArray a, WireArray b, WireArray select, WireArray out) - { - super(processTime); - size = a.length; - if(b.length != out.length || b.length != size) - throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!"); - this.a = a; - a.addObserver(this); - this.b = b; - b.addObserver(this); - this.select = select; - select.addObserver(this); - this.out = out; - } - - @Override - protected void compute() - { - WireArray active = b; - switch(select.getValue()) - { - case ONE: - active = a; - case ZERO: - out.feedSignals(active.getValues()); - break; - default: - Bit[] newValues = new Bit[size]; - for(int i = 0; i < size; i++) - newValues[i] = Bit.X; - out.feedSignals(newValues); - } - } - - public WireArray getA() - { - return a; - } - - public WireArray getB() - { - return b; - } - - public WireArray getOut() - { - return out; - } - - public WireArray getSelect() - { - return select; - } -} +package era.mi.logic.components; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import era.mi.logic.wires.WireArray; +import era.mi.logic.wires.WireArray.WireArrayEnd; + +/** + * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the + * output. + * + * @author Fabian Stemmler + * + */ +public class Mux extends BasicComponent +{ + private WireArray select; + private WireArrayEnd outI; + private WireArray[] inputs; + private final int outputSize; + + /** + * Input {@link WireArray}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(int processTime, WireArray out, WireArray select, WireArray... inputs) + { + super(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 + ")."); + + outI = out.createInput(); + } + + public WireArray getOut() + { + return outI.owner; + } + + public WireArray getSelect() + { + return select; + } + + @Override + public void compute() + { + int selectValue; + if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length) + { + outI.clearSignals(); + return; + } + + WireArray active = inputs[selectValue]; + outI.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(outI.owner); + } +}