diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-12 12:14:51 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-12 12:14:51 +0200 |
commit | 2b941bc1b28e0a60da50baa0875d7cb05c0632c1 (patch) | |
tree | 495dff0f929cc2588273fb838fc46380fa8578cc | |
parent | 2acd9a0097bb89e4b10ff77848d314e60f27be54 (diff) |
WIP connect nets
-rw-r--r-- | Circuit.cpp | 27 | ||||
-rw-r--r-- | Circuit.h | 4 | ||||
-rw-r--r-- | GateAnd.cpp | 43 | ||||
-rw-r--r-- | Net.cpp | 4 | ||||
-rw-r--r-- | Net.h | 12 | ||||
-rw-r--r-- | Node.cpp | 9 | ||||
-rw-r--r-- | 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<string> 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<string> 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; } @@ -18,10 +18,10 @@ public: public: void create(string label, vector<string> nodes); void new_node(string label, string type); - void new_net(string label, string node); + void new_net(string src, vector<string> dests); private: std::map<string, Node *> nodes = {}; - std::map<string, Net *> nets = {}; + vector<Net *> 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() { } @@ -1,10 +1,6 @@ #include "Net.h" #include <iostream> -Net::Net(){} - -Net::~Net(){} - void Net::setLevel(SignalLevel level){ this->level = level; std::cout << this->size() << std::endl; @@ -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(); }; @@ -1,17 +1,18 @@ +#include <iostream> + #include "Node.h" #include "NodeFactory.h" - -#include <iostream> +#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<string label, Net*> + - nets: vector<Net*> - nodes: Map<string label, Node*> } |