use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class CounterPoker method paint.
/**
* Draws an indicator that the caret is being selected. Here, we'll draw a
* red rectangle around the value.
*/
@Override
public void paint(InstancePainter painter) {
Bounds bds = painter.getBounds();
BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
int len = (width.getWidth() + 3) / 4;
Graphics g = painter.getGraphics();
g.setColor(Color.RED);
// width of caret rectangle
int wid = 7 * len + 2;
// height of caret rectangle
int ht = 16;
g.drawRect(bds.getX() + (bds.getWidth() - wid) / 2, bds.getY() + (bds.getHeight() - ht) / 2, wid, ht);
g.setColor(Color.BLACK);
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class GrayCounter method propagate.
@Override
public void propagate(InstanceState state) {
// This is the same as with SimpleGrayCounter, except that we use the
// StdAttr.WIDTH attribute to determine the bit width to work with.
BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
CounterData cur = CounterData.get(state, width);
boolean trigger = cur.updateClock(state.getPortValue(0));
if (trigger)
cur.setValue(GrayIncrementer.nextGray(cur.getValue()));
state.setPort(1, cur.getValue(), 9);
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class GrayCounter method paintInstance.
@Override
public void paintInstance(InstancePainter painter) {
// This is essentially the same as with SimpleGrayCounter, except for
// the invocation of painter.drawLabel to make the label be drawn.
painter.drawBounds();
painter.drawClock(0, Direction.EAST);
painter.drawPort(1);
painter.drawLabel();
if (painter.getShowState()) {
BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
CounterData state = CounterData.get(painter, width);
Bounds bds = painter.getBounds();
GraphicsUtil.drawCenteredText(painter.getGraphics(), StringUtil.toHexString(width.getWidth(), state.getValue().toIntValue()), bds.getX() + bds.getWidth() / 2, bds.getY() + bds.getHeight() / 2);
}
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class GrayIncrementer method nextGray.
/*
* Note that there are no instance variables. There is only one instance of
* this class created, which manages all instances of the component. Any
* information associated with individual instances should be handled
* through attributes. For GrayIncrementer, each instance has a "bit width"
* that it works with, and so we'll have an attribute.
*/
/**
* Computes the next gray value in the sequence after prev. This static
* method just does some bit twiddling; it doesn't have much to do with
* Logisim except that it manipulates Value and BitWidth objects.
*/
static Value nextGray(Value prev) {
BitWidth bits = prev.getBitWidth();
if (!prev.isFullyDefined())
return Value.createError(bits);
int x = prev.toIntValue();
// compute parity of x
int ct = (x >> 16) ^ x;
ct = (ct >> 8) ^ ct;
ct = (ct >> 4) ^ ct;
ct = (ct >> 2) ^ ct;
ct = (ct >> 1) ^ ct;
if ((ct & 1) == 0) {
// if parity is even, flip 1's bit
x = x ^ 1;
} else {
// else flip bit just above last 1
// first compute the last 1
int y = x ^ (x & (x - 1));
y = (y << 1) & bits.getMask();
x = (y == 0 ? 0 : x ^ y);
}
return Value.createKnown(bits, x);
}
use of com.cburch.logisim.data.BitWidth 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