From 1832d7adffb8136a9df345b5ad344a80f2342ff5 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 11 Apr 2017 13:22:37 -0400 Subject: [PATCH] Add the logic for the gates and fix the wire set and get value functions. --- src/andGate.cpp | 15 +++++++++++---- src/nandGate.cpp | 16 +++++++++------- src/norGate.cpp | 13 ++++++++++++- src/notGate.cpp | 14 +++++++++++--- src/orGate.cpp | 13 ++++++++++++- src/wire.cpp | 20 +++++++++++--------- src/wire.h | 2 +- src/xnorGate.cpp | 15 ++++++++++++++- src/xorGate.cpp | 15 ++++++++++++++- 9 files changed, 95 insertions(+), 28 deletions(-) diff --git a/src/andGate.cpp b/src/andGate.cpp index 27deace..eba33fd 100644 --- a/src/andGate.cpp +++ b/src/andGate.cpp @@ -11,9 +11,16 @@ andGate::andGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, out = wire3; } int andGate::evaluate(int evTime) { - int val1 = in1->getValue(evTime); - int val2 = in2->getValue(evTime); - if(val1 != -1 && val2 != -1) { - return val1 && val2; + if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } + else if(in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1){ + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); + } } } diff --git a/src/nandGate.cpp b/src/nandGate.cpp index d25be20..7e87da9 100644 --- a/src/nandGate.cpp +++ b/src/nandGate.cpp @@ -9,16 +9,18 @@ nandGate::nandGate(priority_queue *eQueue, int d, wire * wire1, wire * wi e = eQueue; } -void nandGate::evaluate(int time) +void nandGate::evaluate(int evTime) { - if (in1->getValue(time) == 0 || in2->getValue(time) == 0) { - if (out->getValue(time + delay) != 1) { - e->push(event(e->size, 1, time + delay, out)); - out->setValue(1, time + delay); + if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) { + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); } } else { - e->push(event(e->size, 0, time + delay, out)); - out->setValue(0, time + delay); + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } } } \ No newline at end of file diff --git a/src/norGate.cpp b/src/norGate.cpp index 5d4b949..1538430 100644 --- a/src/norGate.cpp +++ b/src/norGate.cpp @@ -12,5 +12,16 @@ norGate::norGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, } void norGate::evaluate(int evTime) { - //TODO + if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } + else if (in1->getValue(evTime) == 0 && in2->getValue(evTime) == 0) { + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); + } + } } diff --git a/src/notGate.cpp b/src/notGate.cpp index 8634950..1513603 100644 --- a/src/notGate.cpp +++ b/src/notGate.cpp @@ -11,8 +11,16 @@ notGate::notGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2) } void notGate::evaluate(int evTime) { - int val = in1->getValue(); - - if(val != -1) { + if (in1->getValue(evTime) == 0) { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } + else{ + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); + } } } diff --git a/src/orGate.cpp b/src/orGate.cpp index 649c300..031f0d9 100644 --- a/src/orGate.cpp +++ b/src/orGate.cpp @@ -12,5 +12,16 @@ orGate::orGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, } void orGate::evaluate(int evTime) { - //TODO + if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) { + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); + } + } + else if (in1->getValue(evTime) == 0 && in2->getValue(evTime) == 0) { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } } diff --git a/src/wire.cpp b/src/wire.cpp index f4765de..e15146e 100644 --- a/src/wire.cpp +++ b/src/wire.cpp @@ -3,26 +3,28 @@ wire::wire(int number, bool io, string inName) { wireNumber = number; - isInput = io; + isPrint = io; name = inName; value = -1; lastEvent = 0; + history.insert(history.begin(), 60, -1); } int wire::getValue(int time) const { - return history[time]; + for (auto i = history.begin() + time; i != history.begin(); i--) { + if (i[0] != -1) { + return i[0]; + } + } + return -1; } void wire::setValue(int newValue, int setTime) { - while (setTime < 60 && - (history[setTime] == -1 || history[setTime] == history[setTime+1])) { - history[setTime] = newValue; - if (lastEvent < setTime) { - lastEvent = setTime; - } - setTime++; + history[setTime] = newValue; + if (lastEvent < setTime) { + lastEvent = setTime; } } diff --git a/src/wire.h b/src/wire.h index 52e6c93..8e15386 100644 --- a/src/wire.h +++ b/src/wire.h @@ -21,7 +21,7 @@ class wire { private: int wireNumber, value, lastEvent; - vector history (60, -1); + vector history; string name; bool isPrint; vector gates; diff --git a/src/xnorGate.cpp b/src/xnorGate.cpp index 3779fe0..1b8fb33 100644 --- a/src/xnorGate.cpp +++ b/src/xnorGate.cpp @@ -12,5 +12,18 @@ xnorGate::xnorGate(priority_queue *eQueue, int d, wire* wire1, } void xnorGate::evaluate(int evTime) { - //TODO + if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) { + if (in1->getValue(evTime) == in2->getValue(evTime)) { + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); + } + } + else { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } + } } diff --git a/src/xorGate.cpp b/src/xorGate.cpp index 0d356b2..b5cb7d4 100644 --- a/src/xorGate.cpp +++ b/src/xorGate.cpp @@ -12,5 +12,18 @@ xorGate::xorGate(priority_queue *eQueue, int d, wire* wire1, } void xorGate::evaluate(int evTime) { - //TODO + if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) { + if (in1->getValue(evTime) != in2->getValue(evTime)) { + if (out->getValue(evTime + delay) != 1) { + e->push(event(e->size, 1, evTime + delay, out)); + out->setValue(1, evTime + delay); + } + } + else { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } + } }