From ee5fd615dd7bd1c4035ce489c8531869629c61b0 Mon Sep 17 00:00:00 2001 From: hneemann Date: Wed, 24 Jun 2020 11:14:16 +0200 Subject: [PATCH] fixes a bug in the switch net formation, closes #480 --- .../digital/core/switching/PlainSwitch.java | 22 +- .../wiring/bus/BusModelStateObserver.java | 6 + .../core/wiring/bus/ConnectedBusHandler.java | 33 +- .../digital/integration/TestExamples.java | 4 +- .../dig/test/switch/DmitryVyal_bug480.dig | 430 ++++++++++++++++++ src/test/resources/dig/test/switch/GAL2v3.dig | 16 +- 6 files changed, 493 insertions(+), 18 deletions(-) create mode 100644 src/test/resources/dig/test/switch/DmitryVyal_bug480.dig diff --git a/src/main/java/de/neemann/digital/core/switching/PlainSwitch.java b/src/main/java/de/neemann/digital/core/switching/PlainSwitch.java index d4a20a02a..9818f954e 100644 --- a/src/main/java/de/neemann/digital/core/switching/PlainSwitch.java +++ b/src/main/java/de/neemann/digital/core/switching/PlainSwitch.java @@ -102,7 +102,7 @@ public final class PlainSwitch implements NodeInterface { if (constant != null) return new UniDirectionalSwitch(constant, output1); else - return new RealSwitch(in1, in2); + return new RealSwitch(in1, output1, in2, output2); } } else return new UniDirectionalSwitch(input1, output2); @@ -250,12 +250,16 @@ public final class PlainSwitch implements NodeInterface { */ public static final class RealSwitch implements SwitchModel { private final CommonBusValue input1; + private final ObservableValue output1; private final CommonBusValue input2; + private final ObservableValue output2; private BusModelStateObserver obs; - private RealSwitch(CommonBusValue input1, CommonBusValue input2) { + private RealSwitch(CommonBusValue input1, ObservableValue output1, CommonBusValue input2, ObservableValue output2) { this.input1 = input1; + this.output1 = output1; this.input2 = input2; + this.output2 = output2; } @Override @@ -285,5 +289,19 @@ public final class PlainSwitch implements NodeInterface { public CommonBusValue getInput2() { return input2; } + + /** + * @return the output used by the left hand side pin + */ + public ObservableValue getOutput1() { + return output1; + } + + /** + * @return the output used by the right hand side pin + */ + public ObservableValue getOutput2() { + return output2; + } } } diff --git a/src/main/java/de/neemann/digital/core/wiring/bus/BusModelStateObserver.java b/src/main/java/de/neemann/digital/core/wiring/bus/BusModelStateObserver.java index 095afab49..4a5981efd 100644 --- a/src/main/java/de/neemann/digital/core/wiring/bus/BusModelStateObserver.java +++ b/src/main/java/de/neemann/digital/core/wiring/bus/BusModelStateObserver.java @@ -97,20 +97,24 @@ public final class BusModelStateObserver implements ModelStateObserverTyped { createdHandlers.add(h); h.addNet(s.getInput1()); h.addNet(s.getInput2()); + h.addExclude(s.getOutput1(), s.getOutput2()); netMap.put(s.getInput1(), h); netMap.put(s.getInput2(), h); } else { h2.addNet(s.getInput1()); + h2.addExclude(s.getOutput1(), s.getOutput2()); netMap.put(s.getInput1(), h2); } } else { if (h2 == null) { h1.addNet(s.getInput2()); + h1.addExclude(s.getOutput1(), s.getOutput2()); netMap.put(s.getInput2(), h1); } else { if (h1 != h2) { // merge the nets h1.addNet(h2); + h1.addExcludesFrom(h2); for (CommonBusValue v : h2.getValues()) netMap.put(v, h1); createdHandlers.remove(h2); @@ -118,6 +122,8 @@ public final class BusModelStateObserver implements ModelStateObserverTyped { } } } + for (ConnectedBusHandler h : createdHandlers) + h.removeExcludes(); for (ConnectedBusHandler h : createdHandlers) h.recalculate(); } diff --git a/src/main/java/de/neemann/digital/core/wiring/bus/ConnectedBusHandler.java b/src/main/java/de/neemann/digital/core/wiring/bus/ConnectedBusHandler.java index aeda9068f..dedd5bd80 100644 --- a/src/main/java/de/neemann/digital/core/wiring/bus/ConnectedBusHandler.java +++ b/src/main/java/de/neemann/digital/core/wiring/bus/ConnectedBusHandler.java @@ -19,8 +19,9 @@ import java.util.List; */ public final class ConnectedBusHandler extends AbstractBusHandler { private PinDescription.PullResistor resistor = PinDescription.PullResistor.none; - private ArrayList values; - private ArrayList inputs; + private final ArrayList values; + private final ArrayList inputs; + private final ArrayList excludes; /** * Creates a new instance @@ -31,6 +32,7 @@ public final class ConnectedBusHandler extends AbstractBusHandler { super(obs); values = new ArrayList<>(); inputs = new ArrayList<>(); + excludes = new ArrayList<>(); } /** @@ -100,4 +102,31 @@ public final class ConnectedBusHandler extends AbstractBusHandler { return "ConnectedBusHandler{" + "values=" + values + '}'; } + + /** + * Adds output to the net which are to ignore. + * + * @param output1 output1 + * @param output2 output2 + */ + public void addExclude(ObservableValue output1, ObservableValue output2) { + excludes.add(output1); + excludes.add(output2); + } + + /** + * Adds output which are to ignore by net other. + * + * @param other adds the outputs to ignore from this net + */ + public void addExcludesFrom(ConnectedBusHandler other) { + excludes.addAll(other.excludes); + } + + /** + * Removes all the outputs which are to ignore + */ + public void removeExcludes() { + inputs.removeAll(excludes); + } } diff --git a/src/test/java/de/neemann/digital/integration/TestExamples.java b/src/test/java/de/neemann/digital/integration/TestExamples.java index baf210383..aabf42f51 100644 --- a/src/test/java/de/neemann/digital/integration/TestExamples.java +++ b/src/test/java/de/neemann/digital/integration/TestExamples.java @@ -43,8 +43,8 @@ public class TestExamples extends TestCase { */ public void testTestExamples() throws Exception { File examples = new File(Resources.getRoot(), "/dig/test"); - assertEquals(191, new FileScanner(this::check).scan(examples)); - assertEquals(179, testCasesInFiles); + assertEquals(192, new FileScanner(this::check).scan(examples)); + assertEquals(181, testCasesInFiles); } /** diff --git a/src/test/resources/dig/test/switch/DmitryVyal_bug480.dig b/src/test/resources/dig/test/switch/DmitryVyal_bug480.dig new file mode 100644 index 000000000..f3bec23e0 --- /dev/null +++ b/src/test/resources/dig/test/switch/DmitryVyal_bug480.dig @@ -0,0 +1,430 @@ + + + 1 + + + + RelayDT + + + mirror + true + + + rotation + + + + Label + S4 + + + + + + RelayDT + + + mirror + true + + + rotation + + + + Label + S1 + + + + + + Ground + + + + + RelayDT + + + mirror + true + + + rotation + + + + Label + S2 + + + + + + Ground + + + + + RelayDT + + + mirror + true + + + rotation + + + + Label + S3 + + + + + + Ground + + + + + Ground + + + + + In + + + rotation + + + + Label + A + + + + + + In + + + rotation + + + + Label + B + + + + + + In + + + rotation + + + + Label + C + + + + + + In + + + rotation + + + + Label + D + + + + + + Const + + + Value + 0 + + + + + + Const + + + + + Out + + + rotation + + + + Label + Y + + + + + + Out + + + rotation + + + + Label + X + + + + + + Out + + + rotation + + + + Label + Z + + + + + + Out + + + rotation + + + + Label + U + + + + + + Testcase + + + Label + comb + + + Testdata + + D C B A Y X Z U + + 0 0 0 0 0 0 0 0 + 0 0 0 1 0 0 0 1 + 0 0 1 0 0 0 1 1 + 0 0 1 1 0 0 1 1 + 0 1 0 0 0 1 1 1 + 0 1 0 1 0 1 1 1 + 0 1 1 0 0 1 1 1 + 0 1 1 1 0 1 1 1 + 1 0 0 0 1 1 1 1 + 1 0 0 1 1 1 1 1 + 1 0 1 0 1 1 1 1 + 1 0 1 1 1 1 1 1 + 1 1 0 0 1 1 1 1 + 1 1 0 1 1 1 1 1 + 1 1 1 0 1 1 1 1 + 1 1 1 1 1 1 1 1 + + + + + + + + Testcase + + + Label + #480 + + + Testdata + + D C B A Y X Z U + + 0 0 0 0 0 0 0 0 + 0 0 1 0 0 0 1 1 + 0 0 0 0 0 0 0 0 + 0 0 0 1 0 0 0 1 + 0 0 0 0 0 0 0 0 + 0 1 0 0 0 1 1 1 + 0 0 0 0 0 0 0 0 + 1 0 0 0 1 1 1 1 + 0 0 0 0 0 0 0 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/dig/test/switch/GAL2v3.dig b/src/test/resources/dig/test/switch/GAL2v3.dig index 417f92b30..8c0b73d28 100644 --- a/src/test/resources/dig/test/switch/GAL2v3.dig +++ b/src/test/resources/dig/test/switch/GAL2v3.dig @@ -1,6 +1,7 @@ 1 + Ground @@ -1205,22 +1206,12 @@ FGNFET - - - Blown - true - - + FGNFET - - - Blown - true - - + @@ -5881,4 +5872,5 @@ repeat(8) 1 0 C bits(3,7-n) + \ No newline at end of file