Skip to content

Commit

Permalink
dynamic link, realtime simulation, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
gmardon committed Mar 5, 2017
1 parent acb76fe commit 2957c56
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 57 deletions.
14 changes: 8 additions & 6 deletions include/Block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ namespace nts {

Block(QGraphicsItem *parent = 0);

Port *addPort(const QString &name, const Pin* pin, bool isOutput, int flags = 0, int ptr = 0);
Port *addPort(const QString &name, Pin* pin, bool isOutput, int flags = 0, int ptr = 0);

void addInputPort(const QString &name, const Pin *pin);
void addInputPort(const QString &name, Pin *pin);

void addOutputPort(const QString &name, const Pin *pin);
void addOutputPort(const QString &name, Pin *pin);

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

Port *getPortFromPin(const Pin *pin);

Port *getPortFromPinId(size_t index);

size_t getPinIdFromPin(Pin *pin);

Block *clone();

QVector<Port *> ports();

int type() const { return Type; }

const AComponent *getAComponent();
AComponent *getAComponent();

void setAComponent(const AComponent *component);
void setAComponent(AComponent *component);

protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
Expand All @@ -47,7 +49,7 @@ namespace nts {
int vertMargin;
int width;
int height;
const AComponent *aComponent;
AComponent *aComponent;
};
}
#endif // QNEBLOCK_H
6 changes: 6 additions & 0 deletions include/Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define QNECONNECTION_H

#include <QGraphicsPathItem>
#include <qpainter.h>


namespace nts {
Expand All @@ -25,6 +26,10 @@ namespace nts {

void setPort2(Port *p);

Port *getPort1();

Port *getPort2();

void updatePosFromPorts();

void updatePath();
Expand All @@ -35,6 +40,7 @@ namespace nts {

int type() const { return Type; }

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
::QPointF pos1;
::QPointF pos2;
Expand Down
5 changes: 4 additions & 1 deletion include/MainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QMainWindow>
#include <QtWidgets>
#include <qmenubar.h>
#include "AComponent.hpp"
#include "IComponent.hpp"
#include "Pin.hpp"
Expand Down Expand Up @@ -30,14 +31,16 @@ namespace nts {

void loadFile();

void simulate();

void addComponent(std::string name);

private:
Editor *nodesEditor;
QMenu *fileMenu;
EditorView *view;
QGraphicsScene *scene;
std::list<Block*> *blocks;
std::vector<Block*> *blocks;

std::vector<Pin> getPorts(AComponent *component);
};
Expand Down
8 changes: 4 additions & 4 deletions include/Port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ namespace nts {

int type() const { return Type; }

Block *block() const;
Block *block();

quint64 ptr();

void setPtr(quint64);

bool isConnected(Port *);

void setPin(const Pin* pin);
void setPin(Pin* pin);

const Pin *getPin();
Pin *getPin();
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);

Expand All @@ -67,7 +67,7 @@ namespace nts {
QVector<Connection *> m_connections;
int m_portFlags;
quint64 m_ptr;
const Pin *pin;
Pin *pin;
};
}
#endif // QNEPORT_H
41 changes: 22 additions & 19 deletions src/components/Pin.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
#include "Pin.hpp"

namespace nts
{
Pin::Pin(Mode _mode) : mode(_mode), component(0), state(UNDEFINED), target_pin(0)
{
namespace nts {
Pin::Pin(Mode _mode) : mode(_mode), component(0), state(UNDEFINED), target_pin(0) {
}

Pin::Pin(const Pin &other) : mode(other.getMode()), component(&other.getComponent()), state(other.getState()), target_pin(other.getTargetPin())
{
Pin::Pin(const Pin &other) : mode(other.getMode()), component(&other.getComponent()), state(other.getState()),
target_pin(other.getTargetPin()) {
}

Pin::~Pin()
{
Pin::~Pin() {
}

const Pin &Pin::operator=(const Pin &other)
{
const Pin &Pin::operator=(const Pin &other) {
this->component = &other.getComponent();
this->state = other.getState();
this->target_pin = other.getTargetPin();
return (*this);
}

void Pin::setComponent(IComponent &_component) {this->component = &_component;}
void Pin::setComponent(IComponent &_component) {
printf("setComponent %p\n", &_component);
this->component = &_component;
}

void Pin::setTarget(std::size_t _target_pin) {this->target_pin = _target_pin;}
void Pin::setTarget(std::size_t _target_pin) { this->target_pin = _target_pin; }

void Pin::setState(Tristate _state) {this->state = _state;}
void Pin::setState(Tristate _state) { this->state = _state; }

void Pin::setMode(Mode _mode) {this->mode = _mode;}
void Pin::setMode(Mode _mode) { this->mode = _mode; }

IComponent &Pin::getComponent() const {return (*this->component);}
IComponent &Pin::getComponent() const { return (*this->component); }

std::size_t Pin::getTargetPin() const {return (this->target_pin);}
std::size_t Pin::getTargetPin() const { return (this->target_pin); }

Tristate Pin::getState() const {return (this->state);}
Tristate Pin::getState() const { return (this->state); }

nts::Tristate Pin::compute() {return (this->component->Compute(this->target_pin));}
nts::Tristate Pin::compute() {
if (this->component == NULL)
return nts::Tristate::UNDEFINED;
return (this->component->Compute(this->target_pin));
}

Pin::Mode Pin::getMode() const {return (this->mode);}
Pin::Mode Pin::getMode() const { return (this->mode); }
}
11 changes: 11 additions & 0 deletions src/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ namespace nts {
!port1->isConnected(port2)) {
conn->setPos2(port2->scenePos());
conn->setPort2(port2);

if (conn->getPort1()->getPin()->getMode() == Pin::Mode::O && conn->getPort2()->getPin()->getMode() == Pin::Mode::I) {
conn->getPort2()->getPin()->setComponent(*conn->getPort1()->block()->getAComponent());
conn->getPort2()->getPin()->setTarget(conn->getPort1()->block()->getPinIdFromPin(conn->getPort1()->getPin()));
conn->getPort2()->getPin()->setState(conn->getPort2()->getPin()->compute());
} else if (conn->getPort1()->getPin()->getMode() == Pin::Mode::I && conn->getPort2()->getPin()->getMode() == Pin::Mode::O) {
conn->getPort1()->getPin()->setComponent(*conn->getPort2()->block()->getAComponent());
conn->getPort1()->getPin()->setTarget(conn->getPort2()->block()->getPinIdFromPin(conn->getPort2()->getPin()));
conn->getPort1()->getPin()->setState(conn->getPort1()->getPin()->compute());
}
// TODO need to support all
conn->updatePath();
conn = 0;
return true;
Expand Down
48 changes: 31 additions & 17 deletions src/editor/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ namespace nts {
saveAct->setStatusTip(tr("Save a file"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile()));

QAction *simulateAct = new QAction(tr("&Simulate"), this);
saveAct->setStatusTip(tr("Simulate"));
connect(simulateAct, SIGNAL(triggered()), this, SLOT(simulate()));

fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(loadAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(quitAct);

menuBar()->addAction(simulateAct);

setWindowTitle(tr("Nanotekspice"));

view = new EditorView();
Expand All @@ -61,7 +67,7 @@ namespace nts {
nodesEditor = new Editor(this);
nodesEditor->install(scene);

blocks = new std::list<Block *>();
blocks = new std::vector<Block *>();

this->setMinimumWidth(480);
this->setMinimumHeight(640);
Expand Down Expand Up @@ -108,38 +114,38 @@ namespace nts {

Block *b = new Block(0);
scene->addItem(b);
blocks->push_front(b);
blocks->push_back(b);
b->setAComponent(component);
b->addPort(QString::fromStdString(component->getName()), NULL, 0, Port::NamePort);

QPointF pos = view->mapToScene(mapFromGlobal(QCursor::pos()));

b->setPos(pos.x(), pos.y() - 10);

for (const auto &pin : component->getPins()) {
for (auto &pin : component->getPins()) {
switch (pin.getMode()) {
case Pin::Mode::U:
b->addInputPort("UNDEFINED", &pin);
b->addInputPort("UNDEFINED", const_cast<Pin*>(&pin));
break;

case Pin::Mode::I:
b->addInputPort("IN " + pin.getTargetPin(), &pin);
b->addInputPort("IN " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::O:
b->addOutputPort("OUT " + pin.getTargetPin(), &pin);
b->addOutputPort("OUT " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::IO:
b->addOutputPort("IN/OUT" + pin.getTargetPin(), &pin);
b->addOutputPort("IN/OUT" + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::VSS:
b->addOutputPort("VSS " + pin.getTargetPin(), &pin);
b->addOutputPort("VSS " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::VDD:
b->addOutputPort("VDD " + pin.getTargetPin(), &pin);
b->addOutputPort("VDD " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;
}
}
Expand All @@ -165,16 +171,24 @@ namespace nts {
QDataStream ds(&f);
}

void MainWindow::simulate() {
for (const auto &block : *blocks) {
if (const_cast<nts::AComponent *>(block->getAComponent())->getType() == nts::AComponent::Type::IC) {
const_cast<nts::AComponent *>(block->getAComponent())->Compute(1);
}
}
}

void MainWindow::setComponents(std::vector<AComponent *> components) {
blocks = new std::list<Block *>();
blocks = new std::vector<Block *>();

int index = 0;
for (const auto &component : components) {
index++;
Block *b = new Block(0);
b->setPos(120 * index, index / 3 * 120);
scene->addItem(b);
blocks->push_front(b);
blocks->push_back(b);
b->setAComponent(component);
b->addPort(QString::fromStdString(component->getName()), NULL, 0, Port::NamePort);

Expand All @@ -183,27 +197,27 @@ namespace nts {
for (const auto &pin : component->getPins()) {
switch (pin.getMode()) {
case Pin::Mode::U:
b->addInputPort("UNDEFINED", &pin);
b->addInputPort("UNDEFINED", const_cast<Pin*>(&pin));
break;

case Pin::Mode::I:
b->addInputPort("IN " + pin.getTargetPin(), &pin);
b->addInputPort("IN " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::O:
b->addOutputPort("OUT " + pin.getTargetPin(), &pin);
b->addOutputPort("OUT " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::IO:
b->addOutputPort("IN/OUT" + pin.getTargetPin(), &pin);
b->addOutputPort("IN/OUT" + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::VSS:
b->addOutputPort("VSS " + pin.getTargetPin(), &pin);
b->addOutputPort("VSS " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;

case Pin::Mode::VDD:
b->addOutputPort("VDD " + pin.getTargetPin(), &pin);
b->addOutputPort("VDD " + pin.getTargetPin(), const_cast<Pin*>(&pin));
break;
}
}
Expand Down
Loading

0 comments on commit 2957c56

Please sign in to comment.