Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 334 → Rev 335

/Classwork/ECE5505 - Test and Verification/Final Project/Gate_NOR.cpp
0,0 → 1,109
#include "Gate_NOR.h"
 
Gate_NOR::Gate_NOR(int gateID, gType type, int numInputs, int gateLevel)
: Gate_BASE(gateID, type, numInputs, gateLevel)
{
textRect = QRectF(BORDER_OFFSET * 1.2, BORDER_OFFSET, xSize - BORDER_OFFSET * 3, ySize - BORDER_OFFSET * 2);
}
 
void Gate_NOR::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 rightArcBox = QRectF(0, BORDER_OFFSET, 2 * (xSize - (BORDER_OFFSET * 2)) - BORDER_OFFSET * 2, ySize - (BORDER_OFFSET * 2));
rightArcBox.translate(-xSize + BORDER_OFFSET * 4, 0);
painter->drawArc(rightArcBox, 0, 90 * 16);
painter->drawArc(rightArcBox, -90 * 16, 90 * 16);
 
painter->drawEllipse(QPoint(xSize - BORDER_OFFSET - BORDER_OFFSET / 2, ySize / 2), BORDER_OFFSET / 2, BORDER_OFFSET / 2);
 
// 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_NOR::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 (!allZeros) outputValue = logicValue_0;
else if (allZeros && undefined) outputValue = logicValue_X;
else if (allZeros) outputValue = logicValue_1;
else outputValue = logicValue_X;
 
if (!allZerosFaulty) outputFaultyValue = logicValue_0;
else if (allZerosFaulty && undefinedFaulty) outputFaultyValue = logicValue_X;
else if (allZerosFaulty) outputFaultyValue = logicValue_1;
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);
}
}