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