aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 14:01:17 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 14:01:17 +0200
commitefa2f195a5f9c356318fb50532168fa2ee4c0979 (patch)
treef0e78786cd5b71ff0e389b7842f2a88f4b4f7358
parent72c9b98b074d98d60983b178b06e1872eb96c242 (diff)
parenta7eb10e81568b65c39d6e1d042309916a9180a2b (diff)
Merge branch 'master' of github.com:lonkaars/depa
-rw-r--r--Circuit.cpp6
-rw-r--r--Circuit.h7
-rw-r--r--Exception.cpp23
-rw-r--r--Exception.h16
-rw-r--r--GateAnd.cpp2
-rw-r--r--GateAnd.h5
-rw-r--r--Node.cpp2
-rw-r--r--Node.h3
-rw-r--r--NodeInput.cpp28
-rw-r--r--NodeInput.h43
-rw-r--r--NodeOutput.cpp21
-rw-r--r--NodeOutput.h16
-rw-r--r--Parser.cpp19
-rw-r--r--Parser.h11
-rw-r--r--main.cpp12
-rw-r--r--readme.md1
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;
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<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++){
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..b7f6681 100644
--- a/Node.h
+++ b/Node.h
@@ -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;
+};
diff --git a/Parser.cpp b/Parser.cpp
index 84c3217..c8a90b4 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -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
diff --git a/Parser.h b/Parser.h
index 3a86eec..b6d7bd5 100644
--- a/Parser.h
+++ b/Parser.h
@@ -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:
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;
diff --git a/readme.md b/readme.md
index 1703a9f..3ae509d 100644
--- a/readme.md
+++ b/readme.md
@@ -11,6 +11,7 @@ make
- Observer
- Low binding factory
- Prototype
+- Strategy
- Dependency injection
- vast meer!