Subversion Repositories Code-Repo

Rev

Blame | Last modification | View Log | RSS feed

#include "Gate_DFF.h"

Gate_DFF::Gate_DFF(int gateID, gType type, int numInputs, int gateLevel)
    : Gate_BASE(gateID, type, numInputs, gateLevel)
{
    // Override base gate to limit to one I/O point only
    this->xSize = BASE_GATE_SIZE_X + ADDITONAL_INPUTS;
    this->ySize = BASE_GATE_SIZE_Y + ADDITONAL_INPUTS * 2;

    QPointF point = QPointF(0, ADDITONAL_INPUTS * 2);
    this->inputPoints[0] = point;
    QPointF scenePoint = QPointF(this->scenePos().x(), this->scenePos().y() + ADDITONAL_INPUTS * 2);
    this->gateInputPoints[0] = scenePoint;

    outputPoint = QPointF(this->xSize, ADDITONAL_INPUTS * 2);
    gateOutputPoint = QPointF(this->scenePos().x() + this->xSize, this->scenePos().y() + ADDITONAL_INPUTS * 2);

    textRect = QRectF(BORDER_OFFSET, BORDER_OFFSET * 2, xSize - BORDER_OFFSET * 2, ySize - BORDER_OFFSET * 3);
}

void Gate_DFF::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 != 1) {
        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
    painter->drawEllipse(inputPoints[0], INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);
    painter->drawEllipse(outputPoint, INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);

    // Draw gate outline
    QRectF rect = QRectF(BORDER_OFFSET, BORDER_OFFSET, xSize - BORDER_OFFSET * 2, ySize - BORDER_OFFSET * 2);
    painter->drawRect(rect);

    painter->setFont(QFont("Consolas", 20));
    painter->drawText(QPointF(BORDER_OFFSET * 1.5, BORDER_OFFSET * 2.5), "D");
    painter->drawText(QPointF(xSize - BORDER_OFFSET * 2.5, BORDER_OFFSET * 2.5), "Q");

    // Draw I/O lines
    painter->drawLine(inputPoints[0], QPointF(inputPoints[0].x() + BORDER_OFFSET, inputPoints[0].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_DFF::simulateToOutput()
{
    // Save initial values to compare to later
    logicValue initValue = outputValue;
    logicValue initFaultyValue = outputFaultyValue;

    // Compute new output values
    outputValue = inputValues[0];
    outputFaultyValue = inputFaultyValues[0];

    // 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);
    }
}