use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.
the class Demultiplexer method propagate.
@Override
public void propagate(InstanceState state) {
// get attributes
BitWidth data = state.getAttributeValue(StdAttr.WIDTH);
BitWidth select = state.getAttributeValue(Plexers.ATTR_SELECT);
Boolean threeState = state.getAttributeValue(Plexers.ATTR_TRISTATE);
boolean enable = state.getAttributeValue(Plexers.ATTR_ENABLE).booleanValue();
int outputs = 1 << select.getWidth();
Value en = enable ? state.getPortValue(outputs + 1) : Value.TRUE;
// determine output values
// the default output
Value others;
if (threeState.booleanValue()) {
others = Value.createUnknown(data);
} else {
others = Value.createKnown(data, 0);
}
// the special output
int outIndex = -1;
Value out = null;
if (en == Value.FALSE) {
Object opt = state.getAttributeValue(Plexers.ATTR_DISABLED);
Value base = opt == Plexers.DISABLED_ZERO ? Value.FALSE : Value.UNKNOWN;
others = Value.repeat(base, data.getWidth());
} else if (en == Value.ERROR && state.isPortConnected(outputs + 1)) {
others = Value.createError(data);
} else {
Value sel = state.getPortValue(outputs);
if (sel.isFullyDefined()) {
outIndex = sel.toIntValue();
out = state.getPortValue(outputs + (enable ? 2 : 1));
} else if (sel.isErrorValue()) {
others = Value.createError(data);
} else {
others = Value.createUnknown(data);
}
}
// now propagate them
for (int i = 0; i < outputs; i++) {
state.setPort(i, i == outIndex ? out : others, Plexers.DELAY);
}
}
use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.
the class PriorityEncoder method propagate.
@Override
public void propagate(InstanceState state) {
BitWidth select = state.getAttributeValue(Plexers.ATTR_SELECT);
int n = 1 << select.getWidth();
boolean enabled = state.getPortValue(n + EN_IN) != Value.FALSE;
int out = -1;
Value outDefault;
if (enabled) {
outDefault = Value.createUnknown(select);
for (int i = n - 1; i >= 0; i--) {
if (state.getPortValue(i) == Value.TRUE) {
out = i;
break;
}
}
} else {
Object opt = state.getAttributeValue(Plexers.ATTR_DISABLED);
Value base = opt == Plexers.DISABLED_ZERO ? Value.FALSE : Value.UNKNOWN;
outDefault = Value.repeat(base, select.getWidth());
}
if (out < 0) {
state.setPort(n + OUT, outDefault, Plexers.DELAY);
state.setPort(n + EN_OUT, enabled ? Value.TRUE : Value.FALSE, Plexers.DELAY);
state.setPort(n + GS, Value.FALSE, Plexers.DELAY);
} else {
state.setPort(n + OUT, Value.createKnown(select, out), Plexers.DELAY);
state.setPort(n + EN_OUT, Value.FALSE, Plexers.DELAY);
state.setPort(n + GS, Value.TRUE, Plexers.DELAY);
}
}
use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.
the class GrayIncrementer method propagate.
/**
* Computes the current output for this component. This method is invoked
* any time any of the inputs change their values; it may also be invoked in
* other circumstances, even if there is no reason to expect it to change
* anything.
*/
@Override
public void propagate(InstanceState state) {
// First we retrieve the value being fed into the input. Note that in
// the setPorts invocation above, the component's input was included at
// index 0 in the parameter array, so we use 0 as the parameter below.
Value in = state.getPortValue(0);
// Now compute the output. We've farmed this out to a helper method,
// since the same logic is needed for the library's other components.
Value out = nextGray(in);
// Finally we propagate the output into the circuit. The first parameter
// is 1 because in our list of ports (configured by invocation of
// setPorts above) the output is at index 1. The second parameter is the
// value we want to send on that port. And the last parameter is its
// "delay" - the number of steps it will take for the output to update
// after its input.
state.setPort(1, out, out.getWidth() + 1);
}
use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.
the class CircuitBuilder method placeComponents.
//
// placeComponents
//
/**
* @param circuit
* the circuit where to place the components.
* @param layout
* the layout specifying the gates to place there.
* @param x
* the left edge of where the layout should be placed.
* @param y
* the top edge of where the layout should be placed.
* @param inputData
* information about how to reach inputs.
* @param output
* a point to which the output should be connected.
*/
private static void placeComponents(CircuitMutation result, Layout layout, int x, int y, InputData inputData, Location output) {
if (layout.inputName != null) {
int inputX = inputData.getSpineX(layout.inputName);
Location input = Location.create(inputX, output.getY());
inputData.registerConnection(layout.inputName, input);
result.add(Wire.create(input, output));
return;
}
Location compOutput = Location.create(x + layout.width, output.getY());
Component parent = layout.factory.createComponent(compOutput, layout.attrs);
result.add(parent);
if (!compOutput.equals(output)) {
result.add(Wire.create(compOutput, output));
}
// handle a NOT gate pattern implemented with NAND as a special case
if (layout.factory == NandGate.FACTORY && layout.subLayouts.length == 1 && layout.subLayouts[0].inputName == null) {
Layout sub = layout.subLayouts[0];
Location input0 = parent.getEnd(1).getLocation();
Location input1 = parent.getEnd(2).getLocation();
int midX = input0.getX() - 20;
Location subOutput = Location.create(midX, output.getY());
Location midInput0 = Location.create(midX, input0.getY());
Location midInput1 = Location.create(midX, input1.getY());
result.add(Wire.create(subOutput, midInput0));
result.add(Wire.create(midInput0, input0));
result.add(Wire.create(subOutput, midInput1));
result.add(Wire.create(midInput1, input1));
int subX = x + layout.subX - sub.width;
placeComponents(result, sub, subX, y + sub.y, inputData, subOutput);
return;
}
if (layout.subLayouts.length == parent.getEnds().size() - 2) {
int index = layout.subLayouts.length / 2 + 1;
Object factory = parent.getFactory();
if (factory instanceof AbstractGate) {
Value val = ((AbstractGate) factory).getIdentity();
Integer valInt = Integer.valueOf(val.toIntValue());
Location loc = parent.getEnd(index).getLocation();
AttributeSet attrs = Constant.FACTORY.createAttributeSet();
attrs.setValue(Constant.ATTR_VALUE, valInt);
result.add(Constant.FACTORY.createComponent(loc, attrs));
}
}
for (int i = 0; i < layout.subLayouts.length; i++) {
Layout sub = layout.subLayouts[i];
int inputIndex = i + 1;
Location subDest = parent.getEnd(inputIndex).getLocation();
int subOutputY = y + sub.y + sub.outputY;
if (sub.inputName != null) {
int destY = subDest.getY();
if (i == 0 && destY < subOutputY || i == layout.subLayouts.length - 1 && destY > subOutputY) {
subOutputY = destY;
}
}
Location subOutput;
int numSubs = layout.subLayouts.length;
if (subOutputY == subDest.getY()) {
subOutput = subDest;
} else {
int back;
if (i < numSubs / 2) {
if (subOutputY < subDest.getY()) {
// bending upward
back = i;
} else {
back = ((numSubs - 1) / 2) - i;
}
} else {
if (subOutputY > subDest.getY()) {
// bending downward
back = numSubs - 1 - i;
} else {
back = i - (numSubs / 2);
}
}
int subOutputX = subDest.getX() - 20 - 10 * back;
subOutput = Location.create(subOutputX, subOutputY);
Location mid = Location.create(subOutputX, subDest.getY());
result.add(Wire.create(subOutput, mid));
result.add(Wire.create(mid, subDest));
}
int subX = x + layout.subX - sub.width;
int subY = y + sub.y;
placeComponents(result, sub, subX, subY, inputData, subOutput);
}
}
use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.
the class ControlledBuffer method propagate.
@Override
public void propagate(InstanceState state) {
Value control = state.getPortValue(2);
BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
if (control == Value.TRUE) {
Value in = state.getPortValue(1);
state.setPort(0, isInverter ? in.not() : in, GateAttributes.DELAY);
} else if (control == Value.ERROR || control == Value.UNKNOWN) {
state.setPort(0, Value.createError(width), GateAttributes.DELAY);
} else {
Value out;
if (control == Value.UNKNOWN || control == Value.NIL) {
AttributeSet opts = state.getProject().getOptions().getAttributeSet();
if (opts.getValue(Options.ATTR_GATE_UNDEFINED).equals(Options.GATE_UNDEFINED_ERROR)) {
out = Value.createError(width);
} else {
out = Value.createUnknown(width);
}
} else {
out = Value.createUnknown(width);
}
state.setPort(0, out, GateAttributes.DELAY);
}
}
Aggregations