aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GateAnd.cpp48
-rw-r--r--GateAnd.h3
-rw-r--r--Observer.cpp6
-rw-r--r--Observer.h2
-rw-r--r--main.cpp22
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() { }
diff --git a/GateAnd.h b/GateAnd.h
index 5eedc07..187f7b8 100644
--- a/GateAnd.h
+++ b/GateAnd.h
@@ -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++)
diff --git a/Observer.h b/Observer.h
index 7b2db7e..580d539 100644
--- a/Observer.h
+++ b/Observer.h
@@ -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();
diff --git a/main.cpp b/main.cpp
index 01ddeb7..0ff11b2 100644
--- a/main.cpp
+++ b/main.cpp
@@ -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