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"
|
||||
|
||||
bool Simulation::parse(string fileName)
|
||||
bool Simulation::parseCircuit(string fileName)
|
||||
{
|
||||
ifstream in;
|
||||
circuit.open(fileName + ".txt");
|
||||
in.open(fileName + ".txt");
|
||||
if (in.fail()) {
|
||||
cerr << endl << fileName << ".txt could not be opened :(";
|
||||
exit(1);
|
||||
@ -16,10 +16,11 @@ bool Simulation::parse(string fileName)
|
||||
getline(in, tmpString);
|
||||
|
||||
while (!in.eof()) {
|
||||
tmpType << in;
|
||||
in >> tmpType;
|
||||
|
||||
in >> tmpString;
|
||||
in >> tmp1;
|
||||
|
||||
tmpString << in;
|
||||
tmp1 << in;
|
||||
if (tmpType == "INPUT") {
|
||||
tmpWire = new wire(tmp1, true, tmpString);
|
||||
wires.push_back(tmpWire);
|
||||
@ -29,49 +30,49 @@ bool Simulation::parse(string fileName)
|
||||
wires.push_back(tmpWire);
|
||||
}
|
||||
else if (tmpType == "NOT") {
|
||||
tmp2 << in;
|
||||
in >> tmp2;
|
||||
tmpGate = new notGate(getDelay(tmpString), findWire(tmp1),
|
||||
findWire(tmp2));
|
||||
gates.push_back(tmpGate);
|
||||
}
|
||||
else if (tmpType == "AND") {
|
||||
tmp2 << in;
|
||||
tmp3 << in;
|
||||
in >> tmp2;
|
||||
in >> tmp3;
|
||||
tmpGate = new andGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||
findWire(tmp3));
|
||||
gates.push_back(tmpGate);
|
||||
}
|
||||
else if (tmpType == "NAND") {
|
||||
tmp2 << in;
|
||||
tmp3 << in;
|
||||
in >> tmp2;
|
||||
in >> tmp3;
|
||||
tmpGate = new nandGate(getDelay(tmpString), findWire(tmp1),
|
||||
findWire(tmp2), findWire(tmp3));
|
||||
gates.push_back(tmpGate);
|
||||
}
|
||||
else if (tmpType == "OR") {
|
||||
tmp2 << in;
|
||||
tmp3 << in;
|
||||
in >> tmp2;
|
||||
in >> tmp3;
|
||||
tmpGate = new orGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||
findWire(tmp3));
|
||||
gates.push_back(tmpGate);
|
||||
}
|
||||
else if (tmpType == "XOR") {
|
||||
tmp2 << in;
|
||||
tmp3 << in;
|
||||
in >> tmp2;
|
||||
in >> tmp3;
|
||||
tmpGate = new xorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||
findWire(tmp3));
|
||||
gates.push_back(tmpGate);
|
||||
}
|
||||
else if (tmpType == "NOR") {
|
||||
tmp2 << in;
|
||||
tmp3 << in;
|
||||
in >> tmp2;
|
||||
in >> tmp3;
|
||||
tmpGate = new norGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||
findWire(tmp3));
|
||||
gates.push_back(tmpGate);
|
||||
}
|
||||
else if (tmpType == "XNOR") {
|
||||
tmp2 << in;
|
||||
tmp3 << in;
|
||||
in >> tmp2;
|
||||
in >> tmp3;
|
||||
tmpGate = new xnorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
||||
findWire(tmp3));
|
||||
gates.push_back(tmpGate);
|
||||
@ -86,3 +87,17 @@ void Simulation::simulate()
|
||||
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 {
|
||||
public:
|
||||
bool parse(string fileName);
|
||||
bool parseCircuit(string fileName);
|
||||
void simulate();
|
||||
void print();
|
||||
private:
|
||||
wire* findWire(int n);
|
||||
int getDelay(string d);
|
||||
|
||||
priority_queue<event> e;
|
||||
vector<gate*> gates;
|
||||
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,
|
||||
wire* wire3);
|
||||
void evaluate(int evTime);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#endif // !AND
|
||||
|
@ -23,4 +23,9 @@ void nandGate::evaluate(int evTime)
|
||||
out->setValue(0, evTime + delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int nandGate::getDelay()
|
||||
{
|
||||
return delay;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ class nandGate : public gate {
|
||||
public:
|
||||
nandGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2, wire* wire3);
|
||||
void evaluate(int time);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#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,
|
||||
wire* wire3);
|
||||
void evaluate(int evTime);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#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:
|
||||
notGate(priority_queue<event> *eQueue, int d, wire* wire1, wire* wire2);
|
||||
void evaluate(int evTime);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#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,
|
||||
wire* wire3);
|
||||
void evaluate(int evTime);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#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,
|
||||
wire* wire3);
|
||||
void evaluate(int evTime);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#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,
|
||||
wire* wire3);
|
||||
void evaluate(int evTime);
|
||||
int getDelay();
|
||||
};
|
||||
|
||||
#endif // !XOR
|
||||
|
Loading…
Reference in New Issue
Block a user