diff options
-rw-r--r-- | Circuit.cpp | 6 | ||||
-rw-r--r-- | Circuit.h | 7 | ||||
-rw-r--r-- | Exception.cpp | 23 | ||||
-rw-r--r-- | Exception.h | 16 | ||||
-rw-r--r-- | GateAnd.cpp | 2 | ||||
-rw-r--r-- | GateAnd.h | 5 | ||||
-rw-r--r-- | Node.cpp | 2 | ||||
-rw-r--r-- | Node.h | 3 | ||||
-rw-r--r-- | NodeInput.cpp | 28 | ||||
-rw-r--r-- | NodeInput.h | 43 | ||||
-rw-r--r-- | NodeOutput.cpp | 21 | ||||
-rw-r--r-- | NodeOutput.h | 16 | ||||
-rw-r--r-- | Parser.cpp | 19 | ||||
-rw-r--r-- | Parser.h | 11 | ||||
-rw-r--r-- | main.cpp | 12 | ||||
-rw-r--r-- | readme.md | 1 |
16 files changed, 172 insertions, 43 deletions
diff --git a/Circuit.cpp b/Circuit.cpp index 102096c..22ea98c 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -34,6 +34,12 @@ void Circuit::new_net(string src, vector<string> 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; @@ -16,9 +16,10 @@ public: virtual ~Circuit(); public: - void create(string label, vector<string> nodes); - void new_node(string label, string type); - void new_net(string src, vector<string> dests); + virtual void create(string label, vector<string> nodes); + virtual void new_node(string label, string type); + virtual void new_net(string src, vector<string> dests); + virtual void sim(); private: std::map<string, Node *> nodes = {}; 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 <cstdarg> +#include <cstdio> + +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 <exception> + +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/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++){ @@ -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: @@ -18,6 +18,6 @@ void Node::setOutput(Net * net){ void Node::update(){ std::cout << "updated" << std::endl; - this->compare(); + this->sim(); } @@ -19,8 +19,7 @@ public: void update(); virtual void addInput(Net *); virtual void setOutput(Net *); - virtual void compare() = 0; - int gert = 45; + virtual void sim() = 0; protected: Node(const char * type); diff --git a/NodeInput.cpp b/NodeInput.cpp new file mode 100644 index 0000000..17f9ed5 --- /dev/null +++ b/NodeInput.cpp @@ -0,0 +1,28 @@ +#include "NodeInput.h" + +#include <iostream> + +NodeInput::NodeInput(const char * type) : Node(type) { } + +void NodeInput::sim() { + if (this->output == nullptr) return; + std::cout << this->level << " bar\n"; + this->output->setLevel(this->level); +} + +NodeInput::NodeInput(const NodeInput * prototype) : Node() { } + +NodeInput * NodeInput::clone() const { + return new NodeInput(this); +} + +// INPUT_LOW +NodeInputLow NodeInputLow::instance(NodeInputLow::type); +NodeInputLow::NodeInputLow(const char * type) : NodeInput() { } + +// NodeInputLow::NodeInputLow(const NodeInputLow * prototype) : NodeInput() { } + +// // INPUT_HIGH +// NodeInputHigh NodeInputHigh::instance(NodeInputHigh::type); +// NodeInputHigh::NodeInputHigh(const char * type) : NodeInput(type) { } +// NodeInputHigh::NodeInputHigh(const NodeInputHigh * prototype) : NodeInput() { } diff --git a/NodeInput.h b/NodeInput.h new file mode 100644 index 0000000..bcca8f4 --- /dev/null +++ b/NodeInput.h @@ -0,0 +1,43 @@ +#pragma once + +#include "Node.h" + +class NodeInput : public Node { +public: + NodeInput() = default; + NodeInput(const NodeInput * prototype); + ~NodeInput() = default; + virtual void sim(); + virtual NodeInput * clone() const; + +private: + NodeInput(const char * type); + + SignalLevel level = UNDEFINED; +}; + +// Input LOW and HIGH unicorns: + +class NodeInputLow : public NodeInput { +public: + // NodeInputLow(const NodeInputLow * prototype); + // ~NodeInputLow() = default; + // virtual void compare(); + // virtual NodeInputLow * clone() const; + +private: + NodeInputLow(const char * type); + constexpr static const char * type = "input_low"; + static NodeInputLow instance; + + SignalLevel level = LOW; +}; + +class NodeInputHigh : public NodeInput { +private: + NodeInputHigh(const char * type); + constexpr static const char * type = "input_high"; + static NodeInputHigh instance; + + SignalLevel level = HIGH; +}; diff --git a/NodeOutput.cpp b/NodeOutput.cpp new file mode 100644 index 0000000..00ca006 --- /dev/null +++ b/NodeOutput.cpp @@ -0,0 +1,21 @@ +#include "NodeOutput.h" +#include "Exception.h" + +#include <iostream> + +NodeOutput NodeOutput::instance(NodeOutput::type); + +NodeOutput::NodeOutput(const char * type) : Node(type) { } + +void NodeOutput::sim() { + 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() { } + +NodeOutput * NodeOutput::clone() const { + return new NodeOutput(this); +}
\ No newline at end of file diff --git a/NodeOutput.h b/NodeOutput.h new file mode 100644 index 0000000..2e92bf0 --- /dev/null +++ b/NodeOutput.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Node.h" + +class NodeOutput : public Node { +public: + NodeOutput(const NodeOutput * prototype); + ~NodeOutput() = default; + virtual void sim(); + virtual NodeOutput * clone() const; + +private: + NodeOutput(const char * type); + constexpr static const char * type = "probe"; + static NodeOutput instance; +}; @@ -1,29 +1,10 @@ #include <cstring> #include <sstream> -#include <cstdarg> #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 @@ -2,22 +2,15 @@ #include <iostream> #include <istream> -#include <exception> + #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: @@ -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; @@ -11,6 +11,7 @@ make - Observer - Low binding factory - Prototype +- Strategy - Dependency injection - vast meer! |