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