diff --git a/src/Wire.cpp b/src/Wire.cpp new file mode 100644 index 0000000..62a2ba4 --- /dev/null +++ b/src/Wire.cpp @@ -0,0 +1,39 @@ +#include "Wire.h" + +Wire::Wire(int number, bool io, string inName) +{ + WireNumber = number; + isPrint = io; + name = inName; + value = -1; + lastEvent = 0; + history.insert(history.begin(), 60, -1); +} + +int Wire::getValue(int time) const +{ + 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) +{ + history[setTime] = newValue; + if (lastEvent < setTime) { + lastEvent = setTime; + } +} + +int Wire::getNumber() const +{ + return wireNumber; +} + +void Wire::addGate(Gate * newGate) +{ + gates.push_back(newGate); +} diff --git a/src/Wire.h b/src/Wire.h new file mode 100644 index 0000000..0829e15 --- /dev/null +++ b/src/Wire.h @@ -0,0 +1,30 @@ +#ifndef WIRE +#define WIRE + +#include +#include +#include + +using namespace std; + +class Gate; + +class Wire { + public: + Wire(int number, bool io, string inName = ""); + + int getValue(int time) const; + void setValue(int newValue, int setTime); + + int getNumber() const; + void addGate(Gate* newGate); + + private: + int wireNumber, value, lastEvent; + vector history; + string name; + bool isPrint; + vector gates; +}; + +#endif // !WIRE diff --git a/src/XnorGate.cpp b/src/XnorGate.cpp new file mode 100644 index 0000000..3155f6b --- /dev/null +++ b/src/XnorGate.cpp @@ -0,0 +1,32 @@ +#include "XnorGate.h" + +using namespace std; + +XnorGate::XnorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { + delay = d; + in1 = wire1; + in2 = wire2; + out = wire3; +} + +void XnorGate::evaluate(int evTime) { + 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); + } + } + } +} + +int XnorGate::getDelay() +{ + return delay; +} diff --git a/src/XnorGate.h b/src/XnorGate.h new file mode 100644 index 0000000..2448b84 --- /dev/null +++ b/src/XnorGate.h @@ -0,0 +1,14 @@ +#ifndef XNOR +#define XNOR +#include "Gate.h" +#include "Event.h" +#include "Wire.h" + +class XnorGate : public Gate { + public: + XnorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3); + void evaluate(int evTime); + int getDelay(); +}; + +#endif // !XNOR diff --git a/src/XorGate.cpp b/src/XorGate.cpp new file mode 100644 index 0000000..9169df4 --- /dev/null +++ b/src/XorGate.cpp @@ -0,0 +1,32 @@ +#include "XorGate.h" + +using namespace std; + +XorGate::XorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { + delay = d; + in1 = wire1; + in2 = wire2; + out = wire3; +} + +void XorGate::evaluate(int evTime) { + 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); + } + } + } +} + +int XorGate::getDelay() +{ + return delay; +} diff --git a/src/XorGate.h b/src/XorGate.h new file mode 100644 index 0000000..cadeef4 --- /dev/null +++ b/src/XorGate.h @@ -0,0 +1,14 @@ +#ifndef XOR +#define XOR +#include "Gate.h" +#include "Event.h" +#include "Wire.h" + +class XorGate : public Gate { + public: + XorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3); + void evaluate(int evTime); + int getDelay(); +}; + +#endif // !XOR