Fixed stack high level state BitVector length (changed 3 to 12)
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / GUIdff12.java
index 5dd2f96..503831d 100644 (file)
@@ -8,11 +8,13 @@ import java.util.Arrays;
 import java.util.Map;
 
 import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.wires.Wire.ReadEnd;
 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
 import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.model.wires.PinUsage;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
 
@@ -22,13 +24,13 @@ public class GUIdff12 extends SimpleRectangularHardcodedGUIComponent
        {
                super(model, name, "D flip flop\n12 bits");
                setSize(40, 20);
-               addPin(new Pin(this, "D", 12, 20, 20), Usage.INPUT, Position.TOP);
-               addPin(new Pin(this, "C", 1, 0, 10), Usage.INPUT, Position.RIGHT);
-               addPin(new Pin(this, "Y", 12, 20, 0), Usage.OUTPUT, Position.BOTTOM);
+               addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 20), Position.TOP);
+               addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 10), Position.RIGHT);
+               addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 20, 0), Position.BOTTOM);
        }
 
        @Override
-       protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
+       public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
        {
                Bit[] QC = (Bit[]) lastState;
                if (QC == null)
@@ -48,6 +50,34 @@ public class GUIdff12 extends SimpleRectangularHardcodedGUIComponent
                return QC;
        }
 
+       @Override
+       protected Object getHighLevelState(Object state, String stateID)
+       {
+               switch (stateID)
+               {
+               case "q":
+                       return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12));
+               default:
+                       return super.getHighLevelState(state, stateID);
+               }
+       }
+
+       @Override
+       protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
+       {
+               switch (stateID)
+               {
+               case "q":
+                       BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
+                       if (newHighLevelStateCasted.length() != 12)
+                               throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
+                       System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 0, 12);
+                       return lastState;
+               default:
+                       return super.setHighLevelState(lastState, stateID, newHighLevelState);
+               }
+       }
+
        static
        {
                IndirectGUIComponentCreator.setComponentSupplier(GUIdff12.class.getCanonicalName(), (m, p, n) -> new GUIdff12(m, n));