Search in sources :

Example 16 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class Pin method paintInstance.

@Override
public void paintInstance(InstancePainter painter) {
    PinAttributes attrs = (PinAttributes) painter.getAttributeSet();
    Graphics g = painter.getGraphics();
    // intentionally with no
    Bounds bds = painter.getInstance().getBounds();
    // graphics object - we
    // don't want label
    // included
    int x = bds.getX();
    int y = bds.getY();
    GraphicsUtil.switchToWidth(g, 2);
    g.setColor(Color.black);
    if (attrs.type == EndData.OUTPUT_ONLY) {
        if (attrs.width.getWidth() == 1) {
            g.drawOval(x + 1, y + 1, bds.getWidth() - 1, bds.getHeight() - 1);
        } else {
            g.drawRoundRect(x + 1, y + 1, bds.getWidth() - 1, bds.getHeight() - 1, 12, 12);
        }
    } else {
        g.drawRect(x + 1, y + 1, bds.getWidth() - 1, bds.getHeight() - 1);
    }
    painter.drawLabel();
    if (!painter.getShowState()) {
        g.setColor(Color.BLACK);
        GraphicsUtil.drawCenteredText(g, "x" + attrs.width.getWidth(), bds.getX() + bds.getWidth() / 2, bds.getY() + bds.getHeight() / 2);
    } else {
        PinState state = getState(painter);
        if (attrs.width.getWidth() <= 1) {
            Value found = state.foundValue;
            g.setColor(found.getColor());
            g.fillOval(x + 4, y + 4, 13, 13);
            if (attrs.width.getWidth() == 1) {
                g.setColor(Color.WHITE);
                GraphicsUtil.drawCenteredText(g, state.intendedValue.toDisplayString(), x + 11, y + 9);
            }
        } else {
            Probe.paintValue(painter, state.intendedValue);
        }
    }
    painter.drawPorts();
}
Also used : Graphics(java.awt.Graphics) Bounds(com.cburch.logisim.data.Bounds) Value(com.cburch.logisim.data.Value)

Example 17 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class Pin method setValue.

public void setValue(InstanceState state, Value value) {
    PinAttributes attrs = (PinAttributes) state.getAttributeSet();
    Object pull = attrs.pull;
    PinState myState = getState(state);
    if (value == Value.NIL) {
        myState.intendedValue = Value.createUnknown(attrs.width);
    } else {
        Value sendValue;
        if (pull == PULL_NONE || pull == null || value.isFullyDefined()) {
            sendValue = value;
        } else {
            Value[] bits = value.getAll();
            if (pull == PULL_UP) {
                for (int i = 0; i < bits.length; i++) {
                    if (bits[i] != Value.FALSE)
                        bits[i] = Value.TRUE;
                }
            } else if (pull == PULL_DOWN) {
                for (int i = 0; i < bits.length; i++) {
                    if (bits[i] != Value.TRUE)
                        bits[i] = Value.FALSE;
                }
            }
            sendValue = Value.create(bits);
        }
        myState.intendedValue = sendValue;
    }
}
Also used : Value(com.cburch.logisim.data.Value)

Example 18 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class Probe method propagate.

@Override
public void propagate(InstanceState state) {
    StateData oldData = (StateData) state.getData();
    Value oldValue = oldData == null ? Value.NIL : oldData.curValue;
    Value newValue = state.getPortValue(0);
    boolean same = oldValue == null ? newValue == null : oldValue.equals(newValue);
    if (!same) {
        if (oldData == null) {
            oldData = new StateData();
            oldData.curValue = newValue;
            state.setData(oldData);
        } else {
            oldData.curValue = newValue;
        }
        int oldWidth = oldValue == null ? 1 : oldValue.getBitWidth().getWidth();
        int newWidth = newValue.getBitWidth().getWidth();
        if (oldWidth != newWidth) {
            ProbeAttributes attrs = (ProbeAttributes) state.getAttributeSet();
            attrs.width = newValue.getBitWidth();
            state.getInstance().recomputeBounds();
            configureLabel(state.getInstance());
        }
    }
}
Also used : Value(com.cburch.logisim.data.Value)

Example 19 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class Counter method propagate.

@Override
public void propagate(InstanceState state) {
    RegisterData data = (RegisterData) state.getData();
    if (data == null) {
        data = new RegisterData(state.getAttributeValue(StdAttr.WIDTH));
        state.setData(data);
    }
    BitWidth dataWidth = state.getAttributeValue(StdAttr.WIDTH);
    Object triggerType = state.getAttributeValue(StdAttr.EDGE_TRIGGER);
    int max = state.getAttributeValue(ATTR_MAX).intValue();
    Value clock = state.getPortValue(CK);
    boolean triggered = data.updateClock(clock, triggerType);
    Value newValue;
    boolean carry;
    if (state.getPortValue(CLR) == Value.TRUE) {
        newValue = Value.createKnown(dataWidth, 0);
        carry = false;
    } else {
        boolean ld = state.getPortValue(LD) == Value.TRUE;
        boolean en = state.getPortValue(EN) != Value.FALSE;
        boolean UpCount = state.getPortValue(UD) != Value.FALSE;
        Value oldVal = data.value;
        Value newVal;
        if (!triggered) {
            newVal = oldVal;
        } else if (ld) {
            Value in = state.getPortValue(IN);
            newVal = in;
            if (newVal.toIntValue() > max)
                newVal = Value.createKnown(dataWidth, newVal.toIntValue() & max);
        } else if (!oldVal.isFullyDefined()) {
            newVal = oldVal;
        } else if (en) {
            int goal = (UpCount) ? max : 0;
            if (oldVal.toIntValue() == goal) {
                Object onGoal = state.getAttributeValue(ATTR_ON_GOAL);
                if (onGoal == ON_GOAL_WRAP) {
                    newVal = Value.createKnown(dataWidth, (UpCount) ? 0 : max);
                } else if (onGoal == ON_GOAL_STAY) {
                    newVal = oldVal;
                } else if (onGoal == ON_GOAL_LOAD) {
                    Value in = state.getPortValue(IN);
                    newVal = in;
                    if (newVal.toIntValue() > max)
                        newVal = Value.createKnown(dataWidth, newVal.toIntValue() & max);
                } else if (onGoal == ON_GOAL_CONT) {
                    newVal = Value.createKnown(dataWidth, (UpCount) ? oldVal.toIntValue() + 1 : oldVal.toIntValue() - 1);
                } else {
                    logger.error("Invalid goal attribute {}", onGoal);
                    newVal = Value.createKnown(dataWidth, ld ? max : 0);
                }
            } else {
                newVal = Value.createKnown(dataWidth, (UpCount) ? oldVal.toIntValue() + 1 : oldVal.toIntValue() - 1);
            }
        } else {
            newVal = oldVal;
        }
        newValue = newVal;
        carry = newVal.toIntValue() == (UpCount ? max : 0);
    /*
			 * I would want this if I were worried about the carry signal
			 * outrunning the clock. But the component's delay should be enough
			 * to take care of it. if (carry) { if (triggerType ==
			 * StdAttr.TRIG_FALLING) { carry = clock == Value.TRUE; } else {
			 * carry = clock == Value.FALSE; } }
			 */
    }
    data.value = newValue;
    state.setPort(OUT, newValue, DELAY);
    state.setPort(CARRY, carry ? Value.TRUE : Value.FALSE, DELAY);
}
Also used : BitWidth(com.cburch.logisim.data.BitWidth) Value(com.cburch.logisim.data.Value)

Example 20 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class Counter method DrawDataBlock.

private void DrawDataBlock(InstancePainter painter, int xpos, int ypos, int BitNr, int NrOfBits) {
    int RealYpos = ypos + BitNr * 20;
    boolean first = BitNr == 0;
    boolean last = BitNr == (NrOfBits - 1);
    Graphics g = painter.getGraphics();
    Font font = g.getFont();
    g.setFont(font.deriveFont(7.0f));
    GraphicsUtil.switchToWidth(g, 2);
    g.drawRect(xpos + 20, RealYpos, SymbolWidth(NrOfBits), 20);
    /* Input Line */
    if (NrOfBits > 1) {
        g.drawLine(xpos + 10, RealYpos + 10, xpos + 20, RealYpos + 10);
        g.drawLine(xpos + 5, RealYpos + 5, xpos + 10, RealYpos + 10);
    } else {
        g.drawLine(xpos, RealYpos + 10, xpos + 20, RealYpos + 10);
    }
    /* Ouput Line */
    if (NrOfBits > 1) {
        g.drawLine(xpos + 20 + SymbolWidth(NrOfBits), RealYpos + 10, xpos + 30 + SymbolWidth(NrOfBits), RealYpos + 10);
        g.drawLine(xpos + 30 + SymbolWidth(NrOfBits), RealYpos + 10, xpos + 35 + SymbolWidth(NrOfBits), RealYpos + 5);
    } else {
        g.drawLine(xpos + 20 + SymbolWidth(NrOfBits), RealYpos + 10, xpos + 40 + SymbolWidth(NrOfBits), RealYpos + 10);
    }
    g.setColor(Color.BLACK);
    if (NrOfBits > 1) {
        GraphicsUtil.drawText(g, Integer.toString(BitNr), xpos + 30 + SymbolWidth(NrOfBits), RealYpos + 8, GraphicsUtil.H_RIGHT, GraphicsUtil.V_BASELINE);
        GraphicsUtil.drawText(g, Integer.toString(BitNr), xpos + 10, RealYpos + 8, GraphicsUtil.H_LEFT, GraphicsUtil.V_BASELINE);
    }
    g.setFont(font);
    GraphicsUtil.drawText(g, "1,6D", xpos + 21, RealYpos + 10, GraphicsUtil.H_LEFT, GraphicsUtil.V_CENTER);
    int LineWidth = (NrOfBits == 1) ? 2 : 5;
    GraphicsUtil.switchToWidth(g, LineWidth);
    if (first) {
        painter.drawPort(IN);
        painter.drawPort(OUT);
        if (NrOfBits > 1) {
            g.drawLine(xpos, RealYpos, xpos + 5, RealYpos + 5);
            g.drawLine(xpos + 35 + SymbolWidth(NrOfBits), RealYpos + 5, xpos + 40 + SymbolWidth(NrOfBits), RealYpos);
            g.drawLine(xpos + 5, RealYpos + 5, xpos + 5, RealYpos + 20);
            g.drawLine(xpos + 35 + SymbolWidth(NrOfBits), RealYpos + 5, xpos + 35 + SymbolWidth(NrOfBits), RealYpos + 20);
        }
    } else if (last) {
        g.drawLine(xpos + 5, RealYpos, xpos + 5, RealYpos + 5);
        g.drawLine(xpos + 35 + SymbolWidth(NrOfBits), RealYpos, xpos + 35 + SymbolWidth(NrOfBits), RealYpos + 5);
    } else {
        g.drawLine(xpos + 5, RealYpos, xpos + 5, RealYpos + 20);
        g.drawLine(xpos + 35 + SymbolWidth(NrOfBits), RealYpos, xpos + 35 + SymbolWidth(NrOfBits), RealYpos + 20);
    }
    GraphicsUtil.switchToWidth(g, 1);
    RegisterData state = (RegisterData) painter.getData();
    if (painter.getShowState() && (state != null)) {
        /* Here we draw the bit value */
        Value val = state.value;
        BitWidth widthVal = painter.getAttributeValue(StdAttr.WIDTH);
        int width = widthVal == null ? 8 : widthVal.getWidth();
        int xcenter = (SymbolWidth(width) / 2) + 10;
        String value = "";
        if (val.isFullyDefined()) {
            g.setColor(Color.LIGHT_GRAY);
            value = ((1 << BitNr) & val.toIntValue()) != 0 ? "1" : "0";
        } else if (val.isUnknown()) {
            g.setColor(Color.BLUE);
            value = "?";
        } else {
            g.setColor(Color.RED);
            value = "!";
        }
        g.fillRect(xpos + xcenter + 16, RealYpos + 4, 8, 16);
        if (val.isFullyDefined())
            g.setColor(Color.DARK_GRAY);
        else
            g.setColor(Color.YELLOW);
        GraphicsUtil.drawText(g, value, xpos + xcenter + 20, RealYpos + 10, GraphicsUtil.H_CENTER, GraphicsUtil.V_CENTER);
        g.setColor(Color.BLACK);
    }
}
Also used : Graphics(java.awt.Graphics) BitWidth(com.cburch.logisim.data.BitWidth) Value(com.cburch.logisim.data.Value) Font(java.awt.Font)

Aggregations

Value (com.cburch.logisim.data.Value)89 BitWidth (com.cburch.logisim.data.BitWidth)30 Graphics (java.awt.Graphics)15 Location (com.cburch.logisim.data.Location)9 Color (java.awt.Color)8 Bounds (com.cburch.logisim.data.Bounds)7 InstanceDataSingleton (com.cburch.logisim.instance.InstanceDataSingleton)7 AttributeSet (com.cburch.logisim.data.AttributeSet)6 InstanceState (com.cburch.logisim.instance.InstanceState)6 Direction (com.cburch.logisim.data.Direction)3 Instance (com.cburch.logisim.instance.Instance)3 Port (com.cburch.logisim.instance.Port)3 CircuitState (com.cburch.logisim.circuit.CircuitState)2 Component (com.cburch.logisim.comp.Component)2 FailException (com.cburch.logisim.data.FailException)2 TestException (com.cburch.logisim.data.TestException)2 Dimension (java.awt.Dimension)2 FontMetrics (java.awt.FontMetrics)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2