Revisited some hardcoded components
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / am2910 / GUIAm2910SP.java
index b19e3bb..0e89cca 100644 (file)
@@ -1,7 +1,11 @@
 package net.mograsim.logic.model.am2900.components.am2910;
 
+import static net.mograsim.logic.core.types.Bit.ONE;
+import static net.mograsim.logic.core.types.Bit.U;
+import static net.mograsim.logic.core.types.Bit.X;
+import static net.mograsim.logic.core.types.Bit.ZERO;
+
 import java.util.Map;
-import static net.mograsim.logic.core.types.Bit.*;
 
 import net.mograsim.logic.core.types.Bit;
 import net.mograsim.logic.core.types.BitVector;
@@ -10,6 +14,8 @@ 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;
 
 public class GUIAm2910SP extends SimpleRectangularHardcodedGUIComponent
@@ -18,16 +24,16 @@ public class GUIAm2910SP extends SimpleRectangularHardcodedGUIComponent
        {
                super(model, name, "Stack\npointer");
                setSize(40, 30);
-               addPin(new Pin(this, "STKI0", 1, 0, 5), Usage.INPUT, Position.RIGHT);
-               addPin(new Pin(this, "STKI1", 1, 0, 15), Usage.INPUT, Position.RIGHT);
-               addPin(new Pin(this, "C", 1, 0, 25), Usage.INPUT, Position.RIGHT);
-               addPin(new Pin(this, "A", 3, 10, 30), Usage.OUTPUT, Position.TOP);
-               addPin(new Pin(this, "B", 3, 30, 30), Usage.OUTPUT, Position.TOP);
-               addPin(new Pin(this, "_FULL", 1, 40, 15), Usage.OUTPUT, Position.LEFT);
+               addPin(new Pin(this, "STKI0", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
+               addPin(new Pin(this, "STKI1", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
+               addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 25), Position.RIGHT);
+               addPin(new Pin(this, "A", 3, PinUsage.OUTPUT, 10, 30), Position.TOP);
+               addPin(new Pin(this, "B", 3, PinUsage.OUTPUT, 30, 30), Position.TOP);
+               addPin(new Pin(this, "_FULL", 1, PinUsage.OUTPUT, 40, 15), Position.LEFT);
        }
 
        @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)
        {
                BitAndInt SPC = (BitAndInt) lastState;
                if (SPC == null)
@@ -57,7 +63,7 @@ public class GUIAm2910SP extends SimpleRectangularHardcodedGUIComponent
                        else if (STKI0Val == ONE && STKI1Val == ONE)
                                // POP
                                SP = SP <= 0 ? SP : SP - 1;
-               readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP - 1));
+               readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP < 0 ? SP : SP - 1));
                readWriteEnds.get("B").feedSignals(getAsBitVector(SP == 5 ? 4 : SP));
                readWriteEnds.get("_FULL").feedSignals(SP == -2 ? U : SP == -1 ? X : SP == 5 ? ZERO : ONE);
 
@@ -66,6 +72,41 @@ public class GUIAm2910SP extends SimpleRectangularHardcodedGUIComponent
                return SPC;
        }
 
+       @Override
+       protected Object getHighLevelState(Object state, String stateID)
+       {
+               switch (stateID)
+               {
+               case "q":
+                       return getAsBitVector(((BitAndInt) state).i);
+               default:
+                       return super.getHighLevelState(state, stateID);
+               }
+       }
+
+       @Override
+       protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
+       {
+               switch (stateID)
+               {
+               case "q":
+                       int i;
+                       BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
+                       if (newHighLevelStateCasted.length() != 3)
+                               throw new IllegalArgumentException("Expected BitVector of length 3, not " + newHighLevelStateCasted.length());
+                       if (newHighLevelStateCasted.isBinary())
+                               i = newHighLevelStateCasted.getUnsignedValue().intValue();
+                       else
+                               i = -1;// this makes setting to U impossible
+                       if (i > 5)
+                               throw new IllegalArgumentException("Given value not in range (0-5 incl.): " + i);
+                       ((BitAndInt) lastState).i = i;
+                       return lastState;
+               default:
+                       return super.setHighLevelState(lastState, stateID, newHighLevelState);
+               }
+       }
+
        private static class BitAndInt
        {
                Bit bit;
@@ -81,7 +122,11 @@ public class GUIAm2910SP extends SimpleRectangularHardcodedGUIComponent
                        return BitVector.of(X, 3);
                if (i == -2)
                        return BitVector.of(U, 3);
-               // TODO maybe this is the wrong way around
                return BitVector.of((i & 0b100) > 0 ? ONE : ZERO, (i & 0b10) > 0 ? ONE : ZERO, (i & 0b1) > 0 ? ONE : ZERO);
        }
+
+       static
+       {
+               IndirectGUIComponentCreator.setComponentSupplier(GUIAm2910SP.class.getCanonicalName(), (m, p, n) -> new GUIAm2910SP(m, n));
+       }
 }
\ No newline at end of file