mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-15 07:48:29 -04:00
most tests are working again
This commit is contained in:
parent
181803bc78
commit
4b576d4285
@ -97,40 +97,48 @@ public final class PlainSwitch implements NodeInterface {
|
||||
input2.addObserverToValue(this).checkBits(bits, null);
|
||||
switch (unidirectional) {
|
||||
case NO:
|
||||
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)
|
||||
switchModel = new UniDirectionalSwitch(constant, output2);
|
||||
else {
|
||||
constant = in2.searchConstant();
|
||||
if (constant != null)
|
||||
switchModel = new UniDirectionalSwitch(constant, output1);
|
||||
else
|
||||
switchModel = new RealSwitch(in1, in2);
|
||||
}
|
||||
} else
|
||||
switchModel = new UniDirectionalSwitch(input1, output2);
|
||||
} else {
|
||||
if (input2 instanceof CommonBusValue) {
|
||||
switchModel = new UniDirectionalSwitch(input2, output1);
|
||||
} else {
|
||||
throw new NodeException(Lang.get("err_switchHasNoNet"), output1, output2);
|
||||
}
|
||||
}
|
||||
switchModel = createSwitchModel(input1, input2, output1, output2, false, null);
|
||||
break;
|
||||
case FROM1TO2:
|
||||
switchModel = new UniDirectionalSwitch(input1, output2);
|
||||
switchModel = new UniDirectionalSwitch(input1, output2, null);
|
||||
break;
|
||||
case FROM2TO1:
|
||||
switchModel = new UniDirectionalSwitch(input2, output1);
|
||||
switchModel = new UniDirectionalSwitch(input2, output1, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static SwitchModel createSwitchModel(
|
||||
ObservableValue input1, ObservableValue input2,
|
||||
ObservableValue output1, ObservableValue output2,
|
||||
boolean isTDSwitch, String name) 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, name);
|
||||
else {
|
||||
constant = in2.searchConstant();
|
||||
if (constant != null)
|
||||
return new UniDirectionalSwitch(constant, output1, !isTDSwitch, name);
|
||||
else
|
||||
return new RealSwitch(in1, in2);
|
||||
}
|
||||
} else
|
||||
return new UniDirectionalSwitch(input1, output2, name);
|
||||
} else {
|
||||
if (input2 instanceof CommonBusValue) {
|
||||
return new UniDirectionalSwitch(input2, output1, !isTDSwitch, name);
|
||||
} else {
|
||||
throw new NodeException(Lang.get("err_switchHasNoNet"), output1, output2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObservableValues getOutputs() {
|
||||
return new ObservableValues(output1, output2);
|
||||
@ -215,19 +223,30 @@ public final class PlainSwitch implements NodeInterface {
|
||||
private static final class UniDirectionalSwitch implements SwitchModel {
|
||||
private final ObservableValue input;
|
||||
private final ObservableValue output;
|
||||
private final boolean setOpenToHighZ;
|
||||
private final String name;
|
||||
private boolean closed;
|
||||
|
||||
UniDirectionalSwitch(ObservableValue input, ObservableValue output) {
|
||||
UniDirectionalSwitch(ObservableValue input, ObservableValue output, String name) {
|
||||
this(input, output, true, name);
|
||||
}
|
||||
|
||||
UniDirectionalSwitch(ObservableValue input, ObservableValue output, boolean setOpenToHighZ, String name) {
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
this.setOpenToHighZ = setOpenToHighZ;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate() {
|
||||
if (closed) {
|
||||
System.out.println(name + "-Closed " + output.getName());
|
||||
output.set(input.getValue(), input.getHighZ());
|
||||
} else {
|
||||
output.setToHighZ();
|
||||
System.out.println(name + "-Opened " + output.getName());
|
||||
if (setOpenToHighZ)
|
||||
output.setToHighZ();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,38 +5,29 @@
|
||||
*/
|
||||
package de.neemann.digital.core.switching;
|
||||
|
||||
import de.neemann.digital.core.Model;
|
||||
import de.neemann.digital.core.NodeException;
|
||||
import de.neemann.digital.core.ObservableValue;
|
||||
import de.neemann.digital.core.ObservableValues;
|
||||
import de.neemann.digital.core.*;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
|
||||
/**
|
||||
* A simple double throw switch
|
||||
*/
|
||||
public final class PlainSwitchDT {
|
||||
private final PlainSwitch s1;
|
||||
private final PlainSwitch s2;
|
||||
public final class PlainSwitchDT implements NodeInterface {
|
||||
private final ObservableValue outputA;
|
||||
private final ObservableValue outputB;
|
||||
private final ObservableValue outputC;
|
||||
private final int bits;
|
||||
private final String name;
|
||||
private PlainSwitch.SwitchModel s1;
|
||||
private PlainSwitch.SwitchModel s2;
|
||||
private boolean closed = false;
|
||||
|
||||
|
||||
PlainSwitchDT(int bits, int num) {
|
||||
PlainSwitchDT(int bits, int num, String name) {
|
||||
this.bits = bits;
|
||||
this.name = name;
|
||||
outputA = new ObservableValue("A" + num, bits).setBidirectional().setToHighZ().setDescription(Lang.get("elem_Switch_pin")).setSwitchPin(true);
|
||||
outputB = new ObservableValue("B" + num, bits).setBidirectional().setToHighZ().setDescription(Lang.get("elem_Switch_pin")).setSwitchPin(true);
|
||||
outputC = new ObservableValue("C" + num, bits).setBidirectional().setToHighZ().setDescription(Lang.get("elem_Switch_pin")).setSwitchPin(true);
|
||||
s1 = new PlainSwitch(outputA, outputB, false);
|
||||
s2 = new PlainSwitch(outputA, outputC, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the outputs to the given builder
|
||||
*
|
||||
* @param ov the builder to use
|
||||
*/
|
||||
public void addOutputs(ObservableValues.Builder ov) {
|
||||
ov.add(outputA, outputB, outputC);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,8 +39,17 @@ public final class PlainSwitchDT {
|
||||
* @throws NodeException NodeException
|
||||
*/
|
||||
public void setInputs(ObservableValue inA, ObservableValue inB, ObservableValue inC) throws NodeException {
|
||||
s1.setInputs(inA, inB);
|
||||
s2.setInputs(inA, inC);
|
||||
if (inA != null && (inB != null || inC != null))
|
||||
inA.addObserverToValue(this).checkBits(bits, null);
|
||||
|
||||
if (inA != null && inB != null) {
|
||||
inB.addObserverToValue(this).checkBits(bits, null);
|
||||
s1 = PlainSwitch.createSwitchModel(inA, inB, outputA, outputB, true, name+"-A-B");
|
||||
}
|
||||
if (inA != null && inC != null) {
|
||||
inC.addObserverToValue(this).checkBits(bits, null);
|
||||
s2 = PlainSwitch.createSwitchModel(inA, inC, outputA, outputC, true, name+"-A-C");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,8 +58,15 @@ public final class PlainSwitchDT {
|
||||
* @param model the model
|
||||
*/
|
||||
public void init(Model model) {
|
||||
s1.init(model);
|
||||
s2.init(model);
|
||||
if (s1 != null) {
|
||||
s1.setModel(model);
|
||||
s1.setClosed(closed);
|
||||
}
|
||||
if (s2 != null) {
|
||||
s2.setModel(model);
|
||||
s2.setClosed(!closed);
|
||||
}
|
||||
hasChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,16 +75,29 @@ public final class PlainSwitchDT {
|
||||
* @param isClosed true is A-B is closed and A-C is open
|
||||
*/
|
||||
public void setClosed(boolean isClosed) {
|
||||
s1.setClosed(isClosed);
|
||||
s2.setClosed(!isClosed);
|
||||
if (this.closed != isClosed) {
|
||||
this.closed = isClosed;
|
||||
if (closed) {
|
||||
if (s1 != null)
|
||||
s1.setClosed(closed);
|
||||
if (s2 != null)
|
||||
s2.setClosed(!closed);
|
||||
}
|
||||
hasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calles is state has changed
|
||||
*/
|
||||
public void hashChanged() {
|
||||
s1.hasChanged();
|
||||
s2.hasChanged();
|
||||
@Override
|
||||
public void hasChanged() {
|
||||
if (s1 != null)
|
||||
s1.propagate();
|
||||
if (s2 != null)
|
||||
s2.propagate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObservableValues getOutputs() {
|
||||
return new ObservableValues(outputA, outputB, outputC);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,7 +38,7 @@ public class SwitchDT implements Element, NodeInterface {
|
||||
int poleCount = attr.get(Keys.POLES);
|
||||
poles = new PlainSwitchDT[poleCount];
|
||||
for (int i = 0; i < poleCount; i++)
|
||||
poles[i] = new PlainSwitchDT(bits, i + 1);
|
||||
poles[i] = new PlainSwitchDT(bits, i + 1, attr.getCleanLabel());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -67,7 +67,7 @@ public class SwitchDT implements Element, NodeInterface {
|
||||
@Override
|
||||
public void hasChanged() {
|
||||
for (PlainSwitchDT p : poles)
|
||||
p.hashChanged();
|
||||
p.hasChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -6,7 +6,7 @@
|
||||
<visualElement>
|
||||
<elementName>VDD</elementName>
|
||||
<elementAttributes/>
|
||||
<pos x="300" y="200"/>
|
||||
<pos x="300" y="240"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>In</elementName>
|
||||
@ -16,7 +16,7 @@
|
||||
<string>A</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="200" y="220"/>
|
||||
<pos x="200" y="240"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>In</elementName>
|
||||
@ -126,6 +126,14 @@
|
||||
<p1 x="420" y="400"/>
|
||||
<p2 x="460" y="400"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="320" y="240"/>
|
||||
<p2 x="420" y="240"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="200" y="240"/>
|
||||
<p2 x="240" y="240"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="280" y="340"/>
|
||||
<p2 x="320" y="340"/>
|
||||
@ -142,20 +150,12 @@
|
||||
<p1 x="280" y="440"/>
|
||||
<p2 x="420" y="440"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="200" y="220"/>
|
||||
<p2 x="240" y="220"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="320" y="220"/>
|
||||
<p2 x="420" y="220"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="240" y="300"/>
|
||||
<p2 x="240" y="320"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="240" y="220"/>
|
||||
<p1 x="240" y="240"/>
|
||||
<p2 x="240" y="260"/>
|
||||
</wire>
|
||||
<wire>
|
||||
@ -167,7 +167,7 @@
|
||||
<p2 x="240" y="380"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="320" y="220"/>
|
||||
<p1 x="320" y="240"/>
|
||||
<p2 x="320" y="260"/>
|
||||
</wire>
|
||||
<wire>
|
||||
@ -175,21 +175,13 @@
|
||||
<p2 x="320" y="340"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="420" y="220"/>
|
||||
<p1 x="420" y="240"/>
|
||||
<p2 x="420" y="400"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="420" y="400"/>
|
||||
<p2 x="420" y="440"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="340" y="240"/>
|
||||
<p2 x="340" y="260"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="280" y="240"/>
|
||||
<p2 x="280" y="260"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="280" y="300"/>
|
||||
<p2 x="280" y="340"/>
|
||||
@ -203,11 +195,7 @@
|
||||
<p2 x="280" y="380"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="300" y="360"/>
|
||||
<p2 x="300" y="380"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="300" y="200"/>
|
||||
<p1 x="300" y="240"/>
|
||||
<p2 x="300" y="260"/>
|
||||
</wire>
|
||||
</wires>
|
||||
|
158
src/test/resources/dig/test/switch/switch_xor3.dig
Normal file
158
src/test/resources/dig/test/switch/switch_xor3.dig
Normal file
@ -0,0 +1,158 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<circuit>
|
||||
<version>1</version>
|
||||
<attributes/>
|
||||
<visualElements>
|
||||
<visualElement>
|
||||
<elementName>In</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>A</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="140" y="160"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Testcase</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Testdata</string>
|
||||
<testData>
|
||||
<dataString>A B Y
|
||||
0 0 z
|
||||
0 1 1
|
||||
1 0 1
|
||||
1 1 z
|
||||
|
||||
|
||||
# transitions
|
||||
0 0 z
|
||||
0 1 1
|
||||
0 0 z
|
||||
1 0 1
|
||||
0 0 z
|
||||
1 1 z
|
||||
0 0 z
|
||||
0 1 1
|
||||
1 0 1
|
||||
0 1 1
|
||||
1 1 z
|
||||
0 1 1
|
||||
1 0 1
|
||||
1 1 z
|
||||
1 0 1
|
||||
</dataString>
|
||||
</testData>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="300" y="120"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>In</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>B</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="140" y="260"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>VDD</elementName>
|
||||
<elementAttributes/>
|
||||
<pos x="160" y="180"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Out</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>Y</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="360" y="220"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>RelayDT</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>R1</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="180" y="200"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>RelayDT</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>rotation</string>
|
||||
<rotation rotation="2"/>
|
||||
</entry>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>R2</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="280" y="220"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Ground</elementName>
|
||||
<elementAttributes/>
|
||||
<pos x="240" y="180"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Ground</elementName>
|
||||
<elementAttributes/>
|
||||
<pos x="300" y="280"/>
|
||||
</visualElement>
|
||||
</visualElements>
|
||||
<wires>
|
||||
<wire>
|
||||
<p1 x="140" y="160"/>
|
||||
<p2 x="180" y="160"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="220" y="160"/>
|
||||
<p2 x="240" y="160"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="280" y="260"/>
|
||||
<p2 x="300" y="260"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="140" y="260"/>
|
||||
<p2 x="240" y="260"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="160" y="200"/>
|
||||
<p2 x="180" y="200"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="220" y="200"/>
|
||||
<p2 x="240" y="200"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="280" y="220"/>
|
||||
<p2 x="360" y="220"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="220" y="220"/>
|
||||
<p2 x="240" y="220"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="160" y="180"/>
|
||||
<p2 x="160" y="200"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="240" y="160"/>
|
||||
<p2 x="240" y="180"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="300" y="260"/>
|
||||
<p2 x="300" y="280"/>
|
||||
</wire>
|
||||
</wires>
|
||||
<measurementOrdering/>
|
||||
</circuit>
|
Loading…
x
Reference in New Issue
Block a user