Subversion Repositories Code-Repo

Rev

Blame | Last modification | View Log | RSS feed

#include "Gate_BUFFER.h"

Gate_BUFFER::Gate_BUFFER(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;
    this->ySize = BASE_GATE_SIZE_Y + ADDITONAL_INPUTS;

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

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

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

void Gate_BUFFER::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
    QPointF points[3] = {
        QPointF(BORDER_OFFSET, BORDER_OFFSET),
        QPointF(xSize - BORDER_OFFSET, ySize / 2),
        QPointF(BORDER_OFFSET, ySize - BORDER_OFFSET)
    };
    painter->drawPolygon(points, 3);

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