diff --git a/src/AndGate.cpp b/src/AndGate.cpp new file mode 100644 index 0000000..af530d3 --- /dev/null +++ b/src/AndGate.cpp @@ -0,0 +1,31 @@ +#include "AndGate.h" + +using namespace std; + +AndGate::AndGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, + wire* wire3) { + e = eQueue; + delay = d; + in1 = wire1; + in2 = wire2; + out = wire3; +} +int AndGate::evaluate(int evTime) { + 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); + } + } +} + +int AndGate::getDelay() +{ + return delay; +} diff --git a/src/AndGate.h b/src/AndGate.h new file mode 100644 index 0000000..b373c60 --- /dev/null +++ b/src/AndGate.h @@ -0,0 +1,14 @@ +#ifndef AND +#define AND +#include "Gate.h" +#include "Event.h" +#include "Wire.h" + +class AndGate : public Gate { + public: + AndGate(int d, Wire* wire1, Wire* wire2, Wire* wire3); + Event evaluate(int evTime); + int getDelay(); +}; + +#endif // !AND diff --git a/src/Event.cpp b/src/Event.cpp new file mode 100644 index 0000000..8c275ef --- /dev/null +++ b/src/Event.cpp @@ -0,0 +1,16 @@ +#include "Event.h" + +Event::Event(int num, int value, int setTime, Wire * output){ + evNum = num; + evValue = value; + evTime = setTime; + out = output; +} + +bool operator<(const Event &e1, const Event &e2) { + if(e1.evTime == e2.evTime) { + return e1.evNum > e2.evNum; + } + return e1.evTime > e2.evTime; +} + diff --git a/src/Event.h b/src/Event.h new file mode 100644 index 0000000..4a482de --- /dev/null +++ b/src/Event.h @@ -0,0 +1,16 @@ +#ifndef EVENT +#define EVENT + +using namespace std; +#include "Wire.h" + +class Event { + public: + Event(int num, int value, int setTime, Wire * output); + friend bool operator<(const Event &e1, const Event &e2); + private: + int evNum, evValue, evTime; + Wire *out; +}; + +#endif // !EVENT diff --git a/src/Gate.h b/src/Gate.h new file mode 100644 index 0000000..5607793 --- /dev/null +++ b/src/Gate.h @@ -0,0 +1,17 @@ +#ifndef GATE +#define GATE + +#include "Event.h" + +class Wire; + +class Gate { + public: + virtual Event evaluate(int) = 0; + + protected: + Wire *in1, *in2, *out; + int delay; +}; + +#endif // !GATE diff --git a/src/NandGate.cpp b/src/NandGate.cpp new file mode 100644 index 0000000..9c449a2 --- /dev/null +++ b/src/NandGate.cpp @@ -0,0 +1,30 @@ +#include "NandGate.h" + +NandGate::NandGate(int d, Wire * wire1, Wire * wire2, Wire * wire3) +{ + delay = d; + in1 = wire1; + in2 = wire2; + out = wire3; +} + +void NandGate::evaluate(int evTime) +{ + 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 { + if (out->getValue(evTime + delay) != 0) { + e->push(event(e->size, 0, evTime + delay, out)); + out->setValue(0, evTime + delay); + } + } +} + +int NandGate::getDelay() +{ + return delay; +} diff --git a/src/NandGate.h b/src/NandGate.h new file mode 100644 index 0000000..fbfd167 --- /dev/null +++ b/src/NandGate.h @@ -0,0 +1,16 @@ +#ifndef NAND +#define NAND +#include "Gate.h" +#include "Event.h" +#include "Wire.h" + +using namespace std; + +class NandGate : public Gate { + public: + NandGate(int d, Wire* wire1, Wire* wire2, Wire* wire3); + void evaluate(int time); + int getDelay(); +}; + +#endif // !NAND diff --git a/src/NorGate.cpp b/src/NorGate.cpp new file mode 100644 index 0000000..77bc51d --- /dev/null +++ b/src/NorGate.cpp @@ -0,0 +1,31 @@ +#include "NorGate.h" + +using namespace std; + +NorGate::NorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { + e = eQueue; + delay = d; + in1 = wire1; + in2 = wire2; + out = wire3; +} + +void NorGate::evaluate(int evTime) { + 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); + } + } +} + +int NorGate::getDelay() +{ + return delay; +} diff --git a/src/NorGate.h b/src/NorGate.h new file mode 100644 index 0000000..ee0d430 --- /dev/null +++ b/src/NorGate.h @@ -0,0 +1,14 @@ +#ifndef NOR +#define NOR +#include "Gate.h" +#include "Event.h" +#include "Wire.h" + +class NorGate : public Gate { + public: + NorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3); + void evaluate(int evTime); + int getDelay(); +}; + +#endif // !NOR diff --git a/src/NotGate.cpp b/src/NotGate.cpp new file mode 100644 index 0000000..6a97658 --- /dev/null +++ b/src/NotGate.cpp @@ -0,0 +1,30 @@ +#include "NotGate.h" + +using namespace std; + +NotGate::NotGate(int d, Wire* wire1, Wire* wire2) { + delay = d; + in1 = wire1; + out = wire2; + +} + +void NotGate::evaluate(int evTime) { + 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); + } + } +} + +int NotGate::getDelay() +{ + return delay; +} diff --git a/src/NotGate.h b/src/NotGate.h new file mode 100644 index 0000000..f4ca66c --- /dev/null +++ b/src/NotGate.h @@ -0,0 +1,14 @@ +#ifndef NOT +#define NOT +#include "Gate.h" +#include "Wire.h" +#include "Event.h" + +class NotGate : public Gate { + public: + NotGate(int d, Wire* wire1, Wire* wire2); + void evaluate(int evTime); + int getDelay(); +}; + +#endif // !NOT diff --git a/src/OrGate.cpp b/src/OrGate.cpp new file mode 100644 index 0000000..92be0e8 --- /dev/null +++ b/src/OrGate.cpp @@ -0,0 +1,30 @@ +#include "OrGate.h" + +using namespace std; + +OrGate::OrGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { + delay = d; + in1 = wire1; + in2 = wire2; + out = wire3; +} + +void OrGate::evaluate(int evTime) { + 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); + } + } +} + +int OrGate::getDelay() +{ + return delay; +} diff --git a/src/OrGate.h b/src/OrGate.h new file mode 100644 index 0000000..5eec0fc --- /dev/null +++ b/src/OrGate.h @@ -0,0 +1,14 @@ +#ifndef OR +#define OR +#include "Gate.h" +#include "Event.h" +#include "Wire.h" + +class OrGate : public Gate { + public: + OrGate(int d, Wire* wire1, Wire* wire2, Wire* wire3); + void evaluate(int evTime); + int getDelay(); +}; + +#endif // !OR diff --git a/src/wire.cpp b/src/wire.cpp index e15146e..dec8a8d 100644 --- a/src/wire.cpp +++ b/src/wire.cpp @@ -1,8 +1,8 @@ -#include "wire.h" +#include "Wire.h" -wire::wire(int number, bool io, string inName) +Wire::Wire(int number, bool io, string inName) { - wireNumber = number; + WireNumber = number; isPrint = io; name = inName; value = -1;