Implemented GUI_rsLatch's high level state handling
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 15 Jul 2019 17:13:13 +0000 (19:13 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 15 Jul 2019 17:13:13 +0000 (19:13 +0200)
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java

index 20fbbaa..004aa50 100644 (file)
@@ -1,8 +1,8 @@
 package net.mograsim.logic.model.model.components.mi.nandbased;
 
+import java.util.Arrays;
+
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-import net.mograsim.logic.core.types.Bit;
-import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.atomic.GUINandGate;
 import net.mograsim.logic.model.model.components.submodels.SimpleRectangularSubmodelComponent;
@@ -10,10 +10,12 @@ import net.mograsim.logic.model.model.wires.GUIWire;
 import net.mograsim.logic.model.model.wires.Pin;
 import net.mograsim.logic.model.model.wires.WireCrossPoint;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.WireForcingAtomicHighLevelStateHandler;
 
 public class GUI_rsLatch extends SimpleRectangularSubmodelComponent
 {
-       private GUIWire wireQ, wire_Q;
+       private StandardHighLevelStateHandler highLevelStateHandler;
 
        public GUI_rsLatch(ViewModelModifiable model)
        {
@@ -54,52 +56,25 @@ public class GUI_rsLatch extends SimpleRectangularSubmodelComponent
                new GUIWire(submodelModifiable, nand2.getPin("Y"), cp2, new Point(65, 22.5));
                new GUIWire(submodelModifiable, cp1, nand2.getPin("A"), new Point[0]);
                new GUIWire(submodelModifiable, cp2, nand1.getPin("B"), new Point(65, 42.5), new Point(5, 42.5), new Point(5, 22.5));
-               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));
-               wire_Q = new GUIWire(submodelModifiable, cp2, _Q, new Point[0]);
+               GUIWire wireQ = new GUIWire(submodelModifiable, "q", cp1, Q, new Point(35, 17.5), new Point(35, 7.5), new Point(65, 7.5),
+                               new Point(65, 12.5));
+               GUIWire wire_Q = new GUIWire(submodelModifiable, "_q", cp2, _Q, new Point[0]);
 
-               addAtomicHighLevelStateID("q");
+               highLevelStateHandler = new StandardHighLevelStateHandler(this);
+               highLevelStateHandler.addAtomicHighLevelState("q", WireForcingAtomicHighLevelStateHandler::new).set(Arrays.asList(wireQ),
+                               Arrays.asList(wire_Q));
        }
 
        @Override
-       public void setAtomicHighLevelState(String stateID, Object newState)
+       public Object getHighLevelState(String stateID)
        {
-               switch (stateID)
-               {
-               case "q":
-                       if (wireQ != null)
-                       {
-                               // TODO force this to happen without any Timeline updates in the meantime.
-                               // Maybe make it a requirement of setHighLevelState that the Timeline is "halted" during a call?
-                               Bit newStateCasted = (Bit) newState;
-                               BitVector newStateVector = BitVector.of(newStateCasted);
-                               if (wireQ.hasLogicModelBinding())
-                                       wireQ.forceWireValues(newStateVector);
-                               // We set both wires because then both outputs go to their correct state at the same time, and to avoid problems when not
-                               // both
-                               // inputs are 1
-                               if (wire_Q.hasLogicModelBinding())
-                                       wire_Q.forceWireValues(newStateVector.not());
-                       }
-                       break;
-               default:
-                       // should not happen because we tell SubmodelComponent to only allow these state IDs.
-                       throw new IllegalStateException("Illegal atomic state ID: " + stateID);
-               }
+               return highLevelStateHandler.getHighLevelState(stateID);
        }
 
        @Override
-       public Object getAtomicHighLevelState(String stateID)
+       public void setHighLevelState(String stateID, Object newState)
        {
-               switch (stateID)
-               {
-               case "q":
-                       if (wireQ.hasLogicModelBinding())
-                               return wireQ.getWireValues().getLSBit(0);
-                       return null;
-               default:
-                       // should not happen because we tell SubmodelComponent to only allow these state IDs.
-                       throw new IllegalStateException("Illegal atomic state ID: " + stateID);
-               }
+               highLevelStateHandler.setHighLevelState(stateID, newState);
        }
 
        static