diff options
author | UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> | 2024-06-12 15:52:26 +0200 |
---|---|---|
committer | UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> | 2024-06-12 15:52:26 +0200 |
commit | d0aa3cb4e42d9ee60756b3807f4d23776a6f19c3 (patch) | |
tree | 5915231749ee4912397d8e7c62d4a860e7049da4 | |
parent | f134abd08ccfdb1ece39df4d4a0e422419bd2fed (diff) |
WIP
-rw-r--r-- | GateAnd.cpp | 48 | ||||
-rw-r--r-- | GateAnd.h | 3 | ||||
-rw-r--r-- | Observer.cpp | 6 | ||||
-rw-r--r-- | Observer.h | 2 | ||||
-rw-r--r-- | main.cpp | 22 |
5 files changed, 57 insertions, 24 deletions
diff --git a/GateAnd.cpp b/GateAnd.cpp index dc65353..5760117 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -1,34 +1,36 @@ #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(); + + if (l == UNDEFINED) return UNDEFINED; + if (l == LOW) return LOW; + } + return HIGH; +} + // 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); -// } + 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() { } @@ -4,12 +4,15 @@ class GateAnd : public Node { public: + GateAnd() {}; GateAnd(const GateAnd * prototype); virtual ~GateAnd() = default; virtual void compare(); virtual GateAnd * clone() const; private: + SignalLevel level(); + GateAnd(const char * type); constexpr static const char * type = "and"; static GateAnd instance; diff --git a/Observer.cpp b/Observer.cpp index 1d96ed4..d2a1062 100644 --- a/Observer.cpp +++ b/Observer.cpp @@ -5,6 +5,7 @@ void Observer::update(){ std::cout << 'a' << std::endl; } + void Subject::attach(Observer* obs){ std::cout << "added" << std::endl; this->observers.push_back(obs); @@ -14,6 +15,11 @@ void Subject::detach(Observer*){ } +int Subject::size() { + std::cout << "subject list size " << this->observers.size() << std::endl; + return this->observers.size(); +} + // TODO possibly add foo input as update value? void Subject::notify() { for (int i = 0; i < this->observers.size(); i++) @@ -16,7 +16,7 @@ public: // virtual void attach(Observer* obs) { observers.push_back(obs);} virtual void attach(Observer* obs); virtual void detach(Observer*); - virtual int size() { return this->observers.size(); } + virtual int size(); // TODO possibly add foo input as update value? virtual void notify(); @@ -8,6 +8,27 @@ using std::cout; using std::endl; using std::ifstream; +#include "Node.h" +#include "GateAnd.h" + +int main() { + // Observer ob(); + Net n, n1, o; + Node *g = new GateAnd; + g->addInput(&n); + g->addInput(&n1); + g->setOutput(&o); + + // o.setLevel(UNDEFINED); + n.setLevel(LOW); + n1.setLevel(LOW); + int level = 22; + level = o.getLevel(); + printf("hello world! %d\n", level); + return 0; +} + +/* int main(int argc, char** argv) { Parser main_parser; Circuit circuit; @@ -36,3 +57,4 @@ int main(int argc, char** argv) { return EXIT_SUCCESS; } +*/
\ No newline at end of file |