From 2b941bc1b28e0a60da50baa0875d7cb05c0632c1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 12 Jun 2024 12:14:51 +0200 Subject: WIP connect nets --- Circuit.cpp | 27 ++++++++++++++++++--------- Circuit.h | 4 ++-- GateAnd.cpp | 43 ++++++++++++++++++++----------------------- Net.cpp | 4 ---- Net.h | 12 ++++++++---- Node.cpp | 9 +++++---- docs/class-diag.puml | 2 +- 7 files changed, 54 insertions(+), 47 deletions(-) diff --git a/Circuit.cpp b/Circuit.cpp index cf6312e..393060f 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -5,8 +5,7 @@ void Circuit::create(string label, vector nodes) { if (nodes.size() == 1 && NodeFactory::has_type(nodes[0])) return new_node(label, nodes[0]); - for (string node : nodes) - new_net(label, node); + new_net(label, nodes); } void Circuit::new_node(string label, string type) { @@ -20,16 +19,26 @@ void Circuit::new_node(string label, string type) { printf("[%s] (%s)\n", label.c_str(), type.c_str()); } -void Circuit::new_net(string label, string connection) { - // TODO: instance Net - // TODO: connect Net to connection - printf("[%s] -> %s\n", label.c_str(), connection.c_str()); +void Circuit::new_net(string src, vector dests) { + printf("%s\n", src.c_str()); + Net * net = new Net(); + nets.push_back(net); + + for (auto dest : dests) { + Node * node = nodes.find(dest)->second; + if (node == nullptr) continue; // TODO: exception! + node->addInput(net); + } + + Node * node = nodes.find(src)->second; + if (node == nullptr) return; // TODO: exception! + node->setOutput(net); } Circuit::~Circuit() { - for (auto const & n : nodes) - delete n.second; - for (auto const & n : nets) + for (auto & n : nodes) delete n.second; + for (auto & n : nets) + delete n; } diff --git a/Circuit.h b/Circuit.h index 5fd8d23..e681742 100644 --- a/Circuit.h +++ b/Circuit.h @@ -18,10 +18,10 @@ public: public: void create(string label, vector nodes); void new_node(string label, string type); - void new_net(string label, string node); + void new_net(string src, vector dests); private: std::map nodes = {}; - std::map nets = {}; + vector nets = {}; }; diff --git a/GateAnd.cpp b/GateAnd.cpp index dc65353..6f19d61 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -4,31 +4,28 @@ GateAnd GateAnd::instance(GateAnd::type); GateAnd::GateAnd(const char * type) : Node(type) { } -// Concrete Nodes: void GateAnd::compare() { SignalLevel new_out = HIGH; -// TODO fix segfault somewhere below -// for (int i = 0; i < this->inputs.size(); i++){ -// switch (this->inputs[i]->getLevel()){ -// case LOW: -// new_out = LOW; -// break; -// case HIGH: -// continue; -// break; -// case UNDEFINED: -// default: -// new_out = UNDEFINED; -// exit; -// break; -// } -// } - -// if (this->output->getLevel() == new_out){ -// /* do nothing */ -// } else { -// this->output->setLevel(new_out); -// } + // TODO: fix segfault somewhere below + for (int i = 0; i < this->inputs.size(); i++){ + switch (this->inputs[i]->getLevel()){ + case LOW: + new_out = LOW; + break; + case HIGH: + continue; + break; + case UNDEFINED: + default: + new_out = UNDEFINED; + // TODO: exception!! + break; + } + } + + if (this->output->getLevel() == new_out) return; + + this->output->setLevel(new_out); } GateAnd::GateAnd(const GateAnd * prototype) : Node() { } diff --git a/Net.cpp b/Net.cpp index 5b3f5e3..249b6bb 100644 --- a/Net.cpp +++ b/Net.cpp @@ -1,10 +1,6 @@ #include "Net.h" #include -Net::Net(){} - -Net::~Net(){} - void Net::setLevel(SignalLevel level){ this->level = level; std::cout << this->size() << std::endl; diff --git a/Net.h b/Net.h index 150c9b6..7168186 100644 --- a/Net.h +++ b/Net.h @@ -2,14 +2,18 @@ #include "Observer.h" -enum SignalLevel {LOW, HIGH, UNDEFINED}; +enum SignalLevel { + LOW, + HIGH, + UNDEFINED +}; -class Net: public Subject { +class Net : public Subject { private: SignalLevel level = UNDEFINED; public: - Net(/* args */); - ~Net(); + Net() = default; + virtual ~Net() = default; virtual void setLevel(SignalLevel); virtual SignalLevel getLevel(); }; diff --git a/Node.cpp b/Node.cpp index a0fc27b..1b0b9b8 100644 --- a/Node.cpp +++ b/Node.cpp @@ -1,17 +1,18 @@ +#include + #include "Node.h" #include "NodeFactory.h" - -#include +#include "Net.h" Node::Node(const char * type) { NodeFactory::assign(type, this); } -void Node::addInput(Net* net) { +void Node::addInput(Net * net) { net->attach(this); } -void Node::setOutput(Net* net){ +void Node::setOutput(Net * net){ this->output = net; } diff --git a/docs/class-diag.puml b/docs/class-diag.puml index 5efda1b..3c572bd 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -67,7 +67,7 @@ class Circuit { + createNode(string type, string label) + createLink(string labelA, string labelB) - - nets: Map + - nets: vector - nodes: Map } -- cgit v1.2.3