Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
335 Kevin 1
#include "Gate_NOT.h"
2
 
3
Gate_NOT::Gate_NOT(int gateID, gType type, int numInputs, int gateLevel)
4
    : Gate_BASE(gateID, type, numInputs, gateLevel)
5
{
6
    // Override base gate to limit to one I/O point only
7
    this->xSize = BASE_GATE_SIZE_X + ADDITONAL_INPUTS;
8
    this->ySize = BASE_GATE_SIZE_Y + ADDITONAL_INPUTS;
9
 
10
    QPointF point = QPointF(0, this->ySize / 2);
11
    this->inputPoints[0] = point;
12
    QPointF scenePoint = QPointF(this->scenePos().x(), this->scenePos().y() - this->ySize / 2);
13
    this->gateInputPoints[0] = scenePoint;
14
 
15
    outputPoint = QPointF(this->xSize, this->ySize / 2);
16
    gateOutputPoint = QPointF(this->scenePos().x() + this->xSize, this->scenePos().y() + this->ySize / 2);
17
 
18
    textRect = QRectF(BORDER_OFFSET, BORDER_OFFSET, xSize - BORDER_OFFSET * 4, ySize - BORDER_OFFSET * 2);
19
}
20
 
21
void Gate_NOT::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
22
{
23
    Q_UNUSED(widget);
24
    Q_UNUSED(option);
25
 
26
#ifdef _DEBUG
27
    painter->save();
28
    painter->setPen((auxSelected) ? debugSelectedPen : debugPen);
29
    painter->setBrush(debugBrush);
30
    painter->drawRect(0, 0, xSize, ySize);
31
    painter->drawRect(textRect);
32
    if (numInputs != 1) {
33
        painter->setPen(debugErrorPen);
34
        painter->drawLine(0, 0, xSize, ySize);
35
        painter->drawLine(xSize, 0, 0, ySize);
36
    }
37
    painter->restore();
38
#endif
39
 
40
    if (auxSelected) painter->setPen(highlightedPen);
41
    else painter->setPen(defaultPen);
42
    painter->setBrush(defaultBrush);
43
 
44
    // Draw circles indicating I/O points
45
    painter->drawEllipse(inputPoints[0], INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);
46
    painter->drawEllipse(outputPoint, INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);
47
 
48
    // Draw gate outline
49
    QPointF points[3] = {
50
        QPointF(BORDER_OFFSET, BORDER_OFFSET),
51
        QPointF(xSize - BORDER_OFFSET * 2, ySize / 2),
52
        QPointF(BORDER_OFFSET, ySize - BORDER_OFFSET)
53
    };
54
    painter->drawPolygon(points, 3);
55
    painter->drawEllipse(QPoint(xSize - BORDER_OFFSET - BORDER_OFFSET / 2, ySize / 2), BORDER_OFFSET / 2, BORDER_OFFSET / 2);
56
 
57
    // Draw I/O lines
58
    painter->drawLine(inputPoints[0], QPointF(inputPoints[0].x() + BORDER_OFFSET, inputPoints[0].y()));
59
    painter->drawLine(outputPoint, QPointF(outputPoint.x() - BORDER_OFFSET, outputPoint.y()));
60
 
61
    // Draw text showing gate ID
62
    painter->setPen(defaultPen);
63
    painter->setFont(defaultFont);
64
    painter->drawText(textRect, Qt::AlignCenter, QString::number(gateID));
65
 
66
    // If enqueued, draw circle around gate ID
67
    if (enqueued) {
68
        painter->drawEllipse(textRect.center(), ENQUEUED_CIRCLE_WIDTH, ENQUEUED_CIRCLE_WIDTH);
69
    }
70
}
71
 
72
void Gate_NOT::simulateToOutput()
73
{
74
    // Save initial values to compare to later
75
    logicValue initValue = outputValue;
76
    logicValue initFaultyValue = outputFaultyValue;
77
 
78
    // Compute new output values
79
    if (inputValues[0] == logicValue_0) outputValue = logicValue_1;
80
    else if (inputValues[0] == logicValue_1) outputValue = logicValue_0;
81
 
82
    if (inputFaultyValues[0] == logicValue_0) outputFaultyValue = logicValue_1;
83
    else if (inputFaultyValues[0] == logicValue_1) outputFaultyValue = logicValue_0;
84
 
85
    // If outputs have changed, queue the connected gate for simulation
86
    if (outputValue != initValue || outputFaultyValue != initFaultyValue)
87
        emit enqueueSim(this);
88
 
89
    // Update connected wire values
90
    for (int i = 0; i < gateOutputWires.size(); i++) {
91
        gateOutputWires[i]->setValue(outputValue, outputFaultyValue, false);
92
    }
93
}