Restructured high level state access
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / model / components / mi / nandbased / GUIdff.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.GUINandGate;
6 import net.mograsim.logic.ui.model.components.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
11 public class GUIdff extends SimpleRectangularSubmodelComponent
12 {
13         private GUI_rsLatch _rsLatch;
14
15         public GUIdff(ViewModelModifiable model)
16         {
17                 super(model, 1, "GUIdff");
18                 setSubmodelScale(.2);
19                 setInputPins("C", "D");
20                 setOutputPins("Q", "_Q");
21                 initSubmodelComponents();
22         }
23
24         @SuppressWarnings("unused") // for GUIWires being created
25         private void initSubmodelComponents()
26         {
27                 Pin C = getSubmodelPin("C");
28                 Pin D = getSubmodelPin("D");
29                 Pin Q = getSubmodelPin("Q");
30                 Pin _Q = getSubmodelPin("_Q");
31
32                 GUI_rsLatch _rsLatch1 = new GUI_rsLatch(submodelModifiable);
33                 GUInand3 nand3 = new GUInand3(submodelModifiable);
34                 GUINandGate nand2 = new GUINandGate(submodelModifiable, 1);
35                 GUI_rsLatch _rsLatch2 = this._rsLatch = new GUI_rsLatch(submodelModifiable);
36
37                 WireCrossPoint cp1 = new WireCrossPoint(submodelModifiable, 1);
38                 WireCrossPoint cp2 = new WireCrossPoint(submodelModifiable, 1);
39                 WireCrossPoint cp3 = new WireCrossPoint(submodelModifiable, 1);
40                 WireCrossPoint cp4 = new WireCrossPoint(submodelModifiable, 1);
41
42                 _rsLatch1.moveTo(40, 10);
43                 nand3.moveTo(40, 40);
44                 nand2.moveTo(120, 60);
45                 _rsLatch2.moveTo(120, 30);
46                 cp1.moveCenterTo(10, 25);
47                 cp2.moveCenterTo(20, 65);
48                 cp3.moveCenterTo(100, 35);
49                 cp4.moveCenterTo(100, 45);
50
51                 new GUIWire(submodelModifiable, C, cp1, new Point[0]);
52                 new GUIWire(submodelModifiable, cp1, _rsLatch1.getPin("_R"), new Point[0]);
53                 new GUIWire(submodelModifiable, cp1, nand3.getPin("B"), new Point(10, 55));
54                 new GUIWire(submodelModifiable, D, nand2.getPin("B"), new Point[0]);
55                 new GUIWire(submodelModifiable, nand2.getPin("Y"), cp2, new Point(145, 70), new Point(145, 85), new Point(20, 85));
56                 new GUIWire(submodelModifiable, cp2, _rsLatch1.getPin("_S"), new Point(20, 15));
57                 new GUIWire(submodelModifiable, cp2, nand3.getPin("C"), new Point[0]);
58                 new GUIWire(submodelModifiable, _rsLatch1.getPin("_Q"), cp3, new Point(100, 25));
59                 new GUIWire(submodelModifiable, cp3, nand3.getPin("A"), new Point(30, 35), new Point(30, 45));
60                 new GUIWire(submodelModifiable, cp3, _rsLatch2.getPin("_S"), new Point[0]);
61                 new GUIWire(submodelModifiable, nand3.getPin("Y"), cp4, new Point[0]);
62                 new GUIWire(submodelModifiable, cp4, _rsLatch2.getPin("_R"), new Point[0]);
63                 new GUIWire(submodelModifiable, cp4, nand2.getPin("A"), new Point(100, 65));
64                 new GUIWire(submodelModifiable, _rsLatch2.getPin("Q"), Q);
65                 new GUIWire(submodelModifiable, _rsLatch2.getPin("_Q"), _Q);
66
67                 addAtomicHighLevelStateID("q");
68         }
69
70         @Override
71         public void setAtomicHighLevelState(String stateID, Object newState)
72         {
73                 switch (stateID)
74                 {
75                 case "q":
76                         _rsLatch.setHighLevelState("q", newState);
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                         return _rsLatch.getHighLevelState("q");
91                 default:
92                         // should not happen because we tell SubmodelComponent to only allow these state IDs.
93                         throw new IllegalStateException("Illegal atomic state ID: " + stateID);
94                 }
95         }
96 }