Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Last active September 9, 2022 04:35
Show Gist options
  • Save rahulbhadani/9198147cc2a1aca3892685dfdcdd59c5 to your computer and use it in GitHub Desktop.
Save rahulbhadani/9198147cc2a1aca3892685dfdcdd59c5 to your computer and use it in GitHub Desktop.
JKFF : Flipflop and Inverter
#ifndef JKFF_H
#define JKFF_H
#include<systemc>
#include<cstdlib>
#include<cstring>
#include<string>
using namespace std;
using namespace sc_core;
SC_MODULE(inverter)
{
sc_in<bool> in;
sc_out<bool> out;
void invert()
{
while(1)
{
wait();
out.write(! in.read() );
}
}
SC_CTOR(inverter)
{
SC_THREAD(invert);
sensitive << in; // sensitive to changes in the input
}
~inverter(){}
};
SC_MODULE(jkff)
{
sc_in<bool> clk;
sc_in<bool> J;
sc_in<bool> K;
sc_out<bool> Q;
sc_out<bool> Qdash;
/* member variables */
bool b[4];
void flipflop()
{
while(1)
{
wait();
b[1] = J.read();
b[2] = K.read();
cout << "$ "<< name() <<" : " << sc_time_stamp().to_seconds()<<" Q(t) "<< b[0];
/*Transitions*/
if(!b[0])
{
if(!b[1] && !b[2])
b[0] = false;
else if(!b[1] && b[2])
b[0] = false;
else if(b[1] && ~b[2])
b[0] = true;
else if(b[1] && b[2])
b[0] = true;
}
else if(b[0])
{
if(!b[1] && !b[2])
b[0] = true;
else if(!b[1] && b[2])
b[0] = false;
else if(b[1] && ~b[2])
b[0] = true;
else if(b[1] && b[2])
b[0] = false;
}
cout << " J "<< b[1] << " K "<< b[2] << " Q(t+1) "<< b[0]<< endl;
/*Create complementary outputs*/
b[3] = b[0];
Q.write(b[0]);
Qdash.write(!b[3]);
}
}
SC_CTOR(jkff):b{false, false, false, false}
{
SC_THREAD(flipflop);
sensitive << clk.pos() << J << K ;
}
~jkff() {}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment