Removed System.out's
[Mograsim.git] / net.mograsim.logic.ui.am2900 / test / net / mograsim / logic / ui / am2900 / TestableAm2901Impl.java
index a91ee18..5a734b1 100644 (file)
@@ -5,7 +5,9 @@ import static org.junit.jupiter.api.Assertions.fail;
 import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedList;
 import java.util.Objects;
+import java.util.Queue;
 import java.util.Set;
 import java.util.TreeSet;
 
@@ -15,18 +17,14 @@ import net.mograsim.logic.core.timeline.Timeline;
 import net.mograsim.logic.core.types.Bit;
 import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
-import net.mograsim.logic.ui.model.ModelVisitor;
 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.SimpleRectangularGUIGate;
-import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
-import net.mograsim.logic.ui.model.components.SubmodelInterface;
+import net.mograsim.logic.ui.model.components.SubmodelComponent;
 import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
 import net.mograsim.logic.ui.model.wires.GUIWire;
-import net.mograsim.logic.ui.model.wires.WireCrossPoint;
 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 
@@ -45,9 +43,6 @@ public class TestableAm2901Impl implements TestableAm2901
        private BitDisplay F_0, Cn_4, OVR, F3;
        private BitDisplay ORAMn, ORAMn_3, OQn, OQn_3;
 
-       private Set<GUIWire> allWires;
-       private Set<GUIComponent> allComponents;
-
        private Set<String> wireDebugChangeSet;
        private boolean debugWires = false;
        public int debugEventThreshold = 10_000;
@@ -67,7 +62,7 @@ public class TestableAm2901Impl implements TestableAm2901
                        timeline.executeNext();
                        if (!timeline.hasNext())
                        {
-                               System.out.println("run() took " + eventCounter + " events");
+//                             System.out.println("run() took " + eventCounter + " events");
                                return Result.SUCCESS;
                        }
                }
@@ -119,6 +114,7 @@ public class TestableAm2901Impl implements TestableAm2901
                for (String id : am2901.getOutputPinNames())
                {
                        GUIBitDisplay bd = new GUIBitDisplay(viewModel);
+//                     bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
                        new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
                        idDisplayMap.put(id, bd);
                }
@@ -132,19 +128,23 @@ public class TestableAm2901Impl implements TestableAm2901
                        setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getManualSwitch());
                for (var entry : idDisplayMap.entrySet())
                        setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getBitDisplay());
-               // Switch Clock off first
-               C.switchOff();
 
                // Debug code
-               allWires = new HashSet<>();
-               allComponents = new HashSet<>();
-               ModelAccumulator accumulator = new ModelAccumulator();
-               accumulator.visit(viewModel);
-               allWires.forEach(w -> w.addRedrawListener(() ->
+               HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
+               Queue<ViewModel> modelsToIterate = new LinkedList<>();
+               modelsToIterate.add(viewModel);
+               while (modelsToIterate.size() > 0)
+               {
+                       ViewModel model = modelsToIterate.poll();
+                       wiresIncludingSubmodels.addAll(model.getWires());
+                       for (GUIComponent comp : model.getComponents())
+                               if (comp instanceof SubmodelComponent)
+                                       modelsToIterate.offer(((SubmodelComponent) comp).submodel);
+               }
+               wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
                {
                        if (debugWires)
                        {
-                               System.out.println(w);
                                wireDebugChangeSet.add(w.toString());
                        }
                }));
@@ -245,9 +245,9 @@ public class TestableAm2901Impl implements TestableAm2901
        }
 
        @Override
-       public void toogleClock()
+       public void clockOn(boolean isClockOn)
        {
-               C.toggle();
+               C.setState(isClockOn);
        }
 
        @Override
@@ -346,57 +346,22 @@ public class TestableAm2901Impl implements TestableAm2901
                return mutator.toBitVector();
        }
 
-       class ModelAccumulator implements ModelVisitor
+       @Override
+       public void setDirectly(Register r, String val_4_bit)
        {
-               @Override
-               public void visit(GUIWire w)
-               {
-                       allWires.add(w);
-               }
-
-               @Override
-               public void visit(SimpleRectangularGUIGate simpleRectangularGUIGate)
-               {
-                       allComponents.add(simpleRectangularGUIGate);
-               }
-
-               @Override
-               public void visit(SimpleRectangularSubmodelComponent simpleRectangularSubmodelComponent)
-               {
-                       allComponents.add(simpleRectangularSubmodelComponent);
-                       simpleRectangularSubmodelComponent.getWires().forEach(w -> w.accept(this));
-                       simpleRectangularSubmodelComponent.getComponents().forEach(w -> w.accept(this));
-               }
-
-               @Override
-               public void visit(WireCrossPoint wireCrossPoint)
-               {
-                       // nothing
-               }
-
-               @Override
-               public void visit(GUIBitDisplay guiBitDisplay)
-               {
-                       allComponents.add(guiBitDisplay);
-               }
-
-               @Override
-               public void visit(GUIManualSwitch guiManualSwitch)
-               {
-                       allComponents.add(guiManualSwitch);
-               }
+               am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_4_bit));
+       }
 
-               @Override
-               public void visit(SubmodelInterface submodelInterface)
-               {
-                       // nothing
-               }
+       @Override
+       public String getDirectly(Register r)
+       {
+               return am2901.getHighLevelState(regToStateID(r)).toString();
+       }
 
-               @Override
-               public void visit(ViewModel viewModel)
-               {
-                       viewModel.getWires().forEach(w -> w.accept(this));
-                       viewModel.getComponents().forEach(w -> w.accept(this));
-               }
+       private static String regToStateID(Register r)
+       {
+               if (r == Register.Q)
+                       return "qreg.q";
+               return "regs.c" + r.toBitString() + ".q";
        }
 }