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)
{
}
if (ipJ == 1 && ipK ==1)
{
if(ipQ == 0)
{
ipQ = 1;
}
else
{
ipQ = 0;
}
}
return ipQ;
}
JKFlipFlop::~JKFlipFlop()
{
}
#include "flipFlop.h"
#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;
}