X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui.am2900%2Ftest%2Fnet%2Fmograsim%2Flogic%2Fui%2Fam2900%2FTestableAm2901Impl.java;h=25f15669237f837928c773a569d8c57dbe5d3b1b;hb=0885062ad73925732b1a4e4c79f910dec9862fd3;hp=a91ee18f47b4796f770003c3cfd49becc1a1b56d;hpb=b37ba7609a925cc945bbac0f6ead619d07912238;p=Mograsim.git diff --git a/net.mograsim.logic.ui.am2900/test/net/mograsim/logic/ui/am2900/TestableAm2901Impl.java b/net.mograsim.logic.ui.am2900/test/net/mograsim/logic/ui/am2900/TestableAm2901Impl.java index a91ee18f..25f15669 100644 --- a/net.mograsim.logic.ui.am2900/test/net/mograsim/logic/ui/am2900/TestableAm2901Impl.java +++ b/net.mograsim.logic.ui.am2900/test/net/mograsim/logic/ui/am2900/TestableAm2901Impl.java @@ -3,9 +3,13 @@ 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; import java.util.TreeSet; @@ -15,24 +19,21 @@ 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.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.WireCrossPoint; +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; @@ -45,9 +46,6 @@ public class TestableAm2901Impl implements TestableAm2901 private BitDisplay F_0, Cn_4, OVR, F3; private BitDisplay ORAMn, ORAMn_3, OQn, OQn_3; - private Set allWires; - private Set allComponents; - private Set wireDebugChangeSet; private boolean debugWires = false; public int debugEventThreshold = 10_000; @@ -67,7 +65,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; } } @@ -105,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 inputPinNames = new ArrayList<>(); + List 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 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()); @@ -116,9 +125,10 @@ public class TestableAm2901Impl implements TestableAm2901 } // Get displays HashMap 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())); new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin()); idDisplayMap.put(id, bd); } @@ -132,19 +142,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 wiresIncludingSubmodels = new HashSet<>(); + Queue 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()); } })); @@ -181,21 +195,21 @@ 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)); + var bits = BitVector.parseMSBFirst(val_4_bit); + 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)); + var bits = BitVector.parseMSBFirst(val_4_bit); + B3.setToValueOf(bits.getBit(3)); + B2.setToValueOf(bits.getBit(2)); + B1.setToValueOf(bits.getBit(1)); + B0.setToValueOf(bits.getBit(0)); } @Override @@ -213,11 +227,11 @@ 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)); + var bits = BitVector.parseMSBFirst(val_4_bit); + D4.setToValueOf(bits.getBit(3)); + D3.setToValueOf(bits.getBit(2)); + D2.setToValueOf(bits.getBit(1)); + D1.setToValueOf(bits.getBit(0)); } @Override @@ -245,9 +259,9 @@ public class TestableAm2901Impl implements TestableAm2901 } @Override - public void toogleClock() + public void clockOn(boolean isClockOn) { - C.toggle(); + C.setState(isClockOn); } @Override @@ -317,7 +331,7 @@ 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(); + return y0.concat(y1).concat(y2).concat(y3).toBitStringMSBFirst(); } private void setField(String name, Object value) @@ -346,57 +360,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.parseMSBFirst(val_4_bit)); + } - @Override - public void visit(SubmodelInterface submodelInterface) - { - // nothing - } + @Override + public String getDirectly(Register r) + { + return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toBitStringMSBFirst(); + } - @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"; } }