import java.util.Properties;
import syncsim.Signal;
import syncsim.SoftPart;
public class AndPart
extends SoftPart {
protected final String PROP_BITWIDTH = "bitwidth"; // name of the property in the XML-file
protected int bitWidth; // Storing the bitWidth-property of the AND-gate
public AndPart() {
}
public String in() {
return "A:" + bitWidth + ", B:" + bitWidth;
}
public String out() {
return "out:" + bitWidth;
}
public void initialize(String instanceName, Properties properties) {
this.instanceName = instanceName;
// read the PROP_BITWIDTH property and then remove it from the Properties object
String value = properties.getProperty(PROP_BITWIDTH);
properties.remove(PROP_BITWIDTH);
// the PROP_BITWIDTH property must be defined for this part to work, if it isn't throw an exception
if (value == null) {
throwMissingProperty(PROP_BITWIDTH);
}
// try to parse the value return from the Properties object into an integer
try {
bitWidth = Integer.parseInt(value);
}
catch (NumberFormatException e) {
throwGeneralException("Property '" + PROP_BITWIDTH + "' is not a number.");
}
// ensure that all properties defined in the XML-file was used
if (properties.size() > 0) {
throwUnknownProperties();
}
}
public void update(Signal[] in, Signal[] out) {
// only preform the logic if the insignals are valid
if (in[0].isValid() && in[1].isValid()) {
out[0] = new Signal(bitWidth, in[0].and(in[1]));
}
}
}
|