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 dc7cfd705..477b34d3c 100644 --- a/src/main/java/de/neemann/digital/core/switching/PlainSwitch.java +++ b/src/main/java/de/neemann/digital/core/switching/PlainSwitch.java @@ -18,7 +18,7 @@ public final class PlainSwitch implements NodeInterface { /** * Defines a direction for the switch. NO means no direction is given, the switch is bidirectional. */ - public enum Unidirectional { NO, FROM1TO2, FROM2TO1 } + public enum Unidirectional {NO, FROM1TO2, FROM2TO1} private final ObservableValue output1; private final ObservableValue output2; @@ -67,7 +67,7 @@ public final class PlainSwitch implements NodeInterface { input2.addObserverToValue(this).checkBits(bits, null); switch (unidirectional) { case NO: - switchModel = createSwitchModel(input1, input2, output1, output2, true); + switchModel = createSwitchModel(input1, input2, output1, output2, true, true); break; case FROM1TO2: switchModel = new UniDirectionalSwitch(input1, output2); @@ -82,22 +82,25 @@ public final class PlainSwitch implements NodeInterface { static SwitchModel createSwitchModel( ObservableValue input1, ObservableValue input2, ObservableValue output1, ObservableValue output2, - boolean setOpenContactToHighZ) throws NodeException { + boolean setOpenContactToHighZ, boolean optimizeConstants) throws NodeException { if (input1 instanceof CommonBusValue) { if (input2 instanceof CommonBusValue) { final CommonBusValue in1 = (CommonBusValue) input1; final CommonBusValue in2 = (CommonBusValue) input2; - ObservableValue constant = in1.searchConstant(); - if (constant != null) - return new UniDirectionalSwitch(constant, output2); - else { - constant = in2.searchConstant(); + if (optimizeConstants) { + ObservableValue constant = in1.searchConstant(); if (constant != null) - return new UniDirectionalSwitch(constant, output1); - else - return new RealSwitch(in1, in2); - } + return new UniDirectionalSwitch(constant, output2); + else { + constant = in2.searchConstant(); + if (constant != null) + return new UniDirectionalSwitch(constant, output1); + else + return new RealSwitch(in1, in2); + } + } else + return new RealSwitch(in1, in2); } else return new UniDirectionalSwitch(input1, output2); } else { diff --git a/src/main/java/de/neemann/digital/core/switching/PlainSwitchDT.java b/src/main/java/de/neemann/digital/core/switching/PlainSwitchDT.java index 065bf6d85..8d3606b3c 100644 --- a/src/main/java/de/neemann/digital/core/switching/PlainSwitchDT.java +++ b/src/main/java/de/neemann/digital/core/switching/PlainSwitchDT.java @@ -42,11 +42,11 @@ public final class PlainSwitchDT implements NodeInterface { if (inA != null && inB != null) { inB.addObserverToValue(this).checkBits(bits, null); - s1 = PlainSwitch.createSwitchModel(inA, inB, outputA, outputB, inC == null); + s1 = PlainSwitch.createSwitchModel(inA, inB, outputA, outputB, inC == null, false); } if (inA != null && inC != null) { inC.addObserverToValue(this).checkBits(bits, null); - s2 = PlainSwitch.createSwitchModel(inA, inC, outputA, outputC, inB == null); + s2 = PlainSwitch.createSwitchModel(inA, inC, outputA, outputC, inB == null, false); } } diff --git a/src/test/java/de/neemann/digital/integration/TestExamples.java b/src/test/java/de/neemann/digital/integration/TestExamples.java index 425ca85e9..37c5ab78b 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(152, new FileScanner(this::check).scan(examples)); - assertEquals(150, testCasesInFiles); + assertEquals(153, new FileScanner(this::check).scan(examples)); + assertEquals(152, testCasesInFiles); } /** diff --git a/src/test/resources/dig/test/switch/const3.dig b/src/test/resources/dig/test/switch/const3.dig new file mode 100644 index 000000000..dfca9d09c --- /dev/null +++ b/src/test/resources/dig/test/switch/const3.dig @@ -0,0 +1,242 @@ + + + 1 + + + + Text + + + Description + see issue #255 + + + + + + Const + + + + + RelayDT + + + rotation + + + + + + + Ground + + + + + In + + + Label + A_2 + + + + + + Testcase + + + Testdata + + A_2 Y_2 + 0 0 + 1 1 + 0 0 + 1 1 + + + + + + + + Out + + + Label + Y_2 + + + + + + Const + + + rotation + + + + Value + 0 + + + + + + PullDown + + + + + Const + + + + + RelayDT + + + rotation + + + + + + + Ground + + + + + In + + + Label + A_1 + + + + + + Testcase + + + Testdata + + A_1 Y_1 + 0 0 + 1 1 + 0 0 + 1 1 + + + + + + + + Out + + + Label + Y_1 + + + + + + Const + + + rotation + + + + Value + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file