I'm writing a couple of classes one for a D Flip Flop and one for a JK Flip Flop in C++. The D Flip Flop seems to be working fine, but I'm having issues with the JK Flip Flop.
I'm getting garbage for the variables when passing from one function to the next. Here is my code for the JK Flip Flop header, class and test:
It's been a while since I've written anything in C++, so it's probably obvious to the experts out there, maybe even to non-experts, but I can't see it and I'm tired of loosing sleep over it, lol. Perhaps it's just my logic, IDK...
Any help would be appreciated.
TMS
I'm getting garbage for the variables when passing from one function to the next. Here is my code for the JK Flip Flop header, class and test:
Code:
#ifndef JKflipFlop_h #define JKflipFlop_h #include <iostream> class JKFlipFlop { public: JKFlipFlop(); JKFlipFlop(int, int, int, int, int); void setJandK(int, int, int, int, int); int JKFlip(int ipJ, int ipK, int ipQ); int enableJK(int inputWire, int ipJ, int ipK, int ipQ); ~JKFlipFlop(); private: int ipJ; int ipK; int ipQ; int ipWireJ; int ipWireK; }; #endif #include "JKflipFlop.h" #include <iostream> using namespace std; JKFlipFlop::JKFlipFlop() { } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ constructor for JKfipFlop() make a copy of J, K and Q for manipulation. ipJ = J, ipK = K, ipQ= Q, ipWire = inputWire ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ JKFlipFlop::JKFlipFlop(int J, int K, int Q, int inputWireA, int inputWireB) { setJandK(J, K, Q, inputWireA, inputWireB); } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function to set ipK, ipK and ipQ make a copy of J,K and Q for manipulation. ipJ = J, ipK = K, ipQ = Q, ipWire = inputWire ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ void JKFlipFlop::setJandK(int J, int K, int Q, int inputWireA, int inputWireB) { ipJ = J; ipK = K; ipQ = Q; ipWireJ = inputWireA; ipWireK = inputWireB; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enable JK is called to start the flipFlop function. If the inputWire is low, JKFlipFlop is not called and Q retains the ame state. If the inputWire is High, JKFlipFlop is called and the wires will be processed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int JKFlipFlop::enableJK(int ipWireA, int ipJ, int ipK, int ipQ) { if(ipWireA == 0) { return ipQ; } else { return JKFlip(ipWireJ, ipWireK, ipQ); } } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JK flip: -if enable is low, Q or Q' will not be changed, and therefore the JKFlipFlop function will not be called. -if enable is high && J and K are low Q and Q' will not be changed. - J is low, K is high Q is set low and Q' is high - J is high and K is low, Q is set to high, Q' is low - J is high and K is high, determine the value of Q and make it opposite. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int JKFlipFlop::JKFlip(int ipJ, int ipK, int ipQ) { cout << "ipJ = " << ipJ; cout << ", ipK = " << ipK ; cout << ", ipQ = " << ipQ << endl; cout << endl; if (ipJ == 0 && ipK == 0) { return ipQ; //ipQ is unchanged } if (ipJ == 0 && ipK == 1) { ipQ = 0; //ipQ is reset cout << "In J low K high" << ipQ << endl; } if (ipJ == 1 && ipK == 0) { ipQ = 1; } if (ipJ == 1 && ipK ==1) { if(ipQ == 0) { ipQ = 1; } else { ipQ = 0; } } return ipQ; } JKFlipFlop::~JKFlipFlop() { } }; #endif #include "JKFlipFlop.h" #include <iostream> using namespace std; int main() { JKFlipFlop FJK; int ipWireA = 0; int ipWireB = 0; int Jtest = 0; int Ktest = 0; int Qtest = 0; int outputWire = 0; ipWireA = 0; Jtest = 0; Ktest = 0; Qtest = 0; outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest); cout << "Result of JK (ipWire 0, J low, K low): " << outputWire << endl; cout << endl; ipWireA = 1; Jtest = 0; Ktest = 0; outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest); cout << "Result of JK (ipWire 1, J low, K low): " << outputWire << endl; cout << endl; ipWireA = 1; Jtest = 0; Ktest = 1; outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest); cout << "Result of JK (ipWire 1, J low, K high): " << outputWire << endl; cout << endl; ipWireA = 1; Jtest = 1; Ktest = 0; outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest); cout << "Result of JK (ipWire 1, J high, K low): " << outputWire << endl; cout << endl; ipWireA = 1; Jtest = 1; Ktest = 1; outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest); cout << "Result of JK (ipWire 1, J = 1, K = 1): " << outputWire << endl; return 0; }
Any help would be appreciated.
TMS
Comment