Subversion Repositories Code-Repo

Rev

Blame | Last modification | View Log | RSS feed

#include "Gate_XOR.h"

Gate_XOR::Gate_XOR(int gateID, gType type, int numInputs, int gateLevel)
    : Gate_BASE(gateID, type, numInputs, gateLevel)
{
    textRect = QRectF(BORDER_OFFSET * 2.2, BORDER_OFFSET, xSize - BORDER_OFFSET * 3, ySize - BORDER_OFFSET * 2);
}

void Gate_XOR::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);

#ifdef _DEBUG
    painter->save();
    painter->setPen((auxSelected) ? debugSelectedPen : debugPen);
    painter->setBrush(debugBrush);
    painter->drawRect(0, 0, xSize, ySize);
    painter->drawRect(textRect);
    if (numInputs < 2) {
        painter->setPen(debugErrorPen);
        painter->drawLine(0, 0, xSize, ySize);
        painter->drawLine(xSize, 0, 0, ySize);
    }
    painter->restore();
#endif

    if (auxSelected) painter->setPen(highlightedPen);
    else painter->setPen(defaultPen);
    painter->setBrush(defaultBrush);

    // Draw circles indicating I/O points
    for (int i = 0; i < numInputs; i++) {
        painter->drawEllipse(inputPoints[i], INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);
    }
    painter->drawEllipse(outputPoint, INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);

    // Draw gate outline
    QRectF leftArcBox = QRectF(0, BORDER_OFFSET, BORDER_OFFSET + (numInputs * BORDER_OFFSET / 2), ySize - BORDER_OFFSET * 2);
    leftArcBox.translate(BORDER_OFFSET - (BORDER_OFFSET + (numInputs * BORDER_OFFSET / 2)) / 2, 0);
    painter->drawArc(leftArcBox, -90 * 16, 180 * 16);

    QRectF leftArcBox2 = QRectF(0, BORDER_OFFSET, BORDER_OFFSET + (numInputs * BORDER_OFFSET / 2), ySize - BORDER_OFFSET * 2);
    leftArcBox2.translate(BORDER_OFFSET * 2 - (BORDER_OFFSET + (numInputs * BORDER_OFFSET / 2)) / 2, 0);
    painter->drawArc(leftArcBox2, -90 * 16, 180 * 16);

    QRectF rightArcBox = QRectF(0, BORDER_OFFSET, 2 * (xSize - (BORDER_OFFSET * 2)) - BORDER_OFFSET * 2, ySize - (BORDER_OFFSET * 2));
    rightArcBox.translate(-xSize + BORDER_OFFSET * 5, 0);
    painter->drawArc(rightArcBox, 0, 90 * 16);
    painter->drawArc(rightArcBox, -90 * 16, 90 * 16);

    // Draw I/O lines
    int a = leftArcBox.width() / 2;
    int b = leftArcBox.height() / 2;
    QPointF arcCenter = leftArcBox.center();
    for (int i = 0; i < numInputs; i++) {
        float xPt = sqrt((1 - pow(inputPoints[i].y() - arcCenter.y(), 2) / (b * b)) * (a * a)) + arcCenter.x();
        painter->drawLine(inputPoints[i], QPointF(xPt, inputPoints[i].y()));
    }
    painter->drawLine(outputPoint, QPointF(outputPoint.x() - BORDER_OFFSET, outputPoint.y()));

    // Draw text showing gate ID
    painter->setPen(defaultPen);
    painter->setFont(defaultFont);
    painter->drawText(textRect, Qt::AlignCenter, QString::number(gateID));

    // If enqueued, draw circle around gate ID
    if (enqueued) {
        painter->drawEllipse(textRect.center(), ENQUEUED_CIRCLE_WIDTH, ENQUEUED_CIRCLE_WIDTH);
    }
}

void Gate_XOR::simulateToOutput()
{
    // Save initial values to compare to later
    logicValue initValue = outputValue;
    logicValue initFaultyValue = outputFaultyValue;

    // Compute new output values
    bool undefined = false, undefinedFaulty = false;
    bool allOnes = true, allOnesFaulty = true;
    bool allZeros = true, allZerosFaulty = true;
    for (int i = 0; i < numInputs; i++) {
        if (inputValues[i] == logicValue_X) undefined = true;
        if (inputValues[i] == logicValue_0) allOnes = false;
        if (inputValues[i] == logicValue_1) allZeros = false;

        if (inputFaultyValues[i] == logicValue_X) undefinedFaulty = true;
        if (inputFaultyValues[i] == logicValue_0) allOnesFaulty = false;
        if (inputFaultyValues[i] == logicValue_1) allZerosFaulty = false;
    }

    if (!allOnes && !allZeros) outputValue = logicValue_1;
    else if ((allOnes || allZeros) && !undefined) outputValue = logicValue_0;
    else outputValue = logicValue_X;

    if (!allOnesFaulty && !allZerosFaulty) outputFaultyValue = logicValue_1;
    else if ((allOnesFaulty || allZerosFaulty) && !undefinedFaulty) outputFaultyValue = logicValue_0;
    else outputFaultyValue = logicValue_X;

    // If outputs have changed, queue the connected gate for simulation
    if (outputValue != initValue || outputFaultyValue != initFaultyValue)
        emit enqueueSim(this);

    // Update connected wire values
    for (int i = 0; i < gateOutputWires.size(); i++) {
        gateOutputWires[i]->setValue(outputValue, outputFaultyValue, false);
    }
}