e0a60386a2bc9bb1839d4e2259f2239833629d5c
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / model / components / mi / nandbased / GUIdlatch.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.ui.model.ViewModelModifiable;
5 import net.mograsim.logic.ui.model.components.atomic.GUINandGate;
6 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
7 import net.mograsim.logic.ui.model.wires.GUIWire;
8 import net.mograsim.logic.ui.model.wires.Pin;
9 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
10 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
11
12 public class GUIdlatch extends SimpleRectangularSubmodelComponent
13 {
14         private GUI_rsLatch _rsLatch;
15
16         public GUIdlatch(ViewModelModifiable model)
17         {
18                 this(model, null);
19         }
20
21         public GUIdlatch(ViewModelModifiable model, String name)
22         {
23                 super(model, 1, "GUIdlatch", name);
24                 setSubmodelScale(.4);
25                 setInputPins("D", "E");
26                 setOutputPins("Q", "_Q");
27                 initSubmodelComponents();
28         }
29
30         @SuppressWarnings("unused") // for GUIWires being created
31         private void initSubmodelComponents()
32         {
33                 Pin D = getSubmodelPin("D");
34                 Pin E = getSubmodelPin("E");
35                 Pin Q = getSubmodelPin("Q");
36                 Pin _Q = getSubmodelPin("_Q");
37
38                 GUINandGate nand1 = new GUINandGate(submodelModifiable, 1);
39                 GUINandGate nand2 = new GUINandGate(submodelModifiable, 1);
40                 _rsLatch = new GUI_rsLatch(submodelModifiable);
41
42                 WireCrossPoint cp1 = new WireCrossPoint(submodelModifiable, 1);
43                 WireCrossPoint cp2 = new WireCrossPoint(submodelModifiable, 1);
44
45                 nand1.moveTo(10, 2.5);
46                 nand2.moveTo(15, 27.5);
47                 _rsLatch.moveTo(45, 7.5);
48                 cp1.moveCenterTo(5, 37.5);
49                 cp2.moveCenterTo(35, 12.5);
50
51                 new GUIWire(submodelModifiable, D, nand1.getPin("A"));
52                 new GUIWire(submodelModifiable, E, cp1, new Point[0]);
53                 new GUIWire(submodelModifiable, cp1, nand1.getPin("B"), new Point(5, 17.5));
54                 new GUIWire(submodelModifiable, cp1, nand2.getPin("B"), new Point(5, 42.5));
55                 new GUIWire(submodelModifiable, nand1.getPin("Y"), cp2, new Point[0]);
56                 new GUIWire(submodelModifiable, cp2, nand2.getPin("A"), new Point(35, 25), new Point(10, 25), new Point(10, 32.5));
57                 new GUIWire(submodelModifiable, cp2, _rsLatch.getPin("_S"), new Point[0]);
58                 new GUIWire(submodelModifiable, nand2.getPin("Y"), _rsLatch.getPin("_R"), new Point(40, 37.5), new Point(40, 22.5));
59                 new GUIWire(submodelModifiable, _rsLatch.getPin("Q"), Q, new Point[0]);
60                 new GUIWire(submodelModifiable, _rsLatch.getPin("_Q"), _Q);
61
62                 addAtomicHighLevelStateID("q");
63         }
64
65         @Override
66         public void setAtomicHighLevelState(String stateID, Object newState)
67         {
68                 switch (stateID)
69                 {
70                 case "q":
71                         _rsLatch.setHighLevelState("q", newState);
72                         break;
73                 default:
74                         // should not happen because we tell SubmodelComponent to only allow these state IDs.
75                         throw new IllegalStateException("Illegal atomic state ID: " + stateID);
76                 }
77         }
78
79         @Override
80         public Object getAtomicHighLevelState(String stateID)
81         {
82                 switch (stateID)
83                 {
84                 case "q":
85                         return _rsLatch.getHighLevelState("q");
86                 default:
87                         // should not happen because we tell SubmodelComponent to only allow these state IDs.
88                         throw new IllegalStateException("Illegal atomic state ID: " + stateID);
89                 }
90         }
91
92         static
93         {
94                 IndirectGUIComponentCreator.setComponentSupplier(GUIdlatch.class.getCanonicalName(), (m, p, n) -> new GUIdlatch(m, n));
95         }
96 }