Switched mostly to using deserialized component versions
[Mograsim.git] / net.mograsim.logic.ui.am2900 / test / net / mograsim / logic / ui / am2900 / TestableAm2901Impl.java
index 0b1adc2..25f1566 100644 (file)
@@ -3,9 +3,11 @@ package net.mograsim.logic.ui.am2900;
 import static org.junit.jupiter.api.Assertions.fail;
 
 import java.lang.reflect.Field;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Objects;
 import java.util.Queue;
 import java.util.Set;
@@ -19,18 +21,19 @@ import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
 import net.mograsim.logic.ui.model.ViewModel;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.GUIBitDisplay;
 import net.mograsim.logic.ui.model.components.GUIComponent;
-import net.mograsim.logic.ui.model.components.GUIManualSwitch;
-import net.mograsim.logic.ui.model.components.SubmodelComponent;
+import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
+import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
 import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
+import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
 import net.mograsim.logic.ui.model.wires.GUIWire;
+import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 
 public class TestableAm2901Impl implements TestableAm2901
 {
-       private GUIAm2901 am2901;
+       private GUIComponent am2901;
        private Timeline timeline;
        private ManualSwitch I8, I7, I6, I5, I4, I3, I2, I1, I0;
        private ManualSwitch C;
@@ -100,10 +103,21 @@ public class TestableAm2901Impl implements TestableAm2901
        {
                // Create view model
                ViewModelModifiable viewModel = new ViewModelModifiable();
+               // TODO replace with deserialized version as soon as high level states work for deserialized components
                am2901 = new GUIAm2901(viewModel);
+//             am2901 = IndirectGUIComponentCreator.createComponent(viewModel, "GUIAm2901");
+               // guess which pins are outputs and which are inputs
+               // TODO this code exists three times... but it seems too "hacky" to put it in a helper class
+               List<String> inputPinNames = new ArrayList<>();
+               List<String> outputPinNames = new ArrayList<>();
+               for (Pin p : am2901.getPins().values())
+                       if (p.getRelX() == 0)
+                               inputPinNames.add(p.name);
+                       else
+                               outputPinNames.add(p.name);
                // Get switches
                HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
-               for (String id : am2901.getInputPinNames())
+               for (String id : inputPinNames)
                {
                        GUIManualSwitch sw = new GUIManualSwitch(viewModel);
                        new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
@@ -111,7 +125,7 @@ public class TestableAm2901Impl implements TestableAm2901
                }
                // Get displays
                HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
-               for (String id : am2901.getOutputPinNames())
+               for (String id : outputPinNames)
                {
                        GUIBitDisplay bd = new GUIBitDisplay(viewModel);
 //                     bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
@@ -181,8 +195,7 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setReg_A(String val_4_bit)
        {
-               // 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());
+               var bits = BitVector.parseMSBFirst(val_4_bit);
                A3.setToValueOf(bits.getBit(3));
                A2.setToValueOf(bits.getBit(2));
                A1.setToValueOf(bits.getBit(1));
@@ -192,8 +205,7 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setReg_B(String val_4_bit)
        {
-               // 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());
+               var bits = BitVector.parseMSBFirst(val_4_bit);
                B3.setToValueOf(bits.getBit(3));
                B2.setToValueOf(bits.getBit(2));
                B1.setToValueOf(bits.getBit(1));
@@ -215,8 +227,7 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setD(String val_4_bit)
        {
-               // 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());
+               var bits = BitVector.parseMSBFirst(val_4_bit);
                D4.setToValueOf(bits.getBit(3));
                D3.setToValueOf(bits.getBit(2));
                D2.setToValueOf(bits.getBit(1));
@@ -320,8 +331,7 @@ public class TestableAm2901Impl implements TestableAm2901
                var y2 = Y3.getDisplayedValue();
                var y1 = Y2.getDisplayedValue();
                var y0 = Y1.getDisplayedValue();
-               // 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();
+               return y0.concat(y1).concat(y2).concat(y3).toBitStringMSBFirst();
        }
 
        private void setField(String name, Object value)
@@ -353,15 +363,13 @@ public class TestableAm2901Impl implements TestableAm2901
        @Override
        public void setDirectly(Register r, String 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()));
+               am2901.setHighLevelState(regToStateID(r), BitVector.parseMSBFirst(val_4_bit));
        }
 
        @Override
        public String getDirectly(Register r)
        {
-               // 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();
+               return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toBitStringMSBFirst();
        }
 
        private static String regToStateID(Register r)