Add the logic for the gates and fix the wire set and get value functions.

This commit is contained in:
daniel 2017-04-11 13:22:37 -04:00
parent 5229141506
commit 1832d7adff
9 changed files with 95 additions and 28 deletions

View File

@ -11,9 +11,16 @@ andGate::andGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
out = wire3; out = wire3;
} }
int andGate::evaluate(int evTime) { int andGate::evaluate(int evTime) {
int val1 = in1->getValue(evTime); if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) {
int val2 = in2->getValue(evTime); if (out->getValue(evTime + delay) != 0) {
if(val1 != -1 && val2 != -1) { e->push(event(e->size, 0, evTime + delay, out));
return val1 && val2; 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);
}
} }
} }

View File

@ -9,16 +9,18 @@ nandGate::nandGate(priority_queue<event> *eQueue, int d, wire * wire1, wire * wi
e = eQueue; e = eQueue;
} }
void nandGate::evaluate(int time) void nandGate::evaluate(int evTime)
{ {
if (in1->getValue(time) == 0 || in2->getValue(time) == 0) { if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) {
if (out->getValue(time + delay) != 1) { if (out->getValue(evTime + delay) != 1) {
e->push(event(e->size, 1, time + delay, out)); e->push(event(e->size, 1, evTime + delay, out));
out->setValue(1, time + delay); out->setValue(1, evTime + delay);
} }
} }
else { else {
e->push(event(e->size, 0, time + delay, out)); if (out->getValue(evTime + delay) != 0) {
out->setValue(0, time + delay); e->push(event(e->size, 0, evTime + delay, out));
out->setValue(0, evTime + delay);
}
} }
} }

View File

@ -12,5 +12,16 @@ norGate::norGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
} }
void norGate::evaluate(int evTime) { 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);
}
}
} }

View File

@ -11,8 +11,16 @@ notGate::notGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2)
} }
void notGate::evaluate(int evTime) { void notGate::evaluate(int evTime) {
int val = in1->getValue(); if (in1->getValue(evTime) == 0) {
if (out->getValue(evTime + delay) != 0) {
if(val != -1) { 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);
}
} }
} }

View File

@ -12,5 +12,16 @@ orGate::orGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
} }
void orGate::evaluate(int evTime) { 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);
}
}
} }

View File

@ -3,27 +3,29 @@
wire::wire(int number, bool io, string inName) wire::wire(int number, bool io, string inName)
{ {
wireNumber = number; wireNumber = number;
isInput = io; isPrint = io;
name = inName; name = inName;
value = -1; value = -1;
lastEvent = 0; lastEvent = 0;
history.insert(history.begin(), 60, -1);
} }
int wire::getValue(int time) const 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) void wire::setValue(int newValue, int setTime)
{ {
while (setTime < 60 &&
(history[setTime] == -1 || history[setTime] == history[setTime+1])) {
history[setTime] = newValue; history[setTime] = newValue;
if (lastEvent < setTime) { if (lastEvent < setTime) {
lastEvent = setTime; lastEvent = setTime;
} }
setTime++;
}
} }
int wire::getNumber() const int wire::getNumber() const

View File

@ -21,7 +21,7 @@ class wire {
private: private:
int wireNumber, value, lastEvent; int wireNumber, value, lastEvent;
vector<int> history (60, -1); vector<int> history;
string name; string name;
bool isPrint; bool isPrint;
vector<gate*> gates; vector<gate*> gates;

View File

@ -12,5 +12,18 @@ xnorGate::xnorGate(priority_queue<event> *eQueue, int d, wire* wire1,
} }
void xnorGate::evaluate(int evTime) { 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);
}
}
}
} }

View File

@ -12,5 +12,18 @@ xorGate::xorGate(priority_queue<event> *eQueue, int d, wire* wire1,
} }
void xorGate::evaluate(int evTime) { 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);
}
}
}
} }