add static variable for tracking event number
This commit is contained in:
parent
fb0c023cf4
commit
848c471b14
@ -10,14 +10,14 @@ AndGate::AndGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
|
||||
}
|
||||
Event AndGate::evaluate(int evTime) {
|
||||
if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
|
||||
}
|
||||
else if(in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1){
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else{
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
#include "Event.h"
|
||||
|
||||
Event::Event(int num, int value, int setTime, Wire * output){
|
||||
evNum = num;
|
||||
int Event::numOfEvents = 0;
|
||||
|
||||
Event::Event(int value, int setTime, Wire * output){
|
||||
evNum = numOfEvents++;
|
||||
evValue = value;
|
||||
evTime = setTime;
|
||||
out = output;
|
||||
|
@ -11,13 +11,13 @@ NandGate::NandGate(int d, Wire * wire1, Wire * wire2, Wire * wire3)
|
||||
Event NandGate::evaluate(int evTime)
|
||||
{
|
||||
if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) {
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else if (in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1) {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
}
|
||||
else{
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,13 +11,13 @@ NorGate::NorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
|
||||
|
||||
Event NorGate::evaluate(int evTime) {
|
||||
if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
}
|
||||
else if (in1->getValue(evTime) == 0 && in2->getValue(evTime) == 0) {
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else{
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,12 +10,12 @@ NotGate::NotGate(int d, Wire* wire1, Wire* wire2) {
|
||||
|
||||
Event NotGate::evaluate(int evTime) {
|
||||
if (in1->getValue(evTime) == 1) {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
}else if (in1->getValue(evTime) == 0) {
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else {
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,13 +11,13 @@ OrGate::OrGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
|
||||
|
||||
Event OrGate::evaluate(int evTime) {
|
||||
if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) {
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else if (in1->getValue(evTime) == 0 && in2->getValue(evTime) == 0) {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
}
|
||||
else{
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -129,32 +129,30 @@ bool Simulation::parseVector(string fileName) {
|
||||
}
|
||||
}
|
||||
|
||||
e.push(Event(eventNum++, valInt, timeInt, tmpWire));
|
||||
e.push(Event(valInt, timeInt, tmpWire));
|
||||
}
|
||||
}
|
||||
|
||||
void Simulation::simulate() {
|
||||
// loop through event queue
|
||||
while(!e.empty()) {
|
||||
bool doesChange;
|
||||
bool changed;
|
||||
Wire * output;
|
||||
|
||||
Event tmpEvent = e.top();
|
||||
e.pop();
|
||||
|
||||
output = tmpEvent.getOutput();
|
||||
doesChange = output->setValue(tmpEvent.getValue(), tmpEvent.getTime());
|
||||
changed = output->doesChange(tmpEvent.getValue(), tmpEvent.getTime());
|
||||
|
||||
// if the wire value changes, evaluate gates
|
||||
if(doesChange) {
|
||||
if(changed && !(tmpEvent.getTime() > 60)) {
|
||||
Gate * tmpGate;
|
||||
Event gateEvent(-1, -1, -1, nullptr);
|
||||
Event gateEvent(-1, -1, nullptr);
|
||||
int index = 0;
|
||||
while(true){
|
||||
tmpGate = output->getGate(index++);
|
||||
if (tmpGate != nullptr) {
|
||||
gateEvent = tmpGate->evaluate(tmpEvent.getTime());
|
||||
gateEvent.setNum(eventNum++);
|
||||
e.push(gateEvent);
|
||||
}
|
||||
else {
|
||||
@ -162,6 +160,8 @@ void Simulation::simulate() {
|
||||
}
|
||||
}
|
||||
}
|
||||
output->setValue(tmpEvent.getValue(), tmpEvent.getTime());
|
||||
e.pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
10
src/Wire.cpp
10
src/Wire.cpp
@ -39,17 +39,19 @@ char Wire::getChar(int wantedTime) const
|
||||
}
|
||||
}
|
||||
|
||||
bool Wire::setValue(int newValue, int setTime)
|
||||
void Wire::setValue(int newValue, int setTime)
|
||||
{
|
||||
if (getValue(setTime) != newValue) {
|
||||
if (doesChange(newValue, setTime)) {
|
||||
historyTimes.push_back(setTime);
|
||||
historyValues.push_back(newValue);
|
||||
if (lastEvent < setTime) {
|
||||
lastEvent = setTime;
|
||||
}
|
||||
return true; // I changed the value
|
||||
}
|
||||
return false; // Nothing changed
|
||||
}
|
||||
|
||||
bool Wire::doesChange(int newValue, int setTime) {
|
||||
return getValue(setTime) != newValue;
|
||||
}
|
||||
|
||||
void Wire::convertToIO(string newName)
|
||||
|
@ -12,14 +12,14 @@ XnorGate::XnorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
|
||||
Event XnorGate::evaluate(int evTime) {
|
||||
if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) {
|
||||
if (in1->getValue(evTime) == in2->getValue(evTime)) {
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,14 +12,14 @@ XorGate::XorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
|
||||
Event XorGate::evaluate(int evTime) {
|
||||
if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) {
|
||||
if (in1->getValue(evTime) != in2->getValue(evTime)) {
|
||||
return Event(-1, 1, evTime + delay, out);
|
||||
return Event(1, evTime + delay, out);
|
||||
}
|
||||
else {
|
||||
return Event(-1, 0, evTime + delay, out);
|
||||
return Event(0, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return Event(-1, -1, evTime + delay, out);
|
||||
return Event(-1, evTime + delay, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user