af227da114cf2049c837bf9e7357ca14419d7460
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / GUImux4_12.java
1 package net.mograsim.logic.model.am2900.components;
2
3 import java.util.Map;
4
5 import net.mograsim.logic.core.types.Bit;
6 import net.mograsim.logic.core.types.BitVector;
7
8 import static net.mograsim.logic.core.types.Bit.*;
9 import net.mograsim.logic.core.wires.Wire.ReadEnd;
10 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
13 import net.mograsim.logic.model.model.wires.Pin;
14 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
15
16 public class GUImux4_12 extends SimpleRectangularHardcodedGUIComponent
17 {
18         public GUImux4_12(ViewModelModifiable model, String name)
19         {
20                 super(model, name, "4-way MUX\n12 bit");
21                 setSize(80, 40);
22                 addPin(new Pin(this, "SA", 1, 0, 5), Usage.INPUT, Position.RIGHT);
23                 addPin(new Pin(this, "SB", 1, 0, 15), Usage.INPUT, Position.RIGHT);
24                 addPin(new Pin(this, "SC", 1, 0, 25), Usage.INPUT, Position.RIGHT);
25                 addPin(new Pin(this, "SD", 1, 0, 35), Usage.INPUT, Position.RIGHT);
26                 addPin(new Pin(this, "A", 12, 10, 0), Usage.INPUT, Position.BOTTOM);
27                 addPin(new Pin(this, "B", 12, 30, 0), Usage.INPUT, Position.BOTTOM);
28                 addPin(new Pin(this, "C", 12, 50, 0), Usage.INPUT, Position.BOTTOM);
29                 addPin(new Pin(this, "D", 12, 70, 0), Usage.INPUT, Position.BOTTOM);
30                 addPin(new Pin(this, "Y", 12, 40, 40), Usage.OUTPUT, Position.TOP);
31         }
32
33         @Override
34         protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
35         {
36                 Bit SAVal = readEnds.get("SA").getValue();
37                 Bit SBVal = readEnds.get("SB").getValue();
38                 Bit SCVal = readEnds.get("SC").getValue();
39                 Bit SDVal = readEnds.get("SD").getValue();
40                 BitVector YVal;
41                 if (SAVal == X || SBVal == X || SCVal == X || SDVal == X)
42                         YVal = BitVector.of(X, 12);
43                 else if (SAVal == U || SBVal == U || SCVal == U || SDVal == U)
44                         YVal = BitVector.of(U, 12);
45                 else if (SAVal == Z || SBVal == Z || SCVal == Z || SDVal == Z)
46                         YVal = BitVector.of(X, 12);
47                 else
48                 {
49                         YVal = null;
50                         if (SAVal == ONE)
51                                 YVal = readEnds.get("A").getValues();
52                         if (SBVal == ONE)
53                                 if (YVal != null)
54                                         YVal = BitVector.of(X, 12);
55                                 else
56                                         YVal = readEnds.get("B").getValues();
57                         if (SCVal == ONE)
58                                 if (YVal != null)
59                                         YVal = BitVector.of(X, 12);
60                                 else
61                                         YVal = readEnds.get("C").getValues();
62                         if (SDVal == ONE)
63                                 if (YVal != null)
64                                         YVal = BitVector.of(X, 12);
65                                 else
66                                         YVal = readEnds.get("D").getValues();
67                         if (YVal == null)
68                                 YVal = BitVector.of(ZERO, 12);
69                 }
70
71                 readWriteEnds.get("Y").feedSignals(YVal);
72                 return null;
73         }
74 }