added some documentation

This commit is contained in:
hneemann 2016-04-16 10:13:14 +02:00
parent ee42b0d266
commit 6cfdaec7ff
7 changed files with 57 additions and 25 deletions

View File

@ -12,10 +12,15 @@ import de.neemann.digital.core.element.ElementTypeDescription;
import static de.neemann.digital.core.element.PinInfo.input; import static de.neemann.digital.core.element.PinInfo.input;
/** /**
* A adder.
*
* @author hneemann * @author hneemann
*/ */
public class Add extends Node implements Element { public class Add extends Node implements Element {
/**
* The adders description
*/
public static final ElementTypeDescription DESCRIPTION public static final ElementTypeDescription DESCRIPTION
= new ElementTypeDescription(Add.class, input("a"), input("b"), input("c_i")) = new ElementTypeDescription(Add.class, input("a"), input("b"), input("c_i"))
.addAttribute(AttributeKey.Rotate) .addAttribute(AttributeKey.Rotate)
@ -24,50 +29,47 @@ public class Add extends Node implements Element {
private final int bits; private final int bits;
private final ObservableValue sum; private final ObservableValue sum;
private final ObservableValue c_out; private final ObservableValue cOut;
private final long mask; private final long mask;
protected ObservableValue a; protected ObservableValue a;
protected ObservableValue b; protected ObservableValue b;
protected ObservableValue c_in; protected ObservableValue cIn;
protected long value; protected long value;
/**
* Create a new instance
*
* @param attributes the attributes
*/
public Add(ElementAttributes attributes) { public Add(ElementAttributes attributes) {
bits = attributes.get(AttributeKey.Bits); bits = attributes.get(AttributeKey.Bits);
this.mask = 1 << bits; this.mask = 1 << bits;
this.sum = new ObservableValue("s", bits); this.sum = new ObservableValue("s", bits);
this.c_out = new ObservableValue("c_o", 1); this.cOut = new ObservableValue("c_o", 1);
} }
@Override @Override
public void readInputs() throws NodeException { public void readInputs() throws NodeException {
value = a.getValue() + b.getValue() + c_in.getValue(); value = a.getValue() + b.getValue() + cIn.getValue();
} }
@Override @Override
public void writeOutputs() throws NodeException { public void writeOutputs() throws NodeException {
sum.setValue(value); sum.setValue(value);
c_out.setValue((value & mask) == 0 ? 0 : 1); cOut.setValue((value & mask) == 0 ? 0 : 1);
}
public ObservableValue getSum() {
return sum;
}
public ObservableValue getCOut() {
return c_out;
} }
@Override @Override
public void setInputs(ObservableValue... inputs) throws BitsException { public void setInputs(ObservableValue... inputs) throws BitsException {
a = inputs[0].addObserverToValue(this).checkBits(bits, this); a = inputs[0].addObserverToValue(this).checkBits(bits, this);
b = inputs[1].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 @Override
public ObservableValue[] getOutputs() { public ObservableValue[] getOutputs() {
return new ObservableValue[]{sum, c_out}; return new ObservableValue[]{sum, cOut};
} }
} }

View File

@ -12,10 +12,14 @@ import de.neemann.digital.core.element.ElementTypeDescription;
import static de.neemann.digital.core.element.PinInfo.input; import static de.neemann.digital.core.element.PinInfo.input;
/** /**
* A comparator
* @author hneemann * @author hneemann
*/ */
public class Comparator extends Node implements Element { public class Comparator extends Node implements Element {
/**
* The comparators description
*/
public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Comparator.class, input("a"), input("b")) public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Comparator.class, input("a"), input("b"))
.addAttribute(AttributeKey.Rotate) .addAttribute(AttributeKey.Rotate)
.addAttribute(AttributeKey.Label) .addAttribute(AttributeKey.Label)
@ -32,8 +36,14 @@ public class Comparator extends Node implements Element {
private final int maskOr; private final int maskOr;
private ObservableValue a; private ObservableValue a;
private ObservableValue b; 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) { public Comparator(ElementAttributes attributes) {
signed = attributes.get(AttributeKey.Signed); signed = attributes.get(AttributeKey.Signed);
bits = attributes.get(AttributeKey.Bits); bits = attributes.get(AttributeKey.Bits);
@ -66,8 +76,8 @@ public class Comparator extends Node implements Element {
} }
boolean kl = valueA < valueB; boolean kl = valueA < valueB;
aklb.setValue(kl ? 1 : 0); aklb.setBool(kl);
agrb.setValue(kl ? 0 : 1); agrb.setBool(!kl);
} }
} }

View File

@ -11,10 +11,15 @@ import de.neemann.digital.core.element.ElementTypeDescription;
import static de.neemann.digital.core.element.PinInfo.input; import static de.neemann.digital.core.element.PinInfo.input;
/** /**
* A multiplicator
*
* @author hneemann * @author hneemann
*/ */
public class Mul extends Node implements Element { public class Mul extends Node implements Element {
/**
* The multiplicators description
*/
public static final ElementTypeDescription DESCRIPTION public static final ElementTypeDescription DESCRIPTION
= new ElementTypeDescription(Mul.class, input("a"), input("b")) = new ElementTypeDescription(Mul.class, input("a"), input("b"))
.addAttribute(AttributeKey.Rotate) .addAttribute(AttributeKey.Rotate)
@ -27,6 +32,11 @@ public class Mul extends Node implements Element {
private ObservableValue b; private ObservableValue b;
private long value; private long value;
/**
* Creates a new instance
*
* @param attributes the attributes
*/
public Mul(ElementAttributes attributes) { public Mul(ElementAttributes attributes) {
bits = attributes.get(AttributeKey.Bits); bits = attributes.get(AttributeKey.Bits);
this.mul = new ObservableValue("mul", bits * 2); this.mul = new ObservableValue("mul", bits * 2);
@ -42,10 +52,6 @@ public class Mul extends Node implements Element {
mul.setValue(value); mul.setValue(value);
} }
public ObservableValue getMul() {
return mul;
}
@Override @Override
public void setInputs(ObservableValue... inputs) throws NodeException { public void setInputs(ObservableValue... inputs) throws NodeException {
a = inputs[0].addObserverToValue(this).checkBits(bits, this); a = inputs[0].addObserverToValue(this).checkBits(bits, this);

View File

@ -12,18 +12,26 @@ import static de.neemann.digital.core.element.PinInfo.input;
*/ */
public class Sub extends Add { public class Sub extends Add {
/**
* The subtractors description
*/
public static final ElementTypeDescription DESCRIPTION public static final ElementTypeDescription DESCRIPTION
= new ElementTypeDescription(Sub.class, input("a"), input("b"), input("c_i")) = new ElementTypeDescription(Sub.class, input("a"), input("b"), input("c_i"))
.addAttribute(AttributeKey.Rotate) .addAttribute(AttributeKey.Rotate)
.addAttribute(AttributeKey.Label) .addAttribute(AttributeKey.Label)
.addAttribute(AttributeKey.Bits); .addAttribute(AttributeKey.Bits);
/**
* Creates a new instance
*
* @param attributes the attributes
*/
public Sub(ElementAttributes attributes) { public Sub(ElementAttributes attributes) {
super(attributes); super(attributes);
} }
@Override @Override
public void readInputs() throws NodeException { public void readInputs() throws NodeException {
value = a.getValue() - b.getValue() - c_in.getValue(); value = a.getValue() - b.getValue() - cIn.getValue();
} }
} }

View File

@ -0,0 +1,6 @@
/**
* Simple arithmetic operations like addition, subtraction and multiplication
*
* @author hneemann
*/
package de.neemann.digital.core.arithmetic;

View File

@ -19,7 +19,7 @@ public class MulTest extends TestCase {
Mul node = model.add(new Mul(new ElementAttributes().setBits(4))); Mul node = model.add(new Mul(new ElementAttributes().setBits(4)));
node.setInputs(a, b); 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(0, 0, 0);
sc.check(6, 6, 36); sc.check(6, 6, 36);
sc.check(15, 15, 225); sc.check(15, 15, 225);

View File

@ -22,7 +22,7 @@ public class SubTest extends TestCase {
node.setInputs(a, b, c); node.setInputs(a, b, c);
model.add(node); 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(0, 0, 0, 0, 0);
sc.check(3, 2, 0, 1, 0); sc.check(3, 2, 0, 1, 0);
sc.check(2, 3, 0, 15, 1); sc.check(2, 3, 0, 15, 1);