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;
}
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);
}
}
}

View File

@ -9,16 +9,18 @@ nandGate::nandGate(priority_queue<event> *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);
}
}
}

View File

@ -12,5 +12,16 @@ norGate::norGate(priority_queue<event> *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);
}
}
}

View File

@ -11,8 +11,16 @@ notGate::notGate(priority_queue<event> *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);
}
}
}

View File

@ -12,5 +12,16 @@ orGate::orGate(priority_queue<event> *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);
}
}
}

View File

@ -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;
}
}

View File

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

View File

@ -12,5 +12,18 @@ xnorGate::xnorGate(priority_queue<event> *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);
}
}
}
}

View File

@ -12,5 +12,18 @@ xorGate::xorGate(priority_queue<event> *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);
}
}
}
}