Fixed the same bug in some Am2900-specific ModelComponents
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 7 Oct 2019 00:05:49 +0000 (02:05 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 7 Oct 2019 00:05:49 +0000 (02:05 +0200)
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modeldff12.java
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modelram5_12.java
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910RegCntr.java
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910SP.java

index 14500d5..4abc5d5 100644 (file)
@@ -34,12 +34,7 @@ public class Modeldff12 extends SimpleRectangularHardcodedModelComponent
        @Override
        public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
        {
-               Bit[] QC = (Bit[]) lastState;
-               if (QC == null)
-               {
-                       QC = new Bit[13];
-                       Arrays.fill(QC, U);
-               }
+               Bit[] QC = castAndInitState(lastState);
 
                Bit CVal = readEnds.get("C").getValue();
 
@@ -55,29 +50,44 @@ public class Modeldff12 extends SimpleRectangularHardcodedModelComponent
        @Override
        protected Object getHighLevelState(Object state, String stateID)
        {
+               Bit[] QC = castAndInitState(state);
+
                switch (stateID)
                {
                case "q":
-                       return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12));
+                       return BitVector.of(Arrays.copyOfRange(QC, 0, 12));
                default:
-                       return super.getHighLevelState(state, stateID);
+                       return super.getHighLevelState(QC, stateID);
                }
        }
 
        @Override
        protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
        {
+               Bit[] QC = castAndInitState(lastState);
+
                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;
+                       System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12);
+                       return QC;
                default:
-                       return super.setHighLevelState(lastState, stateID, newHighLevelState);
+                       return super.setHighLevelState(QC, stateID, newHighLevelState);
+               }
+       }
+
+       private static Bit[] castAndInitState(Object state)
+       {
+               Bit[] QC = (Bit[]) state;
+               if (QC == null)
+               {
+                       QC = new Bit[13];
+                       Arrays.fill(QC, U);
                }
+               return QC;
        }
 
        static
index 16c2077..3656241 100644 (file)
@@ -41,13 +41,8 @@ public class Modelram5_12 extends SimpleRectangularHardcodedModelComponent
        @Override
        public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
        {
-               BitVector[] memC = (BitVector[]) lastState;
-               if (memC == null)
-               {
-                       memC = new BitVector[6];
-                       Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
-                       memC[5] = BitVector.of(U);
-               }
+               BitVector[] memC = castAndInitState(lastState);
+
                BitVector CVal = readEnds.get("C").getValues();
                BitVector oldC = memC[5];
                // TODO is the timing right?
@@ -91,15 +86,19 @@ public class Modelram5_12 extends SimpleRectangularHardcodedModelComponent
        @Override
        protected Object getHighLevelState(Object state, String stateID)
        {
+               BitVector[] memC = castAndInitState(state);
+
                Matcher m = stateIDPattern.matcher(stateID);
                if (m.matches())
-                       return ((BitVector[]) state)[Integer.parseInt(m.group(1), 2)];
-               return super.getHighLevelState(state, stateID);
+                       return memC[Integer.parseInt(m.group(1), 2)];
+               return super.getHighLevelState(memC, stateID);
        }
 
        @Override
        protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
        {
+               BitVector[] memC = castAndInitState(lastState);
+
                Matcher m = stateIDPattern.matcher(stateID);
                if (m.matches())
                {
@@ -107,11 +106,22 @@ public class Modelram5_12 extends SimpleRectangularHardcodedModelComponent
                        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);
+               return super.setHighLevelState(memC, stateID, newHighLevelState);
+       }
+
+       private static BitVector[] castAndInitState(Object state)
+       {
+               BitVector[] memC = (BitVector[]) state;
+               if (memC == null)
+               {
+                       memC = new BitVector[6];
+                       Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
+                       memC[5] = BitVector.of(U);
+               }
+               return memC;
        }
 
        static
index 0c101bb..7bdc703 100644 (file)
@@ -37,12 +37,7 @@ public class ModelAm2910RegCntr extends SimpleRectangularHardcodedModelComponent
        @Override
        public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
        {
-               Bit[] QC = (Bit[]) lastState;
-               if (QC == null)
-               {
-                       QC = new Bit[13];
-                       Arrays.fill(QC, U);
-               }
+               Bit[] QC = castAndInitState(lastState);
 
                ReadEnd D = readEnds.get("D");
                ReadEnd _RLD = readEnds.get("_RLD");
@@ -80,29 +75,44 @@ public class ModelAm2910RegCntr extends SimpleRectangularHardcodedModelComponent
        @Override
        protected Object getHighLevelState(Object state, String stateID)
        {
+               Bit[] QC = castAndInitState(state);
+
                switch (stateID)
                {
                case "q":
-                       return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12));
+                       return BitVector.of(Arrays.copyOfRange(QC, 0, 12));
                default:
-                       return super.getHighLevelState(state, stateID);
+                       return super.getHighLevelState(QC, stateID);
                }
        }
 
        @Override
        protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
        {
+               Bit[] QC = castAndInitState(lastState);
+
                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;
+                       System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12);
+                       return QC;
                default:
-                       return super.setHighLevelState(lastState, stateID, newHighLevelState);
+                       return super.setHighLevelState(QC, stateID, newHighLevelState);
+               }
+       }
+
+       private static Bit[] castAndInitState(Object lastState)
+       {
+               Bit[] QC = (Bit[]) lastState;
+               if (QC == null)
+               {
+                       QC = new Bit[13];
+                       Arrays.fill(QC, U);
                }
+               return QC;
        }
 
        static
index a99fe0d..d9d1a60 100644 (file)
@@ -37,13 +37,7 @@ public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent
        @Override
        public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
        {
-               BitAndInt SPC = (BitAndInt) lastState;
-               if (SPC == null)
-               {
-                       SPC = new BitAndInt();
-                       SPC.bit = U;
-                       SPC.i = -2;
-               }
+               BitAndInt SPC = castAndInitState(lastState);
                int SP = SPC.i;
 
                Bit STKI0Val = readEnds.get("STKI0").getValue();
@@ -77,18 +71,22 @@ public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent
        @Override
        protected Object getHighLevelState(Object state, String stateID)
        {
+               BitAndInt SPC = castAndInitState(state);
+
                switch (stateID)
                {
                case "q":
-                       return getAsBitVector(((BitAndInt) state).i);
+                       return getAsBitVector(SPC.i);
                default:
-                       return super.getHighLevelState(state, stateID);
+                       return super.getHighLevelState(SPC, stateID);
                }
        }
 
        @Override
        protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
        {
+               BitAndInt SPC = castAndInitState(lastState);
+
                switch (stateID)
                {
                case "q":
@@ -102,11 +100,23 @@ public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent
                                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;
+                       SPC.i = i;
+                       return SPC;
                default:
-                       return super.setHighLevelState(lastState, stateID, newHighLevelState);
+                       return super.setHighLevelState(SPC, stateID, newHighLevelState);
+               }
+       }
+
+       private static BitAndInt castAndInitState(Object lastState)
+       {
+               BitAndInt SPC = (BitAndInt) lastState;
+               if (SPC == null)
+               {
+                       SPC = new BitAndInt();
+                       SPC.bit = U;
+                       SPC.i = -2;
                }
+               return SPC;
        }
 
        private static class BitAndInt