aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 12:14:51 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 12:14:51 +0200
commit2b941bc1b28e0a60da50baa0875d7cb05c0632c1 (patch)
tree495dff0f929cc2588273fb838fc46380fa8578cc
parent2acd9a0097bb89e4b10ff77848d314e60f27be54 (diff)
WIP connect nets
-rw-r--r--Circuit.cpp27
-rw-r--r--Circuit.h4
-rw-r--r--GateAnd.cpp43
-rw-r--r--Net.cpp4
-rw-r--r--Net.h12
-rw-r--r--Node.cpp9
-rw-r--r--docs/class-diag.puml2
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;
}
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<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() { }
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 <iostream>
-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 <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*>
}