Added DeserializedSubmodelComponent Editor project
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / model / components / mi / nandbased / GUI_rsLatch.java
1 package net.mograsim.logic.ui.model.components.mi.nandbased;
2
3 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
4 import net.mograsim.logic.core.types.Bit;
5 import net.mograsim.logic.core.types.BitVector;
6 import net.mograsim.logic.ui.model.ViewModelModifiable;
7 import net.mograsim.logic.ui.model.components.atomic.GUINandGate;
8 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
9 import net.mograsim.logic.ui.model.wires.GUIWire;
10 import net.mograsim.logic.ui.model.wires.Pin;
11 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
12 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
13
14 public class GUI_rsLatch extends SimpleRectangularSubmodelComponent
15 {
16         private GUIWire wireQ, wire_Q;
17
18         public GUI_rsLatch(ViewModelModifiable model)
19         {
20                 super(model, 1, "_rsLatch");
21                 setSubmodelScale(.4);
22                 setInputPins("_S", "_R");
23                 setOutputPins("Q", "_Q");
24                 initSubmodelComponents();
25         }
26
27         @SuppressWarnings("unused") // for GUIWires being created
28         private void initSubmodelComponents()
29         {
30                 Pin _S = getSubmodelPin("_S");
31                 Pin _R = getSubmodelPin("_R");
32                 Pin Q = getSubmodelPin("Q");
33                 Pin _Q = getSubmodelPin("_Q");
34
35                 GUINandGate nand1 = new GUINandGate(submodelModifiable, 1);
36                 GUINandGate nand2 = new GUINandGate(submodelModifiable, 1);
37
38                 WireCrossPoint cp1 = new WireCrossPoint(submodelModifiable, 1);
39                 WireCrossPoint cp2 = new WireCrossPoint(submodelModifiable, 1);
40
41                 nand1.moveTo(10, 7.5);
42                 nand2.moveTo(40, 12.5);
43                 cp1.moveCenterTo(35, 17.5);
44                 cp2.moveCenterTo(65, 37.5);
45
46                 new GUIWire(submodelModifiable, _S, nand1.getPin("A"), new Point[0]);
47                 new GUIWire(submodelModifiable, _R, nand2.getPin("B"), new Point(35, 37.5), new Point(35, 27.5));
48                 new GUIWire(submodelModifiable, nand1.getPin("Y"), cp1, new Point[0]);
49                 new GUIWire(submodelModifiable, nand2.getPin("Y"), cp2, new Point(65, 22.5));
50                 new GUIWire(submodelModifiable, cp1, nand2.getPin("A"), new Point[0]);
51                 new GUIWire(submodelModifiable, cp2, nand1.getPin("B"), new Point(65, 42.5), new Point(5, 42.5), new Point(5, 22.5));
52                 wireQ = new GUIWire(submodelModifiable, cp1, Q, new Point(35, 17.5), new Point(35, 7.5), new Point(65, 7.5), new Point(65, 12.5));
53                 wire_Q = new GUIWire(submodelModifiable, cp2, _Q, new Point[0]);
54
55                 addAtomicHighLevelStateID("q");
56         }
57
58         @Override
59         public void setAtomicHighLevelState(String stateID, Object newState)
60         {
61                 switch (stateID)
62                 {
63                 case "q":
64                         if (wireQ != null)
65                         {
66                                 // TODO force this to happen without any Timeline updates in the meantime.
67                                 // Maybe make it a requirement of setHighLevelState that the Timeline is "halted" during a call?
68                                 Bit newStateCasted = (Bit) newState;
69                                 BitVector newStateVector = BitVector.of(newStateCasted);
70                                 if (wireQ.hasLogicModelBinding())
71                                         wireQ.forceWireValues(newStateVector);
72                                 // We set both wires because then both outputs go to their correct state at the same time, and to avoid problems when not
73                                 // both
74                                 // inputs are 1
75                                 if (wire_Q.hasLogicModelBinding())
76                                         wire_Q.forceWireValues(newStateVector.not());
77                         }
78                         break;
79                 default:
80                         // should not happen because we tell SubmodelComponent to only allow these state IDs.
81                         throw new IllegalStateException("Illegal atomic state ID: " + stateID);
82                 }
83         }
84
85         @Override
86         public Object getAtomicHighLevelState(String stateID)
87         {
88                 switch (stateID)
89                 {
90                 case "q":
91                         if (wireQ.hasLogicModelBinding())
92                                 return wireQ.getWireValues().getBit(0);
93                         return null;
94                 default:
95                         // should not happen because we tell SubmodelComponent to only allow these state IDs.
96                         throw new IllegalStateException("Illegal atomic state ID: " + stateID);
97                 }
98         }
99
100         static
101         {
102                 IndirectGUIComponentCreator.setComponentProvider(GUI_rsLatch.class.getCanonicalName(), (m, p) -> new GUI_rsLatch(m));
103         }
104 }