Renamed logic.ui to logic.model
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / model / components / mi / nandbased / GUIdff.java
1 package net.mograsim.logic.model.model.components.mi.nandbased;
2
3 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
4 import net.mograsim.logic.model.model.ViewModelModifiable;
5 import net.mograsim.logic.model.model.components.atomic.GUINandGate;
6 import net.mograsim.logic.model.model.components.submodels.SimpleRectangularSubmodelComponent;
7 import net.mograsim.logic.model.model.wires.GUIWire;
8 import net.mograsim.logic.model.model.wires.Pin;
9 import net.mograsim.logic.model.model.wires.WireCrossPoint;
10 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
11
12 public class GUIdff extends SimpleRectangularSubmodelComponent
13 {
14         private GUI_rsLatch _rsLatch;
15
16         public GUIdff(ViewModelModifiable model)
17         {
18                 this(model, null);
19         }
20
21         public GUIdff(ViewModelModifiable model, String name)
22         {
23                 super(model, 1, "GUIdff", name);
24                 setSubmodelScale(.2);
25                 setInputPins("C", "D");
26                 setOutputPins("Q", "_Q");
27                 initSubmodelComponents();
28         }
29
30         @SuppressWarnings("unused") // for GUIWires being created
31         private void initSubmodelComponents()
32         {
33                 Pin C = getSubmodelPin("C");
34                 Pin D = getSubmodelPin("D");
35                 Pin Q = getSubmodelPin("Q");
36                 Pin _Q = getSubmodelPin("_Q");
37
38                 GUI_rsLatch _rsLatch1 = new GUI_rsLatch(submodelModifiable);
39                 GUInand3 nand3 = new GUInand3(submodelModifiable);
40                 GUINandGate nand2 = new GUINandGate(submodelModifiable, 1);
41                 GUI_rsLatch _rsLatch2 = this._rsLatch = new GUI_rsLatch(submodelModifiable);
42
43                 WireCrossPoint cp1 = new WireCrossPoint(submodelModifiable, 1);
44                 WireCrossPoint cp2 = new WireCrossPoint(submodelModifiable, 1);
45                 WireCrossPoint cp3 = new WireCrossPoint(submodelModifiable, 1);
46                 WireCrossPoint cp4 = new WireCrossPoint(submodelModifiable, 1);
47
48                 _rsLatch1.moveTo(40, 10);
49                 nand3.moveTo(40, 40);
50                 nand2.moveTo(120, 60);
51                 _rsLatch2.moveTo(120, 30);
52                 cp1.moveCenterTo(10, 25);
53                 cp2.moveCenterTo(20, 65);
54                 cp3.moveCenterTo(100, 35);
55                 cp4.moveCenterTo(100, 45);
56
57                 new GUIWire(submodelModifiable, C, cp1, new Point[0]);
58                 new GUIWire(submodelModifiable, cp1, _rsLatch1.getPin("_R"), new Point[0]);
59                 new GUIWire(submodelModifiable, cp1, nand3.getPin("B"), new Point(10, 55));
60                 new GUIWire(submodelModifiable, D, nand2.getPin("B"), new Point[0]);
61                 new GUIWire(submodelModifiable, nand2.getPin("Y"), cp2, new Point(145, 70), new Point(145, 85), new Point(20, 85));
62                 new GUIWire(submodelModifiable, cp2, _rsLatch1.getPin("_S"), new Point(20, 15));
63                 new GUIWire(submodelModifiable, cp2, nand3.getPin("C"), new Point[0]);
64                 new GUIWire(submodelModifiable, _rsLatch1.getPin("_Q"), cp3, new Point(100, 25));
65                 new GUIWire(submodelModifiable, cp3, nand3.getPin("A"), new Point(30, 35), new Point(30, 45));
66                 new GUIWire(submodelModifiable, cp3, _rsLatch2.getPin("_S"), new Point[0]);
67                 new GUIWire(submodelModifiable, nand3.getPin("Y"), cp4, new Point[0]);
68                 new GUIWire(submodelModifiable, cp4, _rsLatch2.getPin("_R"), new Point[0]);
69                 new GUIWire(submodelModifiable, cp4, nand2.getPin("A"), new Point(100, 65));
70                 new GUIWire(submodelModifiable, _rsLatch2.getPin("Q"), Q);
71                 new GUIWire(submodelModifiable, _rsLatch2.getPin("_Q"), _Q);
72
73                 addAtomicHighLevelStateID("q");
74         }
75
76         @Override
77         public void setAtomicHighLevelState(String stateID, Object newState)
78         {
79                 switch (stateID)
80                 {
81                 case "q":
82                         _rsLatch.setHighLevelState("q", newState);
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                         return _rsLatch.getHighLevelState("q");
97                 default:
98                         // should not happen because we tell SubmodelComponent to only allow these state IDs.
99                         throw new IllegalStateException("Illegal atomic state ID: " + stateID);
100                 }
101         }
102
103         static
104         {
105                 IndirectGUIComponentCreator.setComponentSupplier(GUIdff.class.getCanonicalName(), (m, p, n) -> new GUIdff(m, n));
106         }
107 }