From 16b1544fff7e9ca11deb3ae1b891c79eac7ec50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 21 Jan 2018 11:36:34 +0100 Subject: Implement little bit more of scheme and fix connection angle This commit adds few more bits to scheme but mainly it chnages how connectors specify angles. Originally it was in radians but we was mapping that trough mathematical operations directly to sizes. But that was problematic because of floating point inacuracy and we sometimes founded intersection where there should be one. So this commit gets rid of this at all and instead allows just some fixes axes to be used instead of arbitrary angles. --- qtmips_gui/coreview/adder.cpp | 6 +-- qtmips_gui/coreview/alu.cpp | 8 +-- qtmips_gui/coreview/alu.h | 1 - qtmips_gui/coreview/and.cpp | 53 +++++++++++++++++++ qtmips_gui/coreview/and.h | 29 +++++++++++ qtmips_gui/coreview/connection.cpp | 95 +++++++++++++++++++++++----------- qtmips_gui/coreview/connection.h | 54 ++++++++++++++----- qtmips_gui/coreview/constant.cpp | 4 +- qtmips_gui/coreview/junction.cpp | 16 ++++-- qtmips_gui/coreview/junction.h | 5 +- qtmips_gui/coreview/latch.cpp | 4 +- qtmips_gui/coreview/logicblock.cpp | 2 +- qtmips_gui/coreview/memory.cpp | 14 ++--- qtmips_gui/coreview/multiplexer.cpp | 14 ++--- qtmips_gui/coreview/multiplexer.h | 3 +- qtmips_gui/coreview/programcounter.cpp | 4 +- qtmips_gui/coreview/registers.cpp | 14 ++--- 17 files changed, 239 insertions(+), 87 deletions(-) create mode 100644 qtmips_gui/coreview/and.cpp create mode 100644 qtmips_gui/coreview/and.h (limited to 'qtmips_gui/coreview') diff --git a/qtmips_gui/coreview/adder.cpp b/qtmips_gui/coreview/adder.cpp index cd227ab..4363f97 100644 --- a/qtmips_gui/coreview/adder.cpp +++ b/qtmips_gui/coreview/adder.cpp @@ -17,9 +17,9 @@ Adder::Adder() : QGraphicsItem(nullptr), plus("+", this) { QRectF plus_box = plus.boundingRect(); plus.setPos(DENT + (WIDTH-DENT)/2 - plus_box.width()/2, HEIGHT/2 - plus_box.height()/2); - con_in_a = new Connector(0); - con_in_b = new Connector(0); - con_out = new Connector(M_PI); + con_in_a = new Connector(Connector::AX_X); + con_in_b = new Connector(Connector::AX_X); + con_out = new Connector(Connector::AX_X); setPos(x(), y()); // set connector's position } diff --git a/qtmips_gui/coreview/alu.cpp b/qtmips_gui/coreview/alu.cpp index d49cab2..2f24210 100644 --- a/qtmips_gui/coreview/alu.cpp +++ b/qtmips_gui/coreview/alu.cpp @@ -14,10 +14,10 @@ coreview::Alu::Alu() : QGraphicsItem(nullptr), name("ALU", this) { font.setPointSize(7); name.setFont(font); - con_in_a = new Connector(0); - con_in_b = new Connector(0); - con_out = new Connector(M_PI); - con_ctl = new Connector(-M_PI_2); + con_in_a = new Connector(Connector::AX_X); + con_in_b = new Connector(Connector::AX_X); + con_out = new Connector(Connector::AX_X); + con_ctl = new Connector(Connector::AX_Y); setPos(x(), y()); // set connector's position } diff --git a/qtmips_gui/coreview/alu.h b/qtmips_gui/coreview/alu.h index 25d7351..288de87 100644 --- a/qtmips_gui/coreview/alu.h +++ b/qtmips_gui/coreview/alu.h @@ -2,7 +2,6 @@ #define COREVIEW_ALU_H #include -#include #include "connection.h" namespace coreview { diff --git a/qtmips_gui/coreview/and.cpp b/qtmips_gui/coreview/and.cpp new file mode 100644 index 0000000..f60dfcd --- /dev/null +++ b/qtmips_gui/coreview/and.cpp @@ -0,0 +1,53 @@ +#include "and.h" +#include + +using namespace coreview; + +////////////////////// +#define GAP 5 +#define PENW 1 +////////////////////// + +And::And(unsigned size) : QGraphicsItem(nullptr) { + for (unsigned i = 0; i < size; i++) + connectors.append(new Connector(Connector::AX_X)); + con_out = new Connector(Connector::AX_X); + + setPos(x(), y()); // update connectors positions +} + +And::~And() { + delete con_out; + for (int i = 0; i < connectors.size(); i++) + delete connectors[i]; +} + +QRectF And::boundingRect() const { + qreal side = GAP * connectors.size(); + return QRectF(-PENW, -PENW, side + PENW, side + PENW); +} + +void And::paint(QPainter *painter, const QStyleOptionGraphicsItem *option __attribute__((unused)), QWidget *widget __attribute__((unused))) { + qreal size = GAP * connectors.size(); + painter->drawLine(0, 0, 0, size); + painter->drawLine(0, 0, size/2, 0); + painter->drawLine(0, size, size/2, size); + painter->drawArc(0, 0, size, size, 270 * 16, 180 * 16); +} + +void And::setPos(qreal x, qreal y) { + QGraphicsItem::setPos(x, y); + + qreal size = GAP * connectors.size(); + con_out->setPos(x + size, y + size/2); + for (int i = 0; i < connectors.size(); i++) + connectors[i]->setPos(x, y + GAP/2 + GAP*i); +} + +const Connector *And::connector_in(unsigned i) const { + return connectors[i]; +} + +const Connector *And::connector_out() const { + return con_out; +} diff --git a/qtmips_gui/coreview/and.h b/qtmips_gui/coreview/and.h new file mode 100644 index 0000000..ca0af30 --- /dev/null +++ b/qtmips_gui/coreview/and.h @@ -0,0 +1,29 @@ +#ifndef COREVIEW_AND_H +#define COREVIEW_AND_H + +#include +#include +#include "connection.h" + +namespace coreview { + +class And : public QGraphicsItem { +public: + And(unsigned size = 2); + ~And(); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + void setPos(qreal x, qreal y); + const Connector *connector_in(unsigned) const; + const Connector *connector_out() const; + +private: + QVector connectors; + Connector* con_out; +}; + +} + +#endif // COREVIEW_AND_H diff --git a/qtmips_gui/coreview/connection.cpp b/qtmips_gui/coreview/connection.cpp index 71647e2..3355eed 100644 --- a/qtmips_gui/coreview/connection.cpp +++ b/qtmips_gui/coreview/connection.cpp @@ -3,14 +3,19 @@ using namespace coreview; -Connector::Connector(qreal angle) { - ang = angle; +Connector::Connector(enum Axis ax) { + this->ax = ax; } void Connector::setPos(qreal x, qreal y) { qx = x; qy = y; - emit updated(QPointF(qx, qy)); + emit updated(point()); + emit updated(vector()); +} + +enum Connector::Axis Connector::axis() const { + return ax; } qreal Connector::x() const { @@ -21,28 +26,34 @@ qreal Connector::y() const { return qy; } -QLineF Connector::vector() const { - return QLineF(point(), QPointF(x() + cos(ang), y() + sin(ang))); -} - QPointF Connector::point() const { return QPointF(qx, qy); } -qreal Connector::angle() const { - return ang; +QLineF Connector::vector() const { + QPointF p = point(); + switch (ax) { + case AX_X: + return QLineF(p, p + QPointF(1, 0)); + case AX_Y: + return QLineF(p, p + QPointF(0, 1)); + case AX_XY: + return QLineF(p, p + QPointF(1, 1)); + case AX_MXY: + return QLineF(p, p + QPoint(1, -1)); + } } Connection::Connection(const Connector *a, const Connector *b) : QGraphicsObject(nullptr) { pen_width = 1; - ang_start = a->angle(); - ang_end = b->angle(); + ax_start = a->vector(); + ax_end = a->vector(); - connect(a, SIGNAL(updated(QPointF)), this, SLOT(moved_start(QPointF))); - connect(b, SIGNAL(updated(QPointF)), this, SLOT(moved_end(QPointF))); - moved_start(a->point()); - moved_end(b->point()); + connect(a, SIGNAL(updated(QLineF)), this, SLOT(moved_start(QLineF))); + connect(b, SIGNAL(updated(QLineF)), this, SLOT(moved_end(QLineF))); + moved_start(a->vector()); + moved_end(b->vector()); } void Connection::setHasText(bool has) { @@ -65,13 +76,13 @@ void Connection::setAxes(QVector axes) { recalc_line(); } -void Connection::moved_start(QPointF p) { - p_start = p; +void Connection::moved_start(QLineF p) { + ax_start = p; recalc_line(); } -void Connection::moved_end(QPointF p) { - p_end = p; +void Connection::moved_end(QLineF p) { + ax_end = p; recalc_line(); } @@ -88,7 +99,9 @@ QRectF Connection::boundingRect() const { void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option __attribute__((unused)), QWidget *widget __attribute__((unused))) { QPen pen; pen.setWidth(pen_width); - // TODO color? + pen.setColor(color); + pen.setCapStyle(Qt::FlatCap); + pen.setJoinStyle(Qt::BevelJoin); painter->setPen(pen); painter->drawPolyline(QPolygonF(points)); @@ -97,20 +110,44 @@ void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option void Connection::recalc_line() { points.clear(); - points.append(p_start); + points.append(ax_start.p1()); - QLineF cur_l(p_start, QPointF(p_start.x() + cos(ang_start), p_start.y() + sin(ang_start))); + QLineF cur_l = ax_start; for (int i = 0; i < break_axes.size(); i++) { - recalc_line_add_point(cur_l, break_axes[i]); - cur_l = break_axes[i]; + if (recalc_line_add_point(cur_l, break_axes[i])) + cur_l = break_axes[i]; } - recalc_line_add_point(cur_l, QLineF(QPoint(p_end.x() + cos(ang_end), p_end.y() + sin(ang_end)), p_end)); + recalc_line_add_point(cur_l, ax_end); - points.append(p_end); + points.append(ax_end.p1()); } -void Connection::recalc_line_add_point(const QLineF &l1, const QLineF &l2) { +bool Connection::recalc_line_add_point(const QLineF &l1, const QLineF &l2) { QPointF intersec; - if (l1.intersect(l2, &intersec) != QLineF::NoIntersection) - points.append(intersec); + if (l1.intersect(l2, &intersec) == QLineF::NoIntersection) + return false; + points.append(intersec); + return true; +} + +Bus::Bus(const Connector *start, const Connector *end, unsigned width) : Connection(start, end) { + pen_width = width; +} + +const Connector *Bus::new_connector(qreal x, qreal y, enum Connector::Axis axis) { + Connector *c = new Connector(axis); + conns.append({ + .c = c, + .p = QPoint(x, y) + }); + // TODO update positions + return c; +} + +const Connector *Bus::new_connector(const QPointF &p, enum Connector::Axis axis) { + return new_connector(p.x(), p.y(), axis); +} + +Signal::Signal(const Connector *start, const Connector *end) : Connection(start, end) { + color = QColor(0, 0, 255); } diff --git a/qtmips_gui/coreview/connection.h b/qtmips_gui/coreview/connection.h index 0ee981e..88ad1da 100644 --- a/qtmips_gui/coreview/connection.h +++ b/qtmips_gui/coreview/connection.h @@ -10,21 +10,30 @@ namespace coreview { class Connector : public QObject { Q_OBJECT public: - Connector(qreal angle); + enum Axis { + AX_X, // X axis + AX_Y, // Y axis + AX_XY, // X=Y axis (45°) + AX_MXY // X=-Y axis (-45°) + }; + + Connector(enum Axis axis = AX_X); void setPos(qreal x, qreal y); + + enum Axis axis() const; qreal x() const; qreal y() const; + QPointF point() const; QLineF vector() const; - qreal angle() const; - signals: - void updated(QPointF); + void updated(QPointF point); + void updated(QLineF vector); private: - qreal ang; + enum Axis ax; qreal qx, qy; }; @@ -42,22 +51,43 @@ public: void setAxes(QVector); private slots: - void moved_start(QPointF); - void moved_end(QPointF); + void moved_start(QLineF); + void moved_end(QLineF); -private: +protected: QGraphicsSimpleTextItem *value; QVector points; - QPointF p_start, p_end; - qreal ang_start, ang_end; + QLineF ax_start, ax_end; QVector break_axes; QString text; int pen_width; + QColor color; - // TODO line width and possibly bus width void recalc_line(); - void recalc_line_add_point(const QLineF &l1, const QLineF &l2); + bool recalc_line_add_point(const QLineF &l1, const QLineF &l2); +}; + +class Bus : public Connection { +public: + Bus(const Connector *start, const Connector *end, unsigned width = 4); + + // This creates connector snapped to closes point to x,y that is on bus + const Connector *new_connector(qreal x, qreal y, enum Connector::Axis = Connector::AX_X); + const Connector *new_connector(const QPointF&, enum Connector::Axis = Connector::AX_X); + +protected: + struct con_pos { + Connector *c; + QPointF p; + }; + QVector conns; + // TODO because of this we have to overload setAxis function and update that in there +}; + +class Signal : public Connection { +public: + Signal(const Connector *start, const Connector *end); }; #define CON_AXIS_X(Y) QLineF(QPointF(0, Y), QPointF(1, Y)) diff --git a/qtmips_gui/coreview/constant.cpp b/qtmips_gui/coreview/constant.cpp index 2f39fce..433d842 100644 --- a/qtmips_gui/coreview/constant.cpp +++ b/qtmips_gui/coreview/constant.cpp @@ -12,8 +12,8 @@ Constant::Constant(const Connector *con, const QString &text) : QGraphicsObject( font.setPointSize(7); this->text.setFont(font); - con_our = new Connector(M_PI); - conn = new Connection(con_our, con); + con_our = new Connector(Connector::AX_X); + conn = new Bus(con_our, con, 2); connect(con, SIGNAL(updated(QPointF)), this, SLOT(ref_con_updated(QPointF))); ref_con_updated(con->point()); // update initial connector position } diff --git a/qtmips_gui/coreview/junction.cpp b/qtmips_gui/coreview/junction.cpp index 271284c..494aca6 100644 --- a/qtmips_gui/coreview/junction.cpp +++ b/qtmips_gui/coreview/junction.cpp @@ -6,7 +6,9 @@ using namespace coreview; #define DOT_SIZE 4 ////////////////////// -Junction::Junction() : QGraphicsItem(nullptr) { } +Junction::Junction(bool point) : QGraphicsItem(nullptr) { + this->point = point; +} Junction::~Junction() { for (int i = 0; i < cons.size(); i++) @@ -14,11 +16,17 @@ Junction::~Junction() { } QRectF Junction::boundingRect() const { - return QRectF(-DOT_SIZE/2, -DOT_SIZE/2, DOT_SIZE, DOT_SIZE); + if (point) + return QRectF(-DOT_SIZE/2, -DOT_SIZE/2, DOT_SIZE, DOT_SIZE); + else + return QRectF(); } void Junction::paint(QPainter *painter, const QStyleOptionGraphicsItem *option __attribute__((unused)), QWidget *widget __attribute__((unused))) { + if (!point) + return; painter->setBrush(QBrush(QColor(0, 0, 0))); + painter->setPen(QPen(Qt::NoPen)); // Disable pen (render only brush) painter->drawEllipse(-DOT_SIZE/2, -DOT_SIZE/2, DOT_SIZE, DOT_SIZE); } @@ -28,8 +36,8 @@ void Junction::setPos(qreal x, qreal y) { con->setPos(x, y); } -Connector *Junction::new_connector(qreal angle) { - Connector*n = new Connector(angle); +Connector *Junction::new_connector(enum Connector::Axis axis) { + Connector*n = new Connector(axis); cons.append(n); setPos(x(), y()); // set connector's position return n; diff --git a/qtmips_gui/coreview/junction.h b/qtmips_gui/coreview/junction.h index 1a8f5d7..2eaa732 100644 --- a/qtmips_gui/coreview/junction.h +++ b/qtmips_gui/coreview/junction.h @@ -10,17 +10,18 @@ namespace coreview { class Junction : public QGraphicsItem { public: - Junction(); + Junction(bool point = true); ~Junction(); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void setPos(qreal x, qreal y); - Connector *new_connector(qreal angle); + Connector *new_connector(enum Connector::Axis axis = Connector::AX_X); private: QList cons; + bool point; }; } diff --git a/qtmips_gui/coreview/latch.cpp b/qtmips_gui/coreview/latch.cpp index c3bab4f..e3d8ef5 100644 --- a/qtmips_gui/coreview/latch.cpp +++ b/qtmips_gui/coreview/latch.cpp @@ -69,8 +69,8 @@ void Latch::setPos(qreal x, qreal y) { struct Latch::ConnectorPair Latch::new_connector(qreal cy) { SANITY_ASSERT(cy < height, "Latch: Trying to create connector outside of latch height"); ConnectorPair cp; - cp.in = new Connector(0); - cp.out = new Connector(M_PI); + cp.in = new Connector(Connector::AX_X); + cp.out = new Connector(Connector::AX_X); connectors.append(cp); connectors_off.append(cy); setPos(x(), y()); // Update connectors position diff --git a/qtmips_gui/coreview/logicblock.cpp b/qtmips_gui/coreview/logicblock.cpp index 1ee9a18..2e74a77 100644 --- a/qtmips_gui/coreview/logicblock.cpp +++ b/qtmips_gui/coreview/logicblock.cpp @@ -78,7 +78,7 @@ const Connector *LogicBlock::new_connector(qreal x, qreal y) { y = sign(y); // Note: we are using here that 0 and M_PI is same angle but different orientation (but we ignore orientation for now) - Connector *c = new Connector(fabs(x) > fabs(y) ? 0 : M_PI_2); + Connector *c = new Connector(fabs(x) > fabs(y) ? Connector::AX_X : Connector::AX_Y); connectors.append({ .con = c, .x = x, diff --git a/qtmips_gui/coreview/memory.cpp b/qtmips_gui/coreview/memory.cpp index 8f1669d..a2e5899 100644 --- a/qtmips_gui/coreview/memory.cpp +++ b/qtmips_gui/coreview/memory.cpp @@ -53,8 +53,8 @@ ProgramMemory::ProgramMemory(machine::QtMipsMachine *machine) : Memory(machine) cache = machine->config().cache_program().enabled(); set_type("Program"); - con_address = new Connector(0); - con_inst = new Connector(M_PI); + con_address = new Connector(Connector::AX_X); + con_inst = new Connector(Connector::AX_X); } ProgramMemory::~ProgramMemory() { @@ -81,11 +81,11 @@ DataMemory::DataMemory(machine::QtMipsMachine *machine) : Memory(machine) { cache = machine->config().cache_data().enabled(); set_type("Data"); - con_address = new Connector(0); - con_data_out = new Connector(M_PI); - con_data_in = new Connector(0); - con_req_write = new Connector(M_PI_2); - con_req_read = new Connector(M_PI_2); + con_address = new Connector(Connector::AX_X); + con_data_out = new Connector(Connector::AX_X); + con_data_in = new Connector(Connector::AX_X); + con_req_write = new Connector(Connector::AX_X); + con_req_read = new Connector(Connector::AX_X); } DataMemory::~DataMemory() { diff --git a/qtmips_gui/coreview/multiplexer.cpp b/qtmips_gui/coreview/multiplexer.cpp index 7429fda..b25e6d1 100644 --- a/qtmips_gui/coreview/multiplexer.cpp +++ b/qtmips_gui/coreview/multiplexer.cpp @@ -10,15 +10,15 @@ using namespace coreview; #define PENW 1 ////////////////////// -Multiplexer::Multiplexer(unsigned size) { +Multiplexer::Multiplexer(unsigned size, bool ctl_up) { this->size = size; seton = 0; - ctlfrom = false; - con_ctl = new Connector(M_PI_2); - con_out = new Connector(M_PI); + ctlfrom = ctl_up; + con_ctl = new Connector(Connector::AX_Y); + con_out = new Connector(Connector::AX_X); con_in = new Connector*[size]; for (unsigned i = 0; i < size; i++) - con_in[i] = new Connector(0); + con_in[i] = new Connector(Connector::AX_X); setPos(x(), y()); // Set connectors possitions } @@ -80,7 +80,3 @@ void Multiplexer::set(unsigned i) { update(); } -void Multiplexer::setCtl(bool up) { - ctlfrom = up; - setPos(x(), y()); // Update connectors -} diff --git a/qtmips_gui/coreview/multiplexer.h b/qtmips_gui/coreview/multiplexer.h index 2cfc90d..ad4fd0b 100644 --- a/qtmips_gui/coreview/multiplexer.h +++ b/qtmips_gui/coreview/multiplexer.h @@ -9,7 +9,7 @@ namespace coreview { class Multiplexer : public QGraphicsItem { public: - Multiplexer(unsigned size); + Multiplexer(unsigned size, bool ctl_up = false); ~Multiplexer(); QRectF boundingRect() const; @@ -21,7 +21,6 @@ public: const Connector *connector_in(unsigned i) const; // Inputs void set(unsigned i); // Set what value should be set as connected (indexing from 1 where 0 is no line) - void setCtl(bool up); // Set if control signal is from up or down (in default down) private: bool ctlfrom; diff --git a/qtmips_gui/coreview/programcounter.cpp b/qtmips_gui/coreview/programcounter.cpp index c43ff6d..ab7d49b 100644 --- a/qtmips_gui/coreview/programcounter.cpp +++ b/qtmips_gui/coreview/programcounter.cpp @@ -22,8 +22,8 @@ ProgramCounter::ProgramCounter(machine::QtMipsMachine *machine) : QGraphicsObjec connect(machine->registers(), SIGNAL(pc_update(std::uint32_t)), this, SLOT(pc_update(std::uint32_t))); - con_in = new Connector(-M_PI_2); - con_out = new Connector(M_PI_2); + con_in = new Connector(Connector::AX_Y); + con_out = new Connector(Connector::AX_Y); setPos(x(), y()); // To set initial connectors positions } diff --git a/qtmips_gui/coreview/registers.cpp b/qtmips_gui/coreview/registers.cpp index 87db32e..9a4fcff 100644 --- a/qtmips_gui/coreview/registers.cpp +++ b/qtmips_gui/coreview/registers.cpp @@ -10,13 +10,13 @@ using namespace coreview; ////////////////////// Registers::Registers() : QGraphicsObject(nullptr), name("Registers", this) { - con_read1 = new Connector(-M_PI_2); - con_read1_reg = new Connector(-M_PI_2); - con_read2 = new Connector(-M_PI_2); - con_read2_reg = new Connector(-M_PI_2); - con_write = new Connector(-M_PI_2); - con_write_reg = new Connector(-M_PI_2); - con_ctl_write = new Connector(-M_PI_2); + con_read1 = new Connector(Connector::AX_Y); + con_read1_reg = new Connector(Connector::AX_Y); + con_read2 = new Connector(Connector::AX_Y); + con_read2_reg = new Connector(Connector::AX_Y); + con_write = new Connector(Connector::AX_Y); + con_write_reg = new Connector(Connector::AX_Y); + con_ctl_write = new Connector(Connector::AX_Y); // TODO do we want to have any hooks on real registers? -- cgit v1.2.3