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 From c9c4b6caa2a14b7b5a338a6981a616506da7e78f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 12 Jun 2024 12:30:48 +0200 Subject: connect the nodes (no segfault edition) --- Circuit.cpp | 10 ++++++++-- Circuit.h | 2 ++ Node.h | 1 + Observer.cpp | 9 +++++---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Circuit.cpp b/Circuit.cpp index 393060f..7da9384 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -25,16 +25,22 @@ void Circuit::new_net(string src, vector dests) { nets.push_back(net); for (auto dest : dests) { - Node * node = nodes.find(dest)->second; + Node * node = find_node(dest); if (node == nullptr) continue; // TODO: exception! node->addInput(net); } - Node * node = nodes.find(src)->second; + Node * node = find_node(src); if (node == nullptr) return; // TODO: exception! node->setOutput(net); } +Node * Circuit::find_node(string label) { + auto map_index = this->nodes.find(label); + if (map_index == nodes.end()) return nullptr; + return map_index->second; +} + Circuit::~Circuit() { for (auto & n : nodes) delete n.second; diff --git a/Circuit.h b/Circuit.h index e681742..1806293 100644 --- a/Circuit.h +++ b/Circuit.h @@ -23,5 +23,7 @@ public: private: std::map nodes = {}; vector nets = {}; + + virtual Node * find_node(string label); }; diff --git a/Node.h b/Node.h index 43cfc6c..e6270a7 100644 --- a/Node.h +++ b/Node.h @@ -20,6 +20,7 @@ public: virtual void addInput(Net *); virtual void setOutput(Net *); virtual void compare() = 0; + int gert = 45; protected: Node(const char * type); diff --git a/Observer.cpp b/Observer.cpp index 1d96ed4..8d9823b 100644 --- a/Observer.cpp +++ b/Observer.cpp @@ -1,20 +1,21 @@ -#include "Observer.h" #include +#include "Observer.h" + void Observer::update(){ std::cout << 'a' << std::endl; } -void Subject::attach(Observer* obs){ +void Subject::attach(Observer * obs){ std::cout << "added" << std::endl; this->observers.push_back(obs); } -void Subject::detach(Observer*){ +void Subject::detach(Observer *){ } -// TODO possibly add foo input as update value? +// TODO: possibly add foo input as update value? void Subject::notify() { for (int i = 0; i < this->observers.size(); i++) this->observers[i]->update(); -- cgit v1.2.3 From 6fd8dcb9e0d33f7660c4e3a602a5c2a2b5091a7c Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:34:39 +0200 Subject: renamed compare function to sim function --- GateAnd.cpp | 2 +- GateAnd.h | 5 +++-- Node.cpp | 2 +- Node.h | 2 +- NodeInput.cpp | 6 ++++-- NodeInput.h | 2 +- NodeOutput.cpp | 6 ++++-- NodeOutput.h | 2 +- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/GateAnd.cpp b/GateAnd.cpp index 6f19d61..93edacb 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -4,7 +4,7 @@ GateAnd GateAnd::instance(GateAnd::type); GateAnd::GateAnd(const char * type) : Node(type) { } -void GateAnd::compare() { +void GateAnd::sim() { SignalLevel new_out = HIGH; // TODO: fix segfault somewhere below for (int i = 0; i < this->inputs.size(); i++){ diff --git a/GateAnd.h b/GateAnd.h index 5eedc07..12dad53 100644 --- a/GateAnd.h +++ b/GateAnd.h @@ -4,9 +4,10 @@ class GateAnd : public Node { public: + GateAnd() = default; GateAnd(const GateAnd * prototype); - virtual ~GateAnd() = default; - virtual void compare(); + ~GateAnd() = default; + virtual void sim(); virtual GateAnd * clone() const; private: diff --git a/Node.cpp b/Node.cpp index 1b0b9b8..d3564da 100644 --- a/Node.cpp +++ b/Node.cpp @@ -18,6 +18,6 @@ void Node::setOutput(Net * net){ void Node::update(){ std::cout << "updated" << std::endl; - this->compare(); + this->sim(); } diff --git a/Node.h b/Node.h index e6270a7..36da279 100644 --- a/Node.h +++ b/Node.h @@ -19,7 +19,7 @@ public: void update(); virtual void addInput(Net *); virtual void setOutput(Net *); - virtual void compare() = 0; + virtual void sim() = 0; int gert = 45; protected: diff --git a/NodeInput.cpp b/NodeInput.cpp index 046cd72..17f9ed5 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -1,10 +1,12 @@ #include "NodeInput.h" +#include + NodeInput::NodeInput(const char * type) : Node(type) { } -void NodeInput::compare() { +void NodeInput::sim() { if (this->output == nullptr) return; - + std::cout << this->level << " bar\n"; this->output->setLevel(this->level); } diff --git a/NodeInput.h b/NodeInput.h index 73ef6e1..bcca8f4 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -7,7 +7,7 @@ public: NodeInput() = default; NodeInput(const NodeInput * prototype); ~NodeInput() = default; - virtual void compare(); + virtual void sim(); virtual NodeInput * clone() const; private: diff --git a/NodeOutput.cpp b/NodeOutput.cpp index cff66c0..d47b6a3 100644 --- a/NodeOutput.cpp +++ b/NodeOutput.cpp @@ -5,9 +5,11 @@ NodeOutput NodeOutput::instance(NodeOutput::type); NodeOutput::NodeOutput(const char * type) : Node(type) { } -void NodeOutput::compare() { +void NodeOutput::sim() { if (this->inputs.size() > 0) { - std::cout << this->inputs[0]->getLevel() << std::endl; + std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; + } else { + std::cout << "err: No inputs on probe" << std::endl; } } diff --git a/NodeOutput.h b/NodeOutput.h index 4e7f742..2e92bf0 100644 --- a/NodeOutput.h +++ b/NodeOutput.h @@ -6,7 +6,7 @@ class NodeOutput : public Node { public: NodeOutput(const NodeOutput * prototype); ~NodeOutput() = default; - virtual void compare(); + virtual void sim(); virtual NodeOutput * clone() const; private: -- cgit v1.2.3 From 00c11d618d46067ed8656e5e9c834773c189cbc0 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:49:40 +0200 Subject: removed gert :< --- Node.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Node.h b/Node.h index 36da279..b7f6681 100644 --- a/Node.h +++ b/Node.h @@ -20,7 +20,6 @@ public: virtual void addInput(Net *); virtual void setOutput(Net *); virtual void sim() = 0; - int gert = 45; protected: Node(const char * type); -- cgit v1.2.3 From b93c2d642d7c9f94f36f408f3614741b29161c14 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:50:08 +0200 Subject: de big exception --- Exception.cpp | 23 +++++++++++++++++++++++ Exception.h | 16 ++++++++++++++++ Parser.cpp | 19 ------------------- Parser.h | 11 ++--------- 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 Exception.cpp create mode 100644 Exception.h diff --git a/Exception.cpp b/Exception.cpp new file mode 100644 index 0000000..52e30dc --- /dev/null +++ b/Exception.cpp @@ -0,0 +1,23 @@ +#include "Exception.h" + +#include +#include + +Exception::Exception(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + size_t sz = vsnprintf(NULL, 0, fmt, args) + 1; + if (error != NULL) free(error); + error = (char *) malloc(sz); + vsnprintf(error, sz, fmt, args); + va_end(args); +} + +Exception::~Exception() { + if (error != NULL) + free(error); +} + +const char * Exception::what() { + return error; +} \ No newline at end of file diff --git a/Exception.h b/Exception.h new file mode 100644 index 0000000..818f3f0 --- /dev/null +++ b/Exception.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +class Exception : public std::exception { +public: + Exception(const char * fmt, ...); + virtual ~Exception(); + virtual const char * what(); + +private: + char * error = NULL; +}; + +class ParserException : public Exception { using Exception::Exception; }; +class CircuitException : public Exception { using Exception::Exception; }; diff --git a/Parser.cpp b/Parser.cpp index 84c3217..c8a90b4 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -1,29 +1,10 @@ #include #include -#include #include "Parser.h" using std::getline; -ParserException::ParserException(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - size_t sz = vsnprintf(NULL, 0, fmt, args) + 1; - if (error != NULL) free(error); - error = (char *) malloc(sz); - vsnprintf(error, sz, fmt, args); - va_end(args); -} - -ParserException::~ParserException() { - if (error != NULL) - free(error); -} - -const char * ParserException::what() { - return error; -} size_t Parser::filter(char * input) { size_t diff --git a/Parser.h b/Parser.h index 3a86eec..b6d7bd5 100644 --- a/Parser.h +++ b/Parser.h @@ -2,22 +2,15 @@ #include #include -#include + #include "Circuit.h" +#include "Exception.h" using std::istream; using std::string; -class ParserException : public std::exception { -public: - ParserException(const char * fmt, ...); - virtual ~ParserException(); - virtual const char * what(); -private: - char * error = NULL; -}; class Parser { public: -- cgit v1.2.3 From 064512cb02465ec598e46ae4bc7947395ed66dc2 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:58:02 +0200 Subject: now trowing error on no probe input --- NodeOutput.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/NodeOutput.cpp b/NodeOutput.cpp index d47b6a3..00ca006 100644 --- a/NodeOutput.cpp +++ b/NodeOutput.cpp @@ -1,4 +1,6 @@ #include "NodeOutput.h" +#include "Exception.h" + #include NodeOutput NodeOutput::instance(NodeOutput::type); @@ -6,11 +8,10 @@ NodeOutput NodeOutput::instance(NodeOutput::type); NodeOutput::NodeOutput(const char * type) : Node(type) { } void NodeOutput::sim() { - if (this->inputs.size() > 0) { - std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; - } else { - std::cout << "err: No inputs on probe" << std::endl; - } + if (this->inputs.size() == 0) + throw CircuitException("No inputs on probe"); + + std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; } NodeOutput::NodeOutput(const NodeOutput * prototype) : Node() { } -- cgit v1.2.3 From a7eb10e81568b65c39d6e1d042309916a9180a2b Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:00:51 +0200 Subject: WIP simulate starter --- Circuit.cpp | 6 ++++++ Circuit.h | 7 ++++--- main.cpp | 12 ++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Circuit.cpp b/Circuit.cpp index 7da9384..23f49a6 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -35,6 +35,12 @@ void Circuit::new_net(string src, vector dests) { node->setOutput(net); } +void Circuit::sim() { + for (auto & node : nodes) { + node.second->sim(); + } +} + Node * Circuit::find_node(string label) { auto map_index = this->nodes.find(label); if (map_index == nodes.end()) return nullptr; diff --git a/Circuit.h b/Circuit.h index 1806293..0cc08c6 100644 --- a/Circuit.h +++ b/Circuit.h @@ -16,9 +16,10 @@ public: virtual ~Circuit(); public: - void create(string label, vector nodes); - void new_node(string label, string type); - void new_net(string src, vector dests); + virtual void create(string label, vector nodes); + virtual void new_node(string label, string type); + virtual void new_net(string src, vector dests); + virtual void sim(); private: std::map nodes = {}; diff --git a/main.cpp b/main.cpp index 01ddeb7..728155f 100644 --- a/main.cpp +++ b/main.cpp @@ -24,12 +24,12 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - // try { - // circuit.run(); - // } catch (exception& e) { - // cout << "Circuit error: " << e.what() << endl; - // return EXIT_FAILURE; - // } + try { + circuit.sim(); + } catch (CircuitException& e) { + cout << "Circuit error: " << e.what() << endl; + return EXIT_FAILURE; + } // cout << "Circuit output: " << circuit.getOutput() << endl; -- cgit v1.2.3 From 30b979c0c4a231d3c55a465ccba2ecf751058b62 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 12 Jun 2024 14:01:07 +0200 Subject: remove some printfs --- Circuit.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Circuit.cpp b/Circuit.cpp index 7da9384..102096c 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -16,11 +16,10 @@ void Circuit::new_node(string label, string type) { nodes[label] = node; - printf("[%s] (%s)\n", label.c_str(), type.c_str()); + // printf("[%s] (%s)\n", label.c_str(), type.c_str()); } void Circuit::new_net(string src, vector dests) { - printf("%s\n", src.c_str()); Net * net = new Net(); nets.push_back(net); -- cgit v1.2.3 From 72c9b98b074d98d60983b178b06e1872eb96c242 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 12 Jun 2024 14:01:14 +0200 Subject: update class diagram --- docs/class-diag.puml | 112 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 84 insertions(+), 28 deletions(-) diff --git a/docs/class-diag.puml b/docs/class-diag.puml index 3c572bd..4d9f489 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -1,31 +1,49 @@ @startuml -abstract class Node { /' (also ConcreteObserver) '/ - + setOutput(Net*) - + addInput(Net*) - - inputs: Net*[] - - output: Net* - - type: static const char * string - - minInputs: constexpr unsigned int - - maxInputs: constexpr int +skinparam linetype ortho + +class Node <