mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-16 00:15:01 -04:00
added some documentation
This commit is contained in:
parent
ee42b0d266
commit
6cfdaec7ff
@ -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};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Simple arithmetic operations like addition, subtraction and multiplication
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
package de.neemann.digital.core.arithmetic;
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user