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