GUIComponents now have names
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / model / components / mi / nandbased / GUIram4.java
index d982f01..38b7516 100644 (file)
@@ -3,21 +3,22 @@ package net.mograsim.logic.ui.model.components.mi.nandbased;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
+import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 
 public class GUIram4 extends SimpleRectangularSubmodelComponent
 {
-       private GUIram2 cell00;
-       private GUIram2 cell01;
-       private GUIram2 cell10;
-       private GUIram2 cell11;
-
        public GUIram4(ViewModelModifiable model)
        {
-               super(model, 1, "GUIram4");
+               this(model, null);
+       }
+
+       public GUIram4(ViewModelModifiable model, String name)
+       {
+               super(model, 1, "GUIram4", name);
                setSubmodelScale(.1);
                setInputPins("A0", "A1", "A2", "A3", "B0", "B1", "B2", "B3", "WE", "D1", "D2", "D3", "D4");
                setOutputPins("QA1", "QA2", "QA3", "QA4", "QB1", "QB2", "QB3", "QB4");
@@ -53,10 +54,10 @@ public class GUIram4 extends SimpleRectangularSubmodelComponent
                GUIdemux2   demuxA   = new GUIdemux2  (submodelModifiable);
                GUIdemux2   demuxB   = new GUIdemux2  (submodelModifiable);
                GUIand41    weAndB   = new GUIand41   (submodelModifiable);
-                 cell00   = new GUIram2    (submodelModifiable);
-                 cell01   = new GUIram2    (submodelModifiable);
-                 cell10   = new GUIram2    (submodelModifiable);
-                 cell11   = new GUIram2    (submodelModifiable);
+               GUIram2     cell00   = new GUIram2    (submodelModifiable);
+               GUIram2     cell01   = new GUIram2    (submodelModifiable);
+               GUIram2     cell10   = new GUIram2    (submodelModifiable);
+               GUIram2     cell11   = new GUIram2    (submodelModifiable);
                GUIand41    andB00   = new GUIand41   (submodelModifiable);
                GUIandor414 andorB01 = new GUIandor414(submodelModifiable);
                GUIandor414 andorB10 = new GUIandor414(submodelModifiable);
@@ -285,128 +286,86 @@ public class GUIram4 extends SimpleRectangularSubmodelComponent
                new GUIWire(submodelModifiable, andorA11.getPin("Y3"), QA3                           , new Point(200, 760), new Point(200, 890), new Point(320, 890), new Point(320, 250));
                new GUIWire(submodelModifiable, andorA11.getPin("Y4"), QA4                           , new Point(195, 770), new Point(195, 895), new Point(325, 895), new Point(325, 350));
                //@formatter:on
+
+               addHighLevelStateSubcomponentID("c00", cell00);
+               addHighLevelStateSubcomponentID("c01", cell01);
+               addHighLevelStateSubcomponentID("c10", cell10);
+               addHighLevelStateSubcomponentID("c11", cell11);
+               addAtomicHighLevelStateID("q");
        }
 
        @Override
-       public Object getHighLevelState(String stateID)
+       public void setAtomicHighLevelState(String stateID, Object newState)
        {
                switch (stateID)
                {
                case "q":
-                       BitVector q0000 = (BitVector) getHighLevelState("c0000.q");
-                       BitVector q0001 = (BitVector) getHighLevelState("c0001.q");
-                       BitVector q0010 = (BitVector) getHighLevelState("c0010.q");
-                       BitVector q0011 = (BitVector) getHighLevelState("c0011.q");
-                       BitVector q0100 = (BitVector) getHighLevelState("c0100.q");
-                       BitVector q0101 = (BitVector) getHighLevelState("c0101.q");
-                       BitVector q0110 = (BitVector) getHighLevelState("c0110.q");
-                       BitVector q0111 = (BitVector) getHighLevelState("c0111.q");
-                       BitVector q1000 = (BitVector) getHighLevelState("c1000.q");
-                       BitVector q1001 = (BitVector) getHighLevelState("c1001.q");
-                       BitVector q1010 = (BitVector) getHighLevelState("c1010.q");
-                       BitVector q1011 = (BitVector) getHighLevelState("c1011.q");
-                       BitVector q1100 = (BitVector) getHighLevelState("c1100.q");
-                       BitVector q1101 = (BitVector) getHighLevelState("c1101.q");
-                       BitVector q1110 = (BitVector) getHighLevelState("c1110.q");
-                       BitVector q1111 = (BitVector) getHighLevelState("c1111.q");
-                       return q0000.concat(q0001).concat(q0010).concat(q0011).concat(q0100).concat(q0101).concat(q0110).concat(q0111).concat(q1000)
-                                       .concat(q1001).concat(q1010).concat(q1011).concat(q1100).concat(q1101).concat(q1110).concat(q1111);
+                       BitVector newStateCasted = (BitVector) newState;
+                       setHighLevelState("c00.q", newStateCasted.subVector(0, 16));
+                       setHighLevelState("c01.q", newStateCasted.subVector(16, 32));
+                       setHighLevelState("c10.q", newStateCasted.subVector(32, 48));
+                       setHighLevelState("c11.q", newStateCasted.subVector(48, 64));
+                       break;
                default:
-                       int indexOfDot = stateID.indexOf('.');
-                       if (indexOfDot != -1)
-                       {
-                               String cellID = stateID.substring(0, indexOfDot);
-                               switch (cellID)
-                               {
-                               case "c0000":
-                               case "c0001":
-                               case "c0010":
-                               case "c0011":
-                                       return cell00.getHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot));
-                               case "c0100":
-                               case "c0101":
-                               case "c0110":
-                               case "c0111":
-                                       return cell01.getHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot));
-                               case "c1000":
-                               case "c1001":
-                               case "c1010":
-                               case "c1011":
-                                       return cell10.getHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot));
-                               case "c1100":
-                               case "c1101":
-                               case "c1110":
-                               case "c1111":
-                                       return cell11.getHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot));
-                               default:
-                                       return super.getHighLevelState(stateID);
-                               }
-                       }
-                       return super.getHighLevelState(stateID);
+                       // should not happen because we tell SubmodelComponent to only allow these state IDs.
+                       throw new IllegalStateException("Illegal atomic state ID: " + stateID);
                }
        }
 
        @Override
-       public void setHighLevelState(String stateID, Object newState)
+       protected void setSubcomponentHighLevelState(String subcomponentID, String subcomponentHighLevelStateID, Object newState)
+       {
+               if (checkSubcomponentID(subcomponentID))
+                       setHighLevelState(translateDirectCellAccess(subcomponentID, subcomponentHighLevelStateID), newState);
+               else
+                       super.setSubcomponentHighLevelState(subcomponentID, subcomponentHighLevelStateID, newState);
+
+       }
+
+       @Override
+       public Object getAtomicHighLevelState(String stateID)
        {
                switch (stateID)
                {
                case "q":
-                       BitVector newStateCasted = (BitVector) newState;
-                       setHighLevelState("c0000.q", newStateCasted.subVector(0, 4));
-                       setHighLevelState("c0001.q", newStateCasted.subVector(4, 8));
-                       setHighLevelState("c0010.q", newStateCasted.subVector(8, 12));
-                       setHighLevelState("c0011.q", newStateCasted.subVector(12, 16));
-                       setHighLevelState("c0100.q", newStateCasted.subVector(16, 20));
-                       setHighLevelState("c0101.q", newStateCasted.subVector(20, 24));
-                       setHighLevelState("c0110.q", newStateCasted.subVector(24, 28));
-                       setHighLevelState("c0111.q", newStateCasted.subVector(28, 32));
-                       setHighLevelState("c1000.q", newStateCasted.subVector(32, 36));
-                       setHighLevelState("c1001.q", newStateCasted.subVector(36, 40));
-                       setHighLevelState("c1010.q", newStateCasted.subVector(40, 44));
-                       setHighLevelState("c1011.q", newStateCasted.subVector(44, 48));
-                       setHighLevelState("c1100.q", newStateCasted.subVector(48, 52));
-                       setHighLevelState("c1101.q", newStateCasted.subVector(52, 56));
-                       setHighLevelState("c1110.q", newStateCasted.subVector(56, 60));
-                       setHighLevelState("c1111.q", newStateCasted.subVector(60, 64));
-                       break;
+                       BitVector q00 = (BitVector) getHighLevelState("c00.q");
+                       BitVector q01 = (BitVector) getHighLevelState("c01.q");
+                       BitVector q10 = (BitVector) getHighLevelState("c10.q");
+                       BitVector q11 = (BitVector) getHighLevelState("c11.q");
+                       return q00.concat(q01).concat(q10).concat(q11);
                default:
-                       int indexOfDot = stateID.indexOf('.');
-                       if (indexOfDot != -1)
-                       {
-                               String cellID = stateID.substring(0, indexOfDot);
-                               switch (cellID)
-                               {
-                               case "c0000":
-                               case "c0001":
-                               case "c0010":
-                               case "c0011":
-                                       cell00.setHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot), newState);
-                                       break;
-                               case "c0100":
-                               case "c0101":
-                               case "c0110":
-                               case "c0111":
-                                       cell01.setHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot), newState);
-                                       break;
-                               case "c1000":
-                               case "c1001":
-                               case "c1010":
-                               case "c1011":
-                                       cell10.setHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot), newState);
-                                       break;
-                               case "c1100":
-                               case "c1101":
-                               case "c1110":
-                               case "c1111":
-                                       cell11.setHighLevelState('c' + cellID.substring(3, 5) + stateID.substring(indexOfDot), newState);
-                                       break;
-                               default:
-                                       super.setHighLevelState(stateID, newState);
-                                       break;
-                               }
-                       } else
-                               super.setHighLevelState(stateID, newState);
+                       // should not happen because we tell SubmodelComponent to only allow these state IDs.
+                       throw new IllegalStateException("Illegal atomic state ID: " + stateID);
                }
        }
+
+       @Override
+       protected Object getSubcomponentHighLevelState(String subcomponentID, String subcomponentHighLevelStateID)
+       {
+               if (checkSubcomponentID(subcomponentID))
+                       return getHighLevelState(translateDirectCellAccess(subcomponentID, subcomponentHighLevelStateID));
+               return super.getSubcomponentHighLevelState(subcomponentID, subcomponentHighLevelStateID);
+       }
+
+       private static String translateDirectCellAccess(String subcomponentID, String subcomponentHighLevelStateID)
+       {
+               return 'c' + subcomponentID.substring(3, 5) + "." + subcomponentID.substring(0, 3) + '.' + subcomponentHighLevelStateID;
+       }
+
+       private static boolean checkSubcomponentID(String subcomponentID)
+       {
+               if (subcomponentID.length() != 5 || subcomponentID.charAt(0) != 'c')
+                       return false;
+               char addr3 = subcomponentID.charAt(1);
+               char addr2 = subcomponentID.charAt(2);
+               char addr1 = subcomponentID.charAt(3);
+               char addr0 = subcomponentID.charAt(4);
+               return (addr3 == '0' || addr3 == '1') || (addr2 == '0' || addr2 == '1') || (addr1 == '0' || addr1 == '1')
+                               || (addr0 == '0' || addr0 == '1');
+       }
+
+       static
+       {
+               IndirectGUIComponentCreator.setComponentSupplier(GUIram4.class.getCanonicalName(), (m, p, n) -> new GUIram4(m, n));
+       }
 }
\ No newline at end of file