1 package era.mi.logic.components;
3 import java.util.Arrays;
4 import java.util.Collections;
7 import era.mi.logic.wires.WireArray;
8 import era.mi.logic.wires.WireArray.WireArrayInput;
11 * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which,
12 * as determined by select, is put through to the output.
13 * @author Fabian Stemmler
16 public class Demux extends BasicComponent
18 private final WireArray select, in;
19 private final WireArray[] outputs;
20 private final WireArrayInput[] outputsI;
21 private final int outputSize;
22 private int selected = -1;
25 * Input {@link WireArray}s and out must be of uniform length
26 * @param out Must be of uniform length with all inputs.
27 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits
28 * to index all inputs.
29 * @param outputs One of these inputs is mapped to the output, depending on the select bits
31 public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)
34 outputSize = in.length;
37 this.outputs = outputs;
38 this.outputsI = new WireArrayInput[outputs.length];
39 for(int i = 0; i < this.outputsI.length; i++)
41 if(outputs[i].length != outputSize)
42 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
43 this.outputsI[i] = outputs[i].createInput();
47 select.addObserver(this);
49 int maxInputs = 1 << select.length;
50 if(this.outputsI.length > maxInputs)
51 throw new IllegalArgumentException("There are more outputs ("
52 + this.outputsI.length + ") to the DEMUX than supported by "
53 + select.length + " select bits (" + maxInputs + ").");
58 public void compute() {
59 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
60 if(selectValue >= outputsI.length)
63 if(selected != selectValue && selected != -1)
64 outputsI[selected].clearSignals();
66 selected = selectValue;
69 outputsI[selectValue].feedSignals(in.getValues());
73 public List<WireArray> getAllInputs()
75 return Collections.unmodifiableList(Arrays.asList(in, select));
79 public List<WireArray> getAllOutputs()
81 return Collections.unmodifiableList(Arrays.asList(outputs));