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