use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class Demultiplexer method paintInstance.
@Override
public void paintInstance(InstancePainter painter) {
Graphics g = painter.getGraphics();
Bounds bds = painter.getBounds();
Direction facing = painter.getAttributeValue(StdAttr.FACING);
BitWidth select = painter.getAttributeValue(Plexers.ATTR_SELECT);
boolean enable = painter.getAttributeValue(Plexers.ATTR_ENABLE).booleanValue();
int outputs = 1 << select.getWidth();
// draw select and enable inputs
GraphicsUtil.switchToWidth(g, 3);
boolean vertical = facing == Direction.NORTH || facing == Direction.SOUTH;
Object selectLoc = painter.getAttributeValue(Plexers.ATTR_SELECT_LOC);
int selMult = selectLoc == Plexers.SELECT_BOTTOM_LEFT ? 1 : -1;
int dx = vertical ? selMult : 0;
int dy = vertical ? 0 : -selMult;
if (outputs == 2) {
// draw select wire
Location sel = painter.getInstance().getPortLocation(outputs);
if (painter.getShowState()) {
g.setColor(painter.getPortValue(outputs).getColor());
}
g.drawLine(sel.getX(), sel.getY(), sel.getX() + 2 * dx, sel.getY() + 2 * dy);
}
if (enable) {
Location en = painter.getInstance().getPortLocation(outputs + 1);
if (painter.getShowState()) {
g.setColor(painter.getPortValue(outputs + 1).getColor());
}
int len = outputs == 2 ? 6 : 4;
g.drawLine(en.getX(), en.getY(), en.getX() + len * dx, en.getY() + len * dy);
}
GraphicsUtil.switchToWidth(g, 1);
// draw a circle indicating where the select input is located
Multiplexer.drawSelectCircle(g, bds, painter.getInstance().getPortLocation(outputs));
// draw "0" next to first input
int x0;
int y0;
int halign;
if (facing == Direction.WEST) {
x0 = 3;
y0 = 15 + (outputs == 2 ? 5 : 0);
halign = GraphicsUtil.H_LEFT;
} else if (facing == Direction.NORTH) {
x0 = 10 + (outputs == 2 ? 5 : 0);
y0 = 15;
halign = GraphicsUtil.H_CENTER;
} else if (facing == Direction.SOUTH) {
x0 = 10 + (outputs == 2 ? 5 : 0);
y0 = bds.getHeight() - 3;
halign = GraphicsUtil.H_CENTER;
} else {
x0 = bds.getWidth() - 3;
y0 = 15 + (outputs == 2 ? 5 : 0);
halign = GraphicsUtil.H_RIGHT;
}
g.setColor(Color.GRAY);
GraphicsUtil.drawText(g, "0", bds.getX() + x0, bds.getY() + y0, halign, GraphicsUtil.V_BASELINE);
// draw trapezoid, "DMX" label, and ports
g.setColor(Color.BLACK);
if (outputs == 2) {
if (facing == Direction.EAST || facing == Direction.WEST) {
Plexers.drawTrapezoid(g, Bounds.create(bds.getX(), bds.getY() + 5, bds.getWidth(), bds.getHeight() - 10), facing.reverse(), 10);
} else {
Plexers.drawTrapezoid(g, Bounds.create(bds.getX() + 5, bds.getY(), bds.getWidth() - 10, bds.getHeight()), facing.reverse(), 10);
}
} else {
Plexers.drawTrapezoid(g, bds, facing.reverse(), 20);
}
GraphicsUtil.drawCenteredText(g, "DMX", bds.getX() + bds.getWidth() / 2, bds.getY() + bds.getHeight() / 2);
painter.drawPorts();
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class Demultiplexer method paintGhost.
@Override
public void paintGhost(InstancePainter painter) {
Direction facing = painter.getAttributeValue(StdAttr.FACING);
BitWidth select = painter.getAttributeValue(Plexers.ATTR_SELECT);
Bounds bds = painter.getBounds();
if (select.getWidth() == 1) {
if (facing == Direction.EAST || facing == Direction.WEST) {
Plexers.drawTrapezoid(painter.getGraphics(), Bounds.create(bds.getX(), bds.getY() + 5, bds.getWidth(), bds.getHeight() - 10), facing.reverse(), 10);
} else {
Plexers.drawTrapezoid(painter.getGraphics(), Bounds.create(bds.getX() + 5, bds.getY(), bds.getWidth() - 10, bds.getHeight()), facing.reverse(), 10);
}
} else {
Plexers.drawTrapezoid(painter.getGraphics(), bds, facing.reverse(), 20);
}
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class Multiplexer method propagate.
@Override
public void propagate(InstanceState state) {
BitWidth data = state.getAttributeValue(StdAttr.WIDTH);
BitWidth select = state.getAttributeValue(Plexers.ATTR_SELECT);
boolean enable = state.getAttributeValue(Plexers.ATTR_ENABLE).booleanValue();
int inputs = 1 << select.getWidth();
Value en = enable ? state.getPortValue(inputs + 1) : Value.TRUE;
Value out;
if (en == Value.FALSE) {
Object opt = state.getAttributeValue(Plexers.ATTR_DISABLED);
Value base = opt == Plexers.DISABLED_ZERO ? Value.FALSE : Value.UNKNOWN;
out = Value.repeat(base, data.getWidth());
} else if (en == Value.ERROR && state.isPortConnected(inputs + 1)) {
out = Value.createError(data);
} else {
Value sel = state.getPortValue(inputs);
if (sel.isFullyDefined()) {
out = state.getPortValue(sel.toIntValue());
} else if (sel.isErrorValue()) {
out = Value.createError(data);
} else {
out = Value.createUnknown(data);
}
}
state.setPort(inputs + (enable ? 2 : 1), out, Plexers.DELAY);
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class Multiplexer method paintInstance.
@Override
public void paintInstance(InstancePainter painter) {
Graphics g = painter.getGraphics();
Bounds bds = painter.getBounds();
Direction facing = painter.getAttributeValue(StdAttr.FACING);
BitWidth select = painter.getAttributeValue(Plexers.ATTR_SELECT);
boolean enable = painter.getAttributeValue(Plexers.ATTR_ENABLE).booleanValue();
int inputs = 1 << select.getWidth();
// draw stubs for select/enable inputs that aren't on instance boundary
GraphicsUtil.switchToWidth(g, 3);
boolean vertical = facing != Direction.NORTH && facing != Direction.SOUTH;
Object selectLoc = painter.getAttributeValue(Plexers.ATTR_SELECT_LOC);
int selMult = selectLoc == Plexers.SELECT_BOTTOM_LEFT ? 1 : -1;
int dx = vertical ? 0 : -selMult;
int dy = vertical ? selMult : 0;
if (inputs == 2) {
// draw select wire
Location pt = painter.getInstance().getPortLocation(inputs);
if (painter.getShowState()) {
g.setColor(painter.getPortValue(inputs).getColor());
}
g.drawLine(pt.getX() - 2 * dx, pt.getY() - 2 * dy, pt.getX(), pt.getY());
}
if (enable) {
Location en = painter.getInstance().getPortLocation(inputs + 1);
if (painter.getShowState()) {
g.setColor(painter.getPortValue(inputs + 1).getColor());
}
int len = inputs == 2 ? 6 : 4;
g.drawLine(en.getX() - len * dx, en.getY() - len * dy, en.getX(), en.getY());
}
GraphicsUtil.switchToWidth(g, 1);
// draw a circle indicating where the select input is located
Multiplexer.drawSelectCircle(g, bds, painter.getInstance().getPortLocation(inputs));
// draw a 0 indicating where the numbering starts for inputs
int x0;
int y0;
int halign;
if (facing == Direction.WEST) {
x0 = bds.getX() + bds.getWidth() - 3;
y0 = bds.getY() + 15 + (inputs == 2 ? 5 : 0);
halign = GraphicsUtil.H_RIGHT;
} else if (facing == Direction.NORTH) {
x0 = bds.getX() + 10 + (inputs == 2 ? 5 : 0);
y0 = bds.getY() + bds.getHeight() - 2;
halign = GraphicsUtil.H_CENTER;
} else if (facing == Direction.SOUTH) {
x0 = bds.getX() + 10 + (inputs == 2 ? 5 : 0);
y0 = bds.getY() + 12;
halign = GraphicsUtil.H_CENTER;
} else {
x0 = bds.getX() + 3;
y0 = bds.getY() + 15 + (inputs == 2 ? 5 : 0);
halign = GraphicsUtil.H_LEFT;
}
g.setColor(Color.GRAY);
GraphicsUtil.drawText(g, "0", x0, y0, halign, GraphicsUtil.V_BASELINE);
// draw the trapezoid, "MUX" string, the individual ports
g.setColor(Color.BLACK);
if (inputs == 2) {
if (facing == Direction.EAST || facing == Direction.WEST) {
Plexers.drawTrapezoid(g, Bounds.create(bds.getX(), bds.getY() + 5, bds.getWidth(), bds.getHeight() - 10), facing, 10);
} else {
Plexers.drawTrapezoid(g, Bounds.create(bds.getX() + 5, bds.getY(), bds.getWidth() - 10, bds.getHeight()), facing, 10);
}
} else {
Plexers.drawTrapezoid(g, bds, facing, 20);
}
GraphicsUtil.drawCenteredText(g, "MUX", bds.getX() + bds.getWidth() / 2, bds.getY() + bds.getHeight() / 2);
painter.drawPorts();
}
use of com.cburch.logisim.data.BitWidth in project logisim-evolution by reds-heig.
the class Multiplexer method getOffsetBounds.
@Override
public Bounds getOffsetBounds(AttributeSet attrs) {
Direction dir = attrs.getValue(StdAttr.FACING);
BitWidth select = attrs.getValue(Plexers.ATTR_SELECT);
int inputs = 1 << select.getWidth();
if (inputs == 2) {
return Bounds.create(-30, -25, 30, 50).rotate(Direction.EAST, dir, 0, 0);
} else {
int offs = -(inputs / 2) * 10 - 10;
int length = inputs * 10 + 20;
return Bounds.create(-40, offs, 40, length).rotate(Direction.EAST, dir, 0, 0);
}
}
Aggregations