diff --git a/src/main/java/de/neemann/digital/core/arithmetic/Add.java b/src/main/java/de/neemann/digital/core/arithmetic/Add.java index 2a3b7dddc..16803697c 100644 --- a/src/main/java/de/neemann/digital/core/arithmetic/Add.java +++ b/src/main/java/de/neemann/digital/core/arithmetic/Add.java @@ -12,10 +12,15 @@ import de.neemann.digital.core.element.ElementTypeDescription; import static de.neemann.digital.core.element.PinInfo.input; /** + * A adder. + * * @author hneemann */ public class Add extends Node implements Element { + /** + * The adders description + */ public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Add.class, input("a"), input("b"), input("c_i")) .addAttribute(AttributeKey.Rotate) @@ -24,50 +29,47 @@ public class Add extends Node implements Element { private final int bits; private final ObservableValue sum; - private final ObservableValue c_out; + private final ObservableValue cOut; private final long mask; protected ObservableValue a; protected ObservableValue b; - protected ObservableValue c_in; + protected ObservableValue cIn; protected long value; + /** + * Create a new instance + * + * @param attributes the attributes + */ public Add(ElementAttributes attributes) { bits = attributes.get(AttributeKey.Bits); this.mask = 1 << bits; this.sum = new ObservableValue("s", bits); - this.c_out = new ObservableValue("c_o", 1); + this.cOut = new ObservableValue("c_o", 1); } @Override public void readInputs() throws NodeException { - value = a.getValue() + b.getValue() + c_in.getValue(); + value = a.getValue() + b.getValue() + cIn.getValue(); } @Override public void writeOutputs() throws NodeException { sum.setValue(value); - c_out.setValue((value & mask) == 0 ? 0 : 1); - } - - public ObservableValue getSum() { - return sum; - } - - public ObservableValue getCOut() { - return c_out; + cOut.setValue((value & mask) == 0 ? 0 : 1); } @Override public void setInputs(ObservableValue... inputs) throws BitsException { a = inputs[0].addObserverToValue(this).checkBits(bits, this); b = inputs[1].addObserverToValue(this).checkBits(bits, this); - c_in = inputs[2].addObserverToValue(this).checkBits(1, this); + cIn = inputs[2].addObserverToValue(this).checkBits(1, this); } @Override public ObservableValue[] getOutputs() { - return new ObservableValue[]{sum, c_out}; + return new ObservableValue[]{sum, cOut}; } } diff --git a/src/main/java/de/neemann/digital/core/arithmetic/Comparator.java b/src/main/java/de/neemann/digital/core/arithmetic/Comparator.java index ddb31acbb..1990840a5 100644 --- a/src/main/java/de/neemann/digital/core/arithmetic/Comparator.java +++ b/src/main/java/de/neemann/digital/core/arithmetic/Comparator.java @@ -12,10 +12,14 @@ import de.neemann.digital.core.element.ElementTypeDescription; import static de.neemann.digital.core.element.PinInfo.input; /** + * A comparator * @author hneemann */ public class Comparator extends Node implements Element { + /** + * The comparators description + */ public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Comparator.class, input("a"), input("b")) .addAttribute(AttributeKey.Rotate) .addAttribute(AttributeKey.Label) @@ -32,8 +36,14 @@ public class Comparator extends Node implements Element { private final int maskOr; private ObservableValue a; private ObservableValue b; - private long valueA, valueB; + private long valueA; + private long valueB; + /** + * Create a new instance + * + * @param attributes the attributes + */ public Comparator(ElementAttributes attributes) { signed = attributes.get(AttributeKey.Signed); bits = attributes.get(AttributeKey.Bits); @@ -66,8 +76,8 @@ public class Comparator extends Node implements Element { } boolean kl = valueA < valueB; - aklb.setValue(kl ? 1 : 0); - agrb.setValue(kl ? 0 : 1); + aklb.setBool(kl); + agrb.setBool(!kl); } } diff --git a/src/main/java/de/neemann/digital/core/arithmetic/Mul.java b/src/main/java/de/neemann/digital/core/arithmetic/Mul.java index 62e480e50..14b8f1f94 100644 --- a/src/main/java/de/neemann/digital/core/arithmetic/Mul.java +++ b/src/main/java/de/neemann/digital/core/arithmetic/Mul.java @@ -11,10 +11,15 @@ import de.neemann.digital.core.element.ElementTypeDescription; import static de.neemann.digital.core.element.PinInfo.input; /** + * A multiplicator + * * @author hneemann */ public class Mul extends Node implements Element { + /** + * The multiplicators description + */ public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Mul.class, input("a"), input("b")) .addAttribute(AttributeKey.Rotate) @@ -27,6 +32,11 @@ public class Mul extends Node implements Element { private ObservableValue b; private long value; + /** + * Creates a new instance + * + * @param attributes the attributes + */ public Mul(ElementAttributes attributes) { bits = attributes.get(AttributeKey.Bits); this.mul = new ObservableValue("mul", bits * 2); @@ -42,10 +52,6 @@ public class Mul extends Node implements Element { mul.setValue(value); } - public ObservableValue getMul() { - return mul; - } - @Override public void setInputs(ObservableValue... inputs) throws NodeException { a = inputs[0].addObserverToValue(this).checkBits(bits, this); diff --git a/src/main/java/de/neemann/digital/core/arithmetic/Sub.java b/src/main/java/de/neemann/digital/core/arithmetic/Sub.java index 28a71e712..f650d1c19 100644 --- a/src/main/java/de/neemann/digital/core/arithmetic/Sub.java +++ b/src/main/java/de/neemann/digital/core/arithmetic/Sub.java @@ -12,18 +12,26 @@ import static de.neemann.digital.core.element.PinInfo.input; */ public class Sub extends Add { + /** + * The subtractors description + */ public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Sub.class, input("a"), input("b"), input("c_i")) .addAttribute(AttributeKey.Rotate) .addAttribute(AttributeKey.Label) .addAttribute(AttributeKey.Bits); + /** + * Creates a new instance + * + * @param attributes the attributes + */ public Sub(ElementAttributes attributes) { super(attributes); } @Override public void readInputs() throws NodeException { - value = a.getValue() - b.getValue() - c_in.getValue(); + value = a.getValue() - b.getValue() - cIn.getValue(); } } diff --git a/src/main/java/de/neemann/digital/core/arithmetic/package-info.java b/src/main/java/de/neemann/digital/core/arithmetic/package-info.java new file mode 100644 index 000000000..2126058e3 --- /dev/null +++ b/src/main/java/de/neemann/digital/core/arithmetic/package-info.java @@ -0,0 +1,6 @@ +/** + * Simple arithmetic operations like addition, subtraction and multiplication + * + * @author hneemann + */ +package de.neemann.digital.core.arithmetic; diff --git a/src/test/java/de/neemann/digital/core/arithmetic/MulTest.java b/src/test/java/de/neemann/digital/core/arithmetic/MulTest.java index 6616ef89e..18e8ef5d9 100644 --- a/src/test/java/de/neemann/digital/core/arithmetic/MulTest.java +++ b/src/test/java/de/neemann/digital/core/arithmetic/MulTest.java @@ -19,7 +19,7 @@ public class MulTest extends TestCase { Mul node = model.add(new Mul(new ElementAttributes().setBits(4))); node.setInputs(a, b); - TestExecuter sc = new TestExecuter(model).setInputs(a, b).setOutputs(node.getMul()); + TestExecuter sc = new TestExecuter(model).setInputs(a, b).setOutputsOf(node); sc.check(0, 0, 0); sc.check(6, 6, 36); sc.check(15, 15, 225); diff --git a/src/test/java/de/neemann/digital/core/arithmetic/SubTest.java b/src/test/java/de/neemann/digital/core/arithmetic/SubTest.java index 5d20feb10..3cd3a98db 100644 --- a/src/test/java/de/neemann/digital/core/arithmetic/SubTest.java +++ b/src/test/java/de/neemann/digital/core/arithmetic/SubTest.java @@ -22,7 +22,7 @@ public class SubTest extends TestCase { node.setInputs(a, b, c); model.add(node); - TestExecuter sc = new TestExecuter(model).setInputs(a, b, c).setOutputs(node.getSum(), node.getCOut()); + TestExecuter sc = new TestExecuter(model).setInputs(a, b, c).setOutputsOf(node); sc.check(0, 0, 0, 0, 0); sc.check(3, 2, 0, 1, 0); sc.check(2, 3, 0, 15, 1);