WireEnd functionality split into ReadEnd and ReadWriteEnd
[Mograsim.git] / era.mi / src / era / mi / logic / components / Mux.java
1 package era.mi.logic.components;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import era.mi.logic.wires.Wire;
9 import era.mi.logic.wires.Wire.ReadEnd;
10 import era.mi.logic.wires.Wire.ReadWriteEnd;
11
12 /**
13  * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
14  * output.
15  * 
16  * @author Fabian Stemmler
17  *
18  */
19 public class Mux extends BasicComponent
20 {
21         private ReadEnd select;
22         private ReadWriteEnd out;
23         private ReadEnd[] inputs;
24         private final int outputSize;
25
26         /**
27          * Input {@link Wire}s and out must be of uniform length
28          * 
29          * @param out    Must be of uniform length with all inputs.
30          * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
31          * @param inputs One of these inputs is mapped to the output, depending on the select bits
32          */
33         public Mux(int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs)
34         {
35                 super(processTime);
36                 outputSize = out.length();
37
38                 this.inputs = inputs.clone();
39                 for (int i = 0; i < this.inputs.length; i++)
40                 {
41                         if (inputs[i].length() != outputSize)
42                                 throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
43                         inputs[i].addObserver(this);
44                 }
45
46                 this.select = select;
47                 select.addObserver(this);
48
49                 int maxInputs = 1 << select.length();
50                 if (this.inputs.length > maxInputs)
51                         throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "
52                                         + select.length() + " select bits (" + maxInputs + ").");
53
54                 this.out = out;
55         }
56
57         public ReadEnd getOut()
58         {
59                 return out;
60         }
61
62         public ReadEnd getSelect()
63         {
64                 return select;
65         }
66
67         @Override
68         public void compute()
69         {
70                 int selectValue;
71                 if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
72                 {
73                         out.clearSignals();
74                         return;
75                 }
76
77                 ReadEnd active = inputs[selectValue];
78                 out.feedSignals(active.getValues());
79         }
80
81         @Override
82         public List<ReadEnd> getAllInputs()
83         {
84                 ArrayList<ReadEnd> wires = new ArrayList<ReadEnd>(Arrays.asList(inputs));
85                 wires.add(select);
86                 return Collections.unmodifiableList(wires);
87         }
88
89         @Override
90         public List<ReadWriteEnd> getAllOutputs()
91         {
92                 return List.of(out);
93         }
94 }