Add all pipeline items to the PipelineWidget
MODIFY
src/graphics/pipelinewidget.cpp
src/graphics/pipelinewidget.cpp
@@ -19,8 +19,8 @@ PipelineWidget::PipelineWidget(QWidget* parent) : QGraphicsView(parent) {
// ------------ Create pipeline objects ---------------
// Registers memory
- Graphics::Shape* registers = new Graphics::Shape(Graphics::ShapeType::Block,
- Graphics::Stage::ID, 10, 15);
+ Graphics::Shape* registers = new Graphics::Shape(
+ Graphics::ShapeType::Block, Graphics::Stage::ID, 10, 15);
registers->addInput(QStringList() << "Read\nregister 1"
<< "Read\nregister 2"
<< "Write\nregister"
@@ -30,8 +30,8 @@ PipelineWidget::PipelineWidget(QWidget* parent) : QGraphicsView(parent) {
registers->setName("Registers");
// Data memory
- Graphics::Shape* data_mem = new Graphics::Shape(Graphics::ShapeType::Block,
- Graphics::Stage::MEM, 50, 5);
+ Graphics::Shape* data_mem = new Graphics::Shape(
+ Graphics::ShapeType::Block, Graphics::Stage::MEM, 50, 5);
data_mem->addInput(QStringList() << "Address"
<< "Write\ndata");
data_mem->addOutput(QStringList() << "Read\ndata");
@@ -93,17 +93,35 @@ PipelineWidget::PipelineWidget(QWidget* parent) : QGraphicsView(parent) {
Graphics::Shape* ifid = new Graphics::Shape(Graphics::ShapeType::Block,
Graphics::Stage::ID, 250, 10);
ifid->setName("IF/ID");
+ for (int i = 0; i < 2; i++) {
ifid->addInput("");
ifid->addOutput("");
+ }
Graphics::Shape* idex = new Graphics::Shape(Graphics::ShapeType::Block,
Graphics::Stage::EX, 250, 10);
idex->setName("ID/EX");
- for (int i = 0; i < 6; i++) {
+ for (int i = 0; i < 4; i++) {
idex->addInput("");
idex->addOutput("");
}
+ Graphics::Shape* exmem = new Graphics::Shape(Graphics::ShapeType::Block,
+ Graphics::Stage::EX, 250, 10);
+ exmem->setName("EX/MEM");
+ for (int i = 0; i < 4; i++) {
+ exmem->addInput("");
+ exmem->addOutput("");
+ }
+
+ Graphics::Shape* memwb = new Graphics::Shape(Graphics::ShapeType::Block,
+ Graphics::Stage::EX, 250, 10);
+ memwb->setName("MEM/WB");
+ for (int i = 0; i < 2; i++) {
+ memwb->addInput("");
+ memwb->addOutput("");
+ }
+
// add a point at 0,0 for testing purposes
scene->addEllipse(0, 0, 5, 5, QPen(Qt::red));
@@ -120,20 +138,43 @@ PipelineWidget::PipelineWidget(QWidget* parent) : QGraphicsView(parent) {
scene->addItem(alu2);
scene->addItem(alu3);
scene->addItem(idex);
+ scene->addItem(exmem);
+ scene->addItem(memwb);
- // Create connections
- // Connections should be added in a left to right ordering!
+ // ----------- Create connections ----------------------
+ // IF
createConnection(mux1, 0, pc, 0);
createConnection(pc, 0, instr_mem, 0);
createConnection(pc, 0, alu1, 0);
- createConnection(instr_mem, 0, ifid, 0);
+ createConnection(alu1, 0, mux1, 0);
+ createConnection(instr_mem, 0, ifid, 1);
+ createConnection(pc, 0, ifid, 0);
+ // ID
createConnection(ifid, 0, registers, 0);
createConnection(ifid, 0, registers, 1);
+ createConnection(ifid, 0, registers, 2);
createConnection(registers, 0, idex, 1);
createConnection(registers, 1, idex, 2);
+ createConnection(ifid, 0, idex, 0);
+ // EX
createConnection(idex, 0, alu2, 0);
createConnection(idex, 1, alu3, 0);
createConnection(idex, 2, mux2, 0);
+ createConnection(mux2, 0, alu3, 1);
+ createConnection(alu2, 0, exmem, 0);
+ createConnection(alu3, 0, exmem, 1);
+ createConnection(idex, 2, exmem, 3);
+ createConnection(idex, 3, alu2, 1);
+ // MEM
+ createConnection(exmem, 0, mux1, 1);
+ createConnection(exmem, 1, data_mem, 0);
+ createConnection(exmem, 2, data_mem, 1);
+ createConnection(data_mem, 0, memwb, 0);
+ createConnection(exmem, 1, memwb, 1);
+ // WB
+ createConnection(memwb, 0, mux3, 0);
+ createConnection(memwb, 1, mux3, 0);
+ createConnection(mux3, 0, registers, 3);
// Adjust positioning of items in scene
adjustPositioning();
@@ -162,8 +203,10 @@ QList<QGraphicsItem*> PipelineWidget::filterAllowedItems(
}
} else if (item->type() == Graphics::Shape::connectionType()) {
if (shape == item) {
- // Remove item if it intersects with itself (this happens when we just
- // check what is intersecting with a rectangle in the scene -ofcourse
+ // Remove item if it intersects with itself (this happens when
+ // we just
+ // check what is intersecting with a rectangle in the scene
+ // -ofcourse
// the item itself will intersect that rectangle)
returnList.removeAll(item);
}
@@ -174,7 +217,6 @@ QList<QGraphicsItem*> PipelineWidget::filterAllowedItems(
void PipelineWidget::adjustPositioning() {
QList<Graphics::Shape*> movedShapes;
- const static int minimumLength = 40; // Minimum connection length
for (const auto& connection : m_connections) {
auto shapes = connection->getShapes();
if (!movedShapes.contains(shapes.first) &&
@@ -186,13 +228,13 @@ void PipelineWidget::adjustPositioning() {
int newY = (pointPair.first.y() - pointPair.second.y()) / 2;
// Step 2: set source and destination points to a sensible
- // x position. Middle point +- minimumLength/2
+ // x position. Middle point +- minConnectionLen/2
// This also enforces that a source shape is positioned to the left
// of a destination shape
int dx = 0;
int diff = pointPair.second.x() - pointPair.first.x();
- if (diff < minimumLength) {
- dx = (minimumLength - diff) / 2;
+ if (diff < minConnectionLen) {
+ dx = (minConnectionLen - diff) / 2;
}
// Move shapes
@@ -212,12 +254,15 @@ void PipelineWidget::adjustPositioning() {
int dx = 0;
int diff =
pointPair.second.x() - shapes.first->sceneBoundingRect().right();
- if (diff < minimumLength) {
- dx = minimumLength - diff;
+ if (diff < minConnectionLen) {
+ dx = minConnectionLen - diff;
}
- // Move shape to the new position. Afterwards, process all pending events
- // in the event loop - this will indirectly update the scene. We need to
- // verify that the scene has been updated before checking intersecting
+ // Move shape to the new position. Afterwards, process all pending
+ // events
+ // in the event loop - this will indirectly update the scene. We
+ // need to
+ // verify that the scene has been updated before checking
+ // intersecting
// items
shapes.second->moveBy(dx, newY);
@@ -226,33 +271,40 @@ void PipelineWidget::adjustPositioning() {
// Check for intersections with other shapes
auto intersectingItems =
items(newRect.toRect(), Qt::IntersectsItemBoundingRect);
- intersectingItems = filterAllowedItems(shapes.second, intersectingItems);
+ intersectingItems =
+ filterAllowedItems(shapes.second, intersectingItems);
- if (intersectingItems.size() == 0) {
+ if (/*intersectingItems.size() == 0*/ true) {
// Accept new position
movedShapes.append(shapes.second);
} else {
- // Intersecting items detected. Move second shape in pair up or down,
+ // Intersecting items detected. Move second shape in pair up or
+ // down,
// until the shapeMargin requirement is met
auto rectBelow = shapes.second->boundingRect().toRect();
auto rectAbove = shapes.second->boundingRect().toRect();
while (intersectingItems.size() != 0) {
- auto intersectingRect = intersectingItems.first()->boundingRect();
- // Translate the shapes rect to be below and above the intersecting
+ auto intersectingRect =
+ intersectingItems.first()->boundingRect();
+ // Translate the shapes rect to be below and above the
+ // intersecting
// item
- rectBelow.translate(0, intersectingRect.bottom() + shapeMargin);
- rectAbove.translate(0, intersectingRect.top() - shapeMargin);
+ rectBelow.translate(
+ 0, intersectingRect.bottom() + shapeMargin);
+ rectAbove.translate(0,
+ intersectingRect.top() - shapeMargin);
// Initially, test with the rect that is closest to y=0
QPair<QRect, QRect> rectOrdered =
abs(rectBelow.y()) < abs(rectAbove.y())
? QPair<QRect, QRect>(rectBelow, rectAbove)
: QPair<QRect, QRect>(rectAbove, rectBelow);
- if (items(rectOrdered.first, Qt::IntersectsItemBoundingRect).size() ==
- 0) {
+ if (items(rectOrdered.first, Qt::IntersectsItemBoundingRect)
+ .size() == 0) {
shapes.second->setPos(rectOrdered.first.topLeft());
break;
- } else if (items(rectOrdered.second, Qt::IntersectsItemBoundingRect)
+ } else if (items(rectOrdered.second,
+ Qt::IntersectsItemBoundingRect)
.size() == 0) {
shapes.second->setPos(rectOrdered.second.topLeft());
break;

MODIFY
src/graphics/pipelinewidget.h
src/graphics/pipelinewidget.h
@@ -27,6 +27,7 @@ class PipelineWidget : public QGraphicsView {
QList<QGraphicsItem*> items);
qreal shapeMargin = 10; // Minimum distance between two shapes
+ qreal minConnectionLen = 50;
QList<Graphics::Connection*> m_connections;
};

Add all pipeline items to the PipelineWidget