Merge remote-tracking branch 'origin/development' into development
[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.BitVector;
5 import net.mograsim.logic.ui.model.ViewModelModifiable;
6 import net.mograsim.logic.ui.model.components.GUINandGate;
7 import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
8 import net.mograsim.logic.ui.model.wires.GUIWire;
9 import net.mograsim.logic.ui.model.wires.Pin;
10 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
11
12 public class GUI_rsLatch extends SimpleRectangularSubmodelComponent
13 {
14         private GUIWire wireQ, wire_Q;
15
16         public GUI_rsLatch(ViewModelModifiable model)
17         {
18                 super(model, 1, "_rsLatch");
19                 setSubmodelScale(.4);
20                 setInputPins("_S", "_R");
21                 setOutputPins("Q", "_Q");
22                 initSubmodelComponents();
23         }
24
25         @SuppressWarnings("unused") // for GUIWires being created
26         private void initSubmodelComponents()
27         {
28                 Pin _S = getSubmodelPin("_S");
29                 Pin _R = getSubmodelPin("_R");
30                 Pin Q = getSubmodelPin("Q");
31                 Pin _Q = getSubmodelPin("_Q");
32
33                 GUINandGate nand1 = new GUINandGate(submodelModifiable, 1);
34                 GUINandGate nand2 = new GUINandGate(submodelModifiable, 1);
35
36                 WireCrossPoint cp1 = new WireCrossPoint(submodelModifiable, 1);
37                 WireCrossPoint cp2 = new WireCrossPoint(submodelModifiable, 1);
38
39                 nand1.moveTo(10, 7.5);
40                 nand2.moveTo(40, 12.5);
41                 cp1.moveCenterTo(35, 17.5);
42                 cp2.moveCenterTo(65, 37.5);
43
44                 new GUIWire(submodelModifiable, _S, nand1.getPin("A"), new Point[0]);
45                 new GUIWire(submodelModifiable, _R, nand2.getPin("B"), new Point(35, 37.5), new Point(35, 27.5));
46                 new GUIWire(submodelModifiable, nand1.getPin("Y"), cp1, new Point[0]);
47                 new GUIWire(submodelModifiable, nand2.getPin("Y"), cp2, new Point(65, 22.5));
48                 new GUIWire(submodelModifiable, cp1, nand2.getPin("A"), new Point[0]);
49                 new GUIWire(submodelModifiable, cp2, nand1.getPin("B"), new Point(65, 42.5), new Point(5, 42.5), new Point(5, 22.5));
50                 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));
51                 wire_Q = new GUIWire(submodelModifiable, cp2, _Q, new Point[0]);
52         }
53
54         @Override
55         public void setHighLevelState(String stateID, Object newState)
56         {
57                 if ("q".equals(stateID))
58                 {
59                         // TODO force this to happen without any Timeline updates in the meantime.
60                         // Maybe make it a requirement of setHighLevelState that the Timeline is "halted" during a call?
61                         BitVector newStateCasted = (BitVector) newState;
62                         if (wireQ.hasLogicModelBinding())
63                                 wireQ.forceWireValues(newStateCasted);
64                         // We set both wires because then both outputs go to their correct state at the same time, and to avoid problems when not both
65                         // inputs are 1
66                         if (wire_Q.hasLogicModelBinding())
67                                 wire_Q.forceWireValues(newStateCasted.not());
68                 } else
69                         super.setHighLevelState(stateID, newState);
70         }
71 }