Made Am2901 tests work again
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 30 Jun 2019 16:28:53 +0000 (18:28 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 30 Jun 2019 16:28:53 +0000 (18:28 +0200)
net.mograsim.logic.ui.am2900/src/net/mograsim/logic/ui/model/components/mi/nandbased/GUIram4.java
net.mograsim.logic.ui.am2900/test/net/mograsim/logic/ui/am2900/TestableAm2901Impl.java

index cdf9b71..ab04a40 100644 (file)
@@ -10,11 +10,6 @@ import net.mograsim.logic.ui.model.wires.WireCrossPoint;
 
 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");
@@ -53,10 +48,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);
@@ -287,8 +282,8 @@ public class GUIram4 extends SimpleRectangularSubmodelComponent
                //@formatter:on
 
                addHighLevelStateSubcomponentID("c00", cell00);
-               addHighLevelStateSubcomponentID("c10", cell01);
-               addHighLevelStateSubcomponentID("c01", cell10);
+               addHighLevelStateSubcomponentID("c01", cell01);
+               addHighLevelStateSubcomponentID("c10", cell10);
                addHighLevelStateSubcomponentID("c11", cell11);
                addAtomicHighLevelStateID("q");
        }
@@ -314,36 +309,11 @@ public class GUIram4 extends SimpleRectangularSubmodelComponent
        @Override
        protected void setSubcomponentHighLevelState(String subcomponentID, String subcomponentHighLevelStateID, Object newState)
        {
-               switch (subcomponentID)
-               {
-               case "c0000":
-               case "c0001":
-               case "c0010":
-               case "c0011":
-                       cell00.setHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID, newState);
-                       break;
-               case "c1000":
-               case "c1001":
-               case "c1010":
-               case "c1011":
-                       cell01.setHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID, newState);
-                       break;
-               case "c0100":
-               case "c0101":
-               case "c0110":
-               case "c0111":
-                       cell10.setHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID, newState);
-                       break;
-               case "c1100":
-               case "c1101":
-               case "c1110":
-               case "c1111":
-                       cell11.setHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID, newState);
-                       break;
-               default:
+               if (checkSubcomponentID(subcomponentID))
+                       setHighLevelState(translateDirectCellAccess(subcomponentID, subcomponentHighLevelStateID), newState);
+               else
                        super.setSubcomponentHighLevelState(subcomponentID, subcomponentHighLevelStateID, newState);
-                       break;
-               }
+
        }
 
        @Override
@@ -366,30 +336,25 @@ public class GUIram4 extends SimpleRectangularSubmodelComponent
        @Override
        protected Object getSubcomponentHighLevelState(String subcomponentID, String subcomponentHighLevelStateID)
        {
-               switch (subcomponentID)
-               {
-               case "c0000":
-               case "c0001":
-               case "c0010":
-               case "c0011":
-                       return cell00.getHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID);
-               case "c0100":
-               case "c0101":
-               case "c0110":
-               case "c0111":
-                       return cell01.getHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID);
-               case "c1000":
-               case "c1001":
-               case "c1010":
-               case "c1011":
-                       return cell10.getHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID);
-               case "c1100":
-               case "c1101":
-               case "c1110":
-               case "c1111":
-                       return cell11.getHighLevelState('c' + subcomponentID.substring(3, 5) + "." + subcomponentHighLevelStateID);
-               default:
-                       return super.getSubcomponentHighLevelState(subcomponentID, 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');
        }
 }
\ No newline at end of file
index 5a734b1..0b1adc2 100644 (file)
@@ -181,21 +181,23 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setReg_A(String val_4_bit)
        {
-               var bits = BitVector.parse(val_4_bit);
-               A3.setToValueOf(bits.getBit(0));
-               A2.setToValueOf(bits.getBit(1));
-               A1.setToValueOf(bits.getBit(2));
-               A0.setToValueOf(bits.getBit(3));
+               // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+               var bits = BitVector.parse(new StringBuilder(val_4_bit).reverse().toString());
+               A3.setToValueOf(bits.getBit(3));
+               A2.setToValueOf(bits.getBit(2));
+               A1.setToValueOf(bits.getBit(1));
+               A0.setToValueOf(bits.getBit(0));
        }
 
        @Override
        public void setReg_B(String val_4_bit)
        {
-               var bits = BitVector.parse(val_4_bit);
-               B3.setToValueOf(bits.getBit(0));
-               B2.setToValueOf(bits.getBit(1));
-               B1.setToValueOf(bits.getBit(2));
-               B0.setToValueOf(bits.getBit(3));
+               // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+               var bits = BitVector.parse(new StringBuilder(val_4_bit).reverse().toString());
+               B3.setToValueOf(bits.getBit(3));
+               B2.setToValueOf(bits.getBit(2));
+               B1.setToValueOf(bits.getBit(1));
+               B0.setToValueOf(bits.getBit(0));
        }
 
        @Override
@@ -213,11 +215,12 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setD(String val_4_bit)
        {
-               var bits = BitVector.parse(val_4_bit);
-               D4.setToValueOf(bits.getBit(0));
-               D3.setToValueOf(bits.getBit(1));
-               D2.setToValueOf(bits.getBit(2));
-               D1.setToValueOf(bits.getBit(3));
+               // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+               var bits = BitVector.parse(new StringBuilder(val_4_bit).reverse().toString());
+               D4.setToValueOf(bits.getBit(3));
+               D3.setToValueOf(bits.getBit(2));
+               D2.setToValueOf(bits.getBit(1));
+               D1.setToValueOf(bits.getBit(0));
        }
 
        @Override
@@ -317,7 +320,8 @@ public class TestableAm2901Impl implements TestableAm2901
                var y2 = Y3.getDisplayedValue();
                var y1 = Y2.getDisplayedValue();
                var y0 = Y1.getDisplayedValue();
-               return y3.concat(y2).concat(y1).concat(y0).toString();
+               // reverse because BitVector.toString() returns the LSBit at the beginning and the MSBit at the end
+               return new StringBuilder(y0.concat(y1).concat(y2).concat(y3).toString()).reverse().toString();
        }
 
        private void setField(String name, Object value)
@@ -349,13 +353,15 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setDirectly(Register r, String val_4_bit)
        {
-               am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_4_bit));
+               // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+               am2901.setHighLevelState(regToStateID(r), BitVector.parse(new StringBuilder(val_4_bit).reverse().toString()));
        }
 
        @Override
        public String getDirectly(Register r)
        {
-               return am2901.getHighLevelState(regToStateID(r)).toString();
+               // reverse because BitVector.toString() returns the LSBit at the beginning and the MSBit at the end
+               return new StringBuilder(am2901.getHighLevelState(regToStateID(r)).toString()).reverse().toString();
        }
 
        private static String regToStateID(Register r)