Merge branch 'development' of
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / CoreDemux.java
1 package net.mograsim.logic.core.components;
2
3 import java.util.List;
4
5 import net.mograsim.logic.core.timeline.Timeline;
6 import net.mograsim.logic.core.wires.CoreWire;
7 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
8 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
9
10 /**
11  * Models a multiplexer. Takes an arbitrary amount of input {@link CoreWire}s, one of which, as determined by select, is put through to the
12  * output.
13  * 
14  * @author Fabian Stemmler
15  *
16  */
17 public class CoreDemux extends BasicCoreComponent
18 {
19         private final ReadEnd select, in;
20         private final ReadWriteEnd[] outputs;
21         private final int outputSize;
22         private int selected = -1;
23
24         /**
25          * Output {@link CoreWire}s and in must be of uniform width
26          * 
27          * @param in      Must be of uniform width with all outputs.
28          * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
29          * @param outputs One of these outputs receives the input signal, depending on the select bits
30          */
31         public CoreDemux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)
32         {
33                 super(timeline, processTime);
34                 outputSize = in.width();
35
36                 this.in = in;
37                 this.outputs = outputs;
38                 for (int i = 0; i < this.outputs.length; i++)
39                 {
40                         if (outputs[i].width() != outputSize)
41                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform width!");
42                         this.outputs[i] = outputs[i];
43                 }
44
45                 this.select = select;
46                 select.registerObserver(this);
47
48                 int maxInputs = 1 << select.width();
49                 if (this.outputs.length > maxInputs)
50                         throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "
51                                         + select.width() + " select bits (" + maxInputs + ").");
52                 in.registerObserver(this);
53         }
54
55         @Override
56         public void compute()
57         {
58                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
59                 if (selectValue >= outputs.length)
60                         selectValue = -1;
61
62                 if (selected != selectValue && selected != -1)
63                         outputs[selected].clearSignals();
64
65                 selected = selectValue;
66
67                 if (selectValue != -1)
68                         outputs[selectValue].feedSignals(in.getValues());
69         }
70
71         @Override
72         public List<ReadEnd> getAllInputs()
73         {
74                 return List.of(in, select);
75         }
76
77         @Override
78         public List<ReadWriteEnd> getAllOutputs()
79         {
80                 return List.of(outputs);
81         }
82 }