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