Fixed stack high level state internal return type (BitVector[])
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / GUIram5_12.java
index 7dcea02..adce29a 100644 (file)
@@ -1,9 +1,15 @@
 package net.mograsim.logic.model.am2900.components;
 
-import static net.mograsim.logic.core.types.Bit.*;
+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.Z;
+import static net.mograsim.logic.core.types.Bit.ZERO;
 
 import java.util.Arrays;
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import net.mograsim.logic.core.types.Bit;
 import net.mograsim.logic.core.types.BitVector;
@@ -12,6 +18,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 GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
@@ -20,16 +28,16 @@ public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
        {
                super(model, name, "RAM\n5 x 12 Bit");
                setSize(40, 40);
-               addPin(new Pin(this, "A", 3, 10, 0), Usage.INPUT, Position.BOTTOM);
-               addPin(new Pin(this, "B", 3, 30, 0), Usage.INPUT, Position.BOTTOM);
-               addPin(new Pin(this, "WE", 1, 0, 5), Usage.INPUT, Position.RIGHT);
-               addPin(new Pin(this, "C", 1, 0, 15), Usage.INPUT, Position.RIGHT);
-               addPin(new Pin(this, "Y", 12, 0, 30), Usage.OUTPUT, Position.RIGHT);
-               addPin(new Pin(this, "D", 12, 20, 40), Usage.INPUT, Position.TOP);
+               addPin(new Pin(this, "A", 3, PinUsage.INPUT, 10, 0), Position.BOTTOM);
+               addPin(new Pin(this, "B", 3, PinUsage.INPUT, 30, 0), Position.BOTTOM);
+               addPin(new Pin(this, "WE", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
+               addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
+               addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 0, 30), Position.RIGHT);
+               addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 40), Position.TOP);
        }
 
        @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)
        {
                BitVector[] memC = (BitVector[]) lastState;
                if (memC == null)
@@ -76,4 +84,37 @@ public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
                // TODO maybe this is the wrong way around
                return (bits[0] == ONE ? 4 : 0) + (bits[1] == ONE ? 2 : 0) + (bits[2] == ONE ? 1 : 0);
        }
+
+       Pattern stateIDPattern = Pattern.compile("c(0[10][10]|100)");
+
+       @Override
+       protected Object getHighLevelState(Object state, String stateID)
+       {
+               Matcher m = stateIDPattern.matcher(stateID);
+               if (m.matches())
+                       return ((BitVector[]) state)[Integer.parseInt(m.group(1), 2)];
+               return super.getHighLevelState(state, stateID);
+       }
+
+       @Override
+       protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
+       {
+               Matcher m = stateIDPattern.matcher(stateID);
+               if (m.matches())
+               {
+                       int addr = Integer.parseInt(m.group(1), 2);
+                       BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
+                       if (newHighLevelStateCasted.length() != 12)
+                               throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
+                       BitVector[] memC = (BitVector[]) lastState;
+                       memC[addr] = newHighLevelStateCasted;
+                       return memC;
+               }
+               return super.setHighLevelState(lastState, stateID, newHighLevelState);
+       }
+
+       static
+       {
+               IndirectGUIComponentCreator.setComponentSupplier(GUIram5_12.class.getCanonicalName(), (m, p, n) -> new GUIram5_12(m, n));
+       }
 }
\ No newline at end of file