From d1e381e301bb55432997ff355b5c938e76cd4f20 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:51:32 +0200 Subject: New OrGate WHOOO --- GateAnd.cpp | 19 ------------------- GateAnd.h | 1 - GateOr.cpp | 23 +++++++++++++++++++++++ GateOr.h | 19 +++++++++++++++++++ Node.cpp | 16 +++++++++++++++- Node.h | 8 +++++++- NodeInput.cpp | 4 ++-- NodeInput.h | 7 ++++--- NodeOutput.cpp | 4 ++-- NodeOutput.h | 1 + Observer.cpp | 6 ------ Observer.h | 2 +- 12 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 GateOr.cpp create mode 100644 GateOr.h diff --git a/GateAnd.cpp b/GateAnd.cpp index 487f8bc..c48d19c 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -1,13 +1,10 @@ #include "GateAnd.h" -#include "Exception.h" GateAnd GateAnd::instance(GateAnd::type); GateAnd::GateAnd(const char * type) : Node(type) { } SignalLevel GateAnd::level() { - if (this->inputs.size() < 1) throw CircuitException("AndGate input size error"); - for (int i = 0; i < this->inputs.size(); i++){ SignalLevel l = this->inputs[i]->getLevel(); @@ -17,22 +14,6 @@ SignalLevel GateAnd::level() { return HIGH; } -// Concrete Nodes: -void GateAnd::sim() { - SignalLevel new_out = this->level(); - - if (new_out == UNDEFINED) return; - - printf("io size: %lu\n", this->inputs.size()); - // TODO: fix segfault somewhere below - - - - if (this->output->getLevel() == new_out) return; - - this->output->setLevel(new_out); -} - GateAnd::GateAnd(const GateAnd * prototype) : Node() { } GateAnd * GateAnd::clone() const { diff --git a/GateAnd.h b/GateAnd.h index 142c628..a8cc3d0 100644 --- a/GateAnd.h +++ b/GateAnd.h @@ -7,7 +7,6 @@ public: GateAnd() = default; GateAnd(const GateAnd * prototype); ~GateAnd() = default; - virtual void sim(); virtual GateAnd * clone() const; private: diff --git a/GateOr.cpp b/GateOr.cpp new file mode 100644 index 0000000..db1e04a --- /dev/null +++ b/GateOr.cpp @@ -0,0 +1,23 @@ +#include "GateOr.h" + +GateOr GateOr::instance(GateOr::type); + +SignalLevel GateOr::level() { + if (this->inputs.size() < 1) throw CircuitException("Or-gate input size error"); + + SignalLevel new_level = LOW; + for (int i = 0; i < this->inputs.size(); i++){ + SignalLevel l = this->inputs[i]->getLevel(); + + if (l == UNDEFINED) return UNDEFINED; + if (l == HIGH) new_level = HIGH; + } + return new_level; +} + +GateOr::GateOr(const GateOr * prototype) : Node() { } + +GateOr * GateOr::clone() const { + return new GateOr(this); +} + diff --git a/GateOr.h b/GateOr.h new file mode 100644 index 0000000..de694be --- /dev/null +++ b/GateOr.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Node.h" + +class GateOr : public Node { +public: + GateOr() = default; + GateOr(const GateOr * prototype); + ~GateOr() = default; + virtual GateOr * clone() const; + +private: + SignalLevel level(); + + using Node::Node; + constexpr static const char * type = "or"; + static GateOr instance; +}; + diff --git a/Node.cpp b/Node.cpp index 75f94e5..4ba3961 100644 --- a/Node.cpp +++ b/Node.cpp @@ -17,8 +17,22 @@ void Node::setOutput(Net * net){ this->output = net; } +void Node::sim() { + size_t input_size = this->inputs.size(); + if (this->min_inputs >= 0 && input_size < min_inputs) + throw CircuitException("Too few inputs"); + if (this->max_inputs >= 0 && input_size > max_inputs) + throw CircuitException("Too many inputs"); + + SignalLevel new_out = this->level(); + + if (new_out == UNDEFINED) return; + if (this->output->getLevel() == new_out) return; + + this->output->setLevel(new_out); +} + void Node::update(){ - std::cout << "updated" << std::endl; this->sim(); } diff --git a/Node.h b/Node.h index b7f6681..3496851 100644 --- a/Node.h +++ b/Node.h @@ -5,6 +5,7 @@ #include "Observer.h" #include "Net.h" +#include "Exception.h" using std::string; using std::vector; @@ -19,7 +20,8 @@ public: void update(); virtual void addInput(Net *); virtual void setOutput(Net *); - virtual void sim() = 0; + virtual void sim(); + virtual SignalLevel level() = 0; protected: Node(const char * type); @@ -28,5 +30,9 @@ protected: vector inputs; Net * output; + +private: + int min_inputs = -1; + int max_inputs = -1; }; diff --git a/NodeInput.cpp b/NodeInput.cpp index 17f9ed5..0fc8110 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -6,8 +6,8 @@ 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); + + this->output->setLevel(this->input); } NodeInput::NodeInput(const NodeInput * prototype) : Node() { } diff --git a/NodeInput.h b/NodeInput.h index bcca8f4..71cf36c 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -8,12 +8,13 @@ public: NodeInput(const NodeInput * prototype); ~NodeInput() = default; virtual void sim(); + SignalLevel level() { return UNDEFINED; }; virtual NodeInput * clone() const; private: NodeInput(const char * type); - SignalLevel level = UNDEFINED; +SignalLevel input = UNDEFINED; }; // Input LOW and HIGH unicorns: @@ -30,7 +31,7 @@ private: constexpr static const char * type = "input_low"; static NodeInputLow instance; - SignalLevel level = LOW; + SignalLevel input = LOW; }; class NodeInputHigh : public NodeInput { @@ -39,5 +40,5 @@ private: constexpr static const char * type = "input_high"; static NodeInputHigh instance; - SignalLevel level = HIGH; + SignalLevel input = HIGH; }; diff --git a/NodeOutput.cpp b/NodeOutput.cpp index 00ca006..5138805 100644 --- a/NodeOutput.cpp +++ b/NodeOutput.cpp @@ -11,11 +11,11 @@ void NodeOutput::sim() { if (this->inputs.size() == 0) throw CircuitException("No inputs on probe"); - std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; + std::cout << "Probe signal level: " << this->inputs[0]->getLevel() << 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 index 2e92bf0..3b60335 100644 --- a/NodeOutput.h +++ b/NodeOutput.h @@ -7,6 +7,7 @@ public: NodeOutput(const NodeOutput * prototype); ~NodeOutput() = default; virtual void sim(); + SignalLevel level() { return UNDEFINED; }; virtual NodeOutput * clone() const; private: diff --git a/Observer.cpp b/Observer.cpp index ff523b8..7d5dc22 100644 --- a/Observer.cpp +++ b/Observer.cpp @@ -2,12 +2,7 @@ #include "Observer.h" -void Observer::update(){ - std::cout << 'a' << std::endl; -} - void Subject::attach(Observer * obs){ - std::cout << "added" << std::endl; this->observers.push_back(obs); } @@ -16,7 +11,6 @@ void Subject::detach(Observer *){ } int Subject::size() { - std::cout << "subject list size " << this->observers.size() << std::endl; return this->observers.size(); } diff --git a/Observer.h b/Observer.h index 580d539..0661bf0 100644 --- a/Observer.h +++ b/Observer.h @@ -5,7 +5,7 @@ class Observer { private: public: - virtual void update(); + virtual void update() = 0; }; -- cgit v1.2.3