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