mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-15 15:58:41 -04:00
added some documentation
This commit is contained in:
parent
6cfdaec7ff
commit
751708bd7a
@ -9,12 +9,22 @@ import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The And gate
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
public class And extends Function {
|
||||
|
||||
/**
|
||||
* The And description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION = new FanInDescription(And.class);
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public And(ElementAttributes attributes) {
|
||||
super(attributes.get(AttributeKey.Bits));
|
||||
}
|
||||
|
@ -10,46 +10,61 @@ import java.util.ArrayList;
|
||||
import static de.neemann.digital.core.element.PinInfo.input;
|
||||
|
||||
/**
|
||||
* A fan in. Used as base class for the simple bool operations
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
public abstract class FanIn extends Node implements Element {
|
||||
|
||||
protected final ArrayList<ObservableValue> inputs;
|
||||
protected final ObservableValue output;
|
||||
private final ArrayList<ObservableValue> inputs;
|
||||
private final ObservableValue output;
|
||||
private final int bits;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param bits the number of bits
|
||||
*/
|
||||
public FanIn(int bits) {
|
||||
this.bits = bits;
|
||||
inputs = new ArrayList<>();
|
||||
output = new ObservableValue("out", bits);
|
||||
}
|
||||
|
||||
public ObservableValue getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInputs(ObservableValue... in) throws NodeException {
|
||||
for (ObservableValue v : in)
|
||||
inputs.add(v.checkBits(bits, this).addObserverToValue(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the outputs
|
||||
*/
|
||||
public ObservableValue getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the outputs
|
||||
*/
|
||||
public ArrayList<ObservableValue> getInputs() {
|
||||
return inputs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObservableValue[] getOutputs() {
|
||||
return new ObservableValue[]{output};
|
||||
}
|
||||
|
||||
public static class FanInDescription extends ElementTypeDescription {
|
||||
public FanInDescription(Class<? extends Element> clazz) {
|
||||
/**
|
||||
* The fan in description
|
||||
*/
|
||||
static class FanInDescription extends ElementTypeDescription {
|
||||
FanInDescription(Class<? extends Element> clazz) {
|
||||
super(clazz);
|
||||
addAttributes();
|
||||
}
|
||||
|
||||
public FanInDescription(String name, ElementFactory elementFactory) {
|
||||
super(name, elementFactory);
|
||||
addAttributes();
|
||||
}
|
||||
|
||||
private void addAttributes() {
|
||||
addAttribute(AttributeKey.Rotate);
|
||||
addAttribute(AttributeKey.Bits);
|
||||
|
@ -8,23 +8,24 @@ import java.util.ArrayList;
|
||||
/**
|
||||
* @author hneemann
|
||||
*/
|
||||
public abstract class Function extends FanIn {
|
||||
abstract class Function extends FanIn {
|
||||
|
||||
private long value;
|
||||
|
||||
public Function(int bits) {
|
||||
Function(int bits) {
|
||||
super(bits);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readInputs() throws NodeException {
|
||||
value = calculate(inputs);
|
||||
value = calculate(getInputs());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeOutputs() throws NodeException {
|
||||
output.setValue(value);
|
||||
getOutput().setValue(value);
|
||||
}
|
||||
|
||||
protected abstract int calculate(ArrayList<ObservableValue> inputs) throws NodeException;
|
||||
|
||||
}
|
||||
|
@ -9,12 +9,21 @@ import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The NAnd
|
||||
* @author hneemann
|
||||
*/
|
||||
public class NAnd extends Function {
|
||||
|
||||
/**
|
||||
* The NAnd description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION = new FanInDescription(NAnd.class);
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public NAnd(ElementAttributes attributes) {
|
||||
super(attributes.get(AttributeKey.Bits));
|
||||
}
|
||||
|
@ -9,12 +9,21 @@ import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The NOr
|
||||
* @author hneemann
|
||||
*/
|
||||
public class NOr extends Function {
|
||||
|
||||
/**
|
||||
* The NOr description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION = new FanInDescription(NOr.class);
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public NOr(ElementAttributes attributes) {
|
||||
super(attributes.get(AttributeKey.Bits));
|
||||
}
|
||||
|
@ -11,10 +11,15 @@ import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import static de.neemann.digital.core.element.PinInfo.input;
|
||||
|
||||
/**
|
||||
* The Not
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
public class Not extends Node implements Element {
|
||||
|
||||
/**
|
||||
* The Not description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Not.class, input("in"))
|
||||
.addAttribute(AttributeKey.Rotate)
|
||||
.addAttribute(AttributeKey.Bits);
|
||||
@ -24,6 +29,11 @@ public class Not extends Node implements Element {
|
||||
private ObservableValue input;
|
||||
private long value;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public Not(ElementAttributes attributes) {
|
||||
bits = attributes.get(AttributeKey.Bits);
|
||||
output = new ObservableValue("out", bits);
|
||||
@ -39,10 +49,6 @@ public class Not extends Node implements Element {
|
||||
output.setValue(~value);
|
||||
}
|
||||
|
||||
public ObservableValue getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInputs(ObservableValue... inputs) throws NodeException {
|
||||
input = inputs[0].addObserverToValue(this).checkBits(bits, this);
|
||||
|
@ -9,12 +9,21 @@ import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The Or
|
||||
* @author hneemann
|
||||
*/
|
||||
public class Or extends Function {
|
||||
|
||||
/**
|
||||
* The And description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION = new FanInDescription(Or.class);
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public Or(ElementAttributes attributes) {
|
||||
super(attributes.get(AttributeKey.Bits));
|
||||
}
|
||||
|
@ -8,15 +8,24 @@ import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import static de.neemann.digital.core.element.PinInfo.input;
|
||||
|
||||
/**
|
||||
* The XNOr
|
||||
* @author hneemann
|
||||
*/
|
||||
public class XNOr extends XOr {
|
||||
|
||||
/**
|
||||
* The XNOr description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION
|
||||
= new ElementTypeDescription(XNOr.class, input("a"), input("b"))
|
||||
.addAttribute(AttributeKey.Rotate)
|
||||
.addAttribute(AttributeKey.Bits);
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public XNOr(ElementAttributes attributes) {
|
||||
super(attributes);
|
||||
}
|
||||
|
@ -16,6 +16,9 @@ import static de.neemann.digital.core.element.PinInfo.input;
|
||||
*/
|
||||
public class XOr extends Node implements Element {
|
||||
|
||||
/**
|
||||
* The XOr description
|
||||
*/
|
||||
public static final ElementTypeDescription DESCRIPTION
|
||||
= new ElementTypeDescription(XOr.class, input("a"), input("b"))
|
||||
.addAttribute(AttributeKey.Rotate)
|
||||
@ -27,6 +30,11 @@ public class XOr extends Node implements Element {
|
||||
protected ObservableValue b;
|
||||
protected long value;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param attributes the attributes
|
||||
*/
|
||||
public XOr(ElementAttributes attributes) {
|
||||
bits = attributes.get(AttributeKey.Bits);
|
||||
this.out = new ObservableValue("out", bits);
|
||||
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* The simple boolean operation like and, or and not
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
package de.neemann.digital.core.basic;
|
@ -47,12 +47,12 @@ public class Multiplexer extends FanIn {
|
||||
@Override
|
||||
public void readInputs() throws NodeException {
|
||||
int n = (int) selector.getValue();
|
||||
value = inputs.get(n).getValue();
|
||||
value = getInputs().get(n).getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeOutputs() throws NodeException {
|
||||
output.setValue(value);
|
||||
getOutput().setValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -85,9 +85,9 @@ public class FlipFlopsTest extends TestCase {
|
||||
|
||||
|
||||
FanIn a3 = model.add(new And(attr));
|
||||
a3.setInputs(nor1.getOutput(), not.getOutput());
|
||||
a3.setInputs(nor1.getOutput(), not.getOutputs()[0]);
|
||||
FanIn a4 = model.add(new And(attr));
|
||||
a4.setInputs(nor2.getOutput(), not.getOutput());
|
||||
a4.setInputs(nor2.getOutput(), not.getOutputs()[0]);
|
||||
|
||||
nor3.setInputs(a3.getOutput(), nor4.getOutput());
|
||||
nor4.setInputs(a4.getOutput(), nor3.getOutput());
|
||||
|
Loading…
x
Reference in New Issue
Block a user