Add the getter of delay in the gate classes. Add the findeWire and get delay functions for the simulation class. Fix the order of the input to: "in >> data" from "data << in".
This commit is contained in:
parent
959bca93c4
commit
13d85a4651
@ -1,9 +1,9 @@
|
|||||||
#include "Simulation.h"
|
#include "Simulation.h"
|
||||||
|
|
||||||
bool Simulation::parse(string fileName)
|
bool Simulation::parseCircuit(string fileName)
|
||||||
{
|
{
|
||||||
ifstream in;
|
ifstream in;
|
||||||
circuit.open(fileName + ".txt");
|
in.open(fileName + ".txt");
|
||||||
if (in.fail()) {
|
if (in.fail()) {
|
||||||
cerr << endl << fileName << ".txt could not be opened :(";
|
cerr << endl << fileName << ".txt could not be opened :(";
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -16,10 +16,11 @@ bool Simulation::parse(string fileName)
|
|||||||
getline(in, tmpString);
|
getline(in, tmpString);
|
||||||
|
|
||||||
while (!in.eof()) {
|
while (!in.eof()) {
|
||||||
tmpType << in;
|
in >> tmpType;
|
||||||
|
|
||||||
|
in >> tmpString;
|
||||||
|
in >> tmp1;
|
||||||
|
|
||||||
tmpString << in;
|
|
||||||
tmp1 << in;
|
|
||||||
if (tmpType == "INPUT") {
|
if (tmpType == "INPUT") {
|
||||||
tmpWire = new wire(tmp1, true, tmpString);
|
tmpWire = new wire(tmp1, true, tmpString);
|
||||||
wires.push_back(tmpWire);
|
wires.push_back(tmpWire);
|
||||||
@ -29,49 +30,49 @@ bool Simulation::parse(string fileName)
|
|||||||
wires.push_back(tmpWire);
|
wires.push_back(tmpWire);
|
||||||
}
|
}
|
||||||
else if (tmpType == "NOT") {
|
else if (tmpType == "NOT") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmpGate = new notGate(getDelay(tmpString), findWire(tmp1),
|
tmpGate = new notGate(getDelay(tmpString), findWire(tmp1),
|
||||||
findWire(tmp2));
|
findWire(tmp2));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
}
|
}
|
||||||
else if (tmpType == "AND") {
|
else if (tmpType == "AND") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmp3 << in;
|
in >> tmp3;
|
||||||
tmpGate = new andGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
tmpGate = new andGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||||
findWire(tmp3));
|
findWire(tmp3));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
}
|
}
|
||||||
else if (tmpType == "NAND") {
|
else if (tmpType == "NAND") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmp3 << in;
|
in >> tmp3;
|
||||||
tmpGate = new nandGate(getDelay(tmpString), findWire(tmp1),
|
tmpGate = new nandGate(getDelay(tmpString), findWire(tmp1),
|
||||||
findWire(tmp2), findWire(tmp3));
|
findWire(tmp2), findWire(tmp3));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
}
|
}
|
||||||
else if (tmpType == "OR") {
|
else if (tmpType == "OR") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmp3 << in;
|
in >> tmp3;
|
||||||
tmpGate = new orGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
tmpGate = new orGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||||
findWire(tmp3));
|
findWire(tmp3));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
}
|
}
|
||||||
else if (tmpType == "XOR") {
|
else if (tmpType == "XOR") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmp3 << in;
|
in >> tmp3;
|
||||||
tmpGate = new xorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
tmpGate = new xorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||||
findWire(tmp3));
|
findWire(tmp3));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
}
|
}
|
||||||
else if (tmpType == "NOR") {
|
else if (tmpType == "NOR") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmp3 << in;
|
in >> tmp3;
|
||||||
tmpGate = new norGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
tmpGate = new norGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||||
findWire(tmp3));
|
findWire(tmp3));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
}
|
}
|
||||||
else if (tmpType == "XNOR") {
|
else if (tmpType == "XNOR") {
|
||||||
tmp2 << in;
|
in >> tmp2;
|
||||||
tmp3 << in;
|
in >> tmp3;
|
||||||
tmpGate = new xnorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
tmpGate = new xnorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||||
findWire(tmp3));
|
findWire(tmp3));
|
||||||
gates.push_back(tmpGate);
|
gates.push_back(tmpGate);
|
||||||
@ -86,3 +87,17 @@ void Simulation::simulate()
|
|||||||
void Simulation::print()
|
void Simulation::print()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wire * Simulation::findWire(int n)
|
||||||
|
{
|
||||||
|
for (auto i = wires.begin(); i != wires.end(); ++i) {
|
||||||
|
if (n == (**i).getNumber()) return *i;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Simulation::getDelay(string d)
|
||||||
|
{
|
||||||
|
d.resize(d.size() - 2);
|
||||||
|
return atoi(d.c_str());
|
||||||
|
}
|
||||||
|
@ -20,10 +20,13 @@ using namespace std;
|
|||||||
|
|
||||||
class Simulation {
|
class Simulation {
|
||||||
public:
|
public:
|
||||||
bool parse(string fileName);
|
bool parseCircuit(string fileName);
|
||||||
void simulate();
|
void simulate();
|
||||||
void print();
|
void print();
|
||||||
private:
|
private:
|
||||||
|
wire* findWire(int n);
|
||||||
|
int getDelay(string d);
|
||||||
|
|
||||||
priority_queue<event> e;
|
priority_queue<event> e;
|
||||||
vector<gate*> gates;
|
vector<gate*> gates;
|
||||||
vector<wire*> wires;
|
vector<wire*> wires;
|
||||||
|
@ -24,3 +24,8 @@ int andGate::evaluate(int evTime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int andGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class andGate : public gate {
|
|||||||
andGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
andGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
||||||
wire* wire3);
|
wire* wire3);
|
||||||
void evaluate(int evTime);
|
void evaluate(int evTime);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !AND
|
#endif // !AND
|
||||||
|
@ -24,3 +24,8 @@ void nandGate::evaluate(int evTime)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int nandGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -10,6 +10,7 @@ class nandGate : public gate {
|
|||||||
public:
|
public:
|
||||||
nandGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2, wire* wire3);
|
nandGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2, wire* wire3);
|
||||||
void evaluate(int time);
|
void evaluate(int time);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !NAND
|
#endif // !NAND
|
||||||
|
@ -25,3 +25,8 @@ void norGate::evaluate(int evTime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int norGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class norGate : public gate {
|
|||||||
norGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
norGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
||||||
wire* wire3);
|
wire* wire3);
|
||||||
void evaluate(int evTime);
|
void evaluate(int evTime);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !NOR
|
#endif // !NOR
|
||||||
|
@ -24,3 +24,8 @@ void notGate::evaluate(int evTime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int notGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@ class notGate : public gate {
|
|||||||
public:
|
public:
|
||||||
notGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2);
|
notGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2);
|
||||||
void evaluate(int evTime);
|
void evaluate(int evTime);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !NOT
|
#endif // !NOT
|
||||||
|
@ -25,3 +25,8 @@ void orGate::evaluate(int evTime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int orGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class orGate : public gate {
|
|||||||
orGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
orGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
||||||
wire* wire3);
|
wire* wire3);
|
||||||
void evaluate(int evTime);
|
void evaluate(int evTime);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !OR
|
#endif // !OR
|
||||||
|
@ -27,3 +27,8 @@ void xnorGate::evaluate(int evTime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int xnorGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class xnorGate : public gate {
|
|||||||
xnorGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
xnorGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
||||||
wire* wire3);
|
wire* wire3);
|
||||||
void evaluate(int evTime);
|
void evaluate(int evTime);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !XNOR
|
#endif // !XNOR
|
||||||
|
@ -27,3 +27,8 @@ void xorGate::evaluate(int evTime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int xorGate::getDelay()
|
||||||
|
{
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class xorGate : public gate {
|
|||||||
xorGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
xorGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2,
|
||||||
wire* wire3);
|
wire* wire3);
|
||||||
void evaluate(int evTime);
|
void evaluate(int evTime);
|
||||||
|
int getDelay();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !XOR
|
#endif // !XOR
|
||||||
|
Loading…
Reference in New Issue
Block a user