- A Logic Circuit Simulation Library in C++





(libLCS Example) A 4-bit shift register using D-flipflops


In this example, we will build a 4-bit shift register using the off-the-shelf D-flipflops provided in libLCS. We will make use of one of the counter output bits from this example as the input bit stream for our shift register. The complete circuit diagram is as shown below.

four_bit_shiftregister.jpg

The following is the program to simulate the above circuit.

#include <iostream>
#include <lcs/bus.h>
#include <lcs/not.h>
#include <lcs/clock.h>
#include <lcs/simul.h>
#include <lcs/changeMonitor.h>
#include <lcs/dflipflop.h>

using namespace lcs;
using namespace std;

int main(void)
{
    Bus<> q0(0), q1(0), q2(0), q3(0), d0, d1, d2, d3, sr0(0), sr1(0), sr2(0), sr3(0), rst(0);
    Clock clk = Clock::getClock();

    Not<> n1(d0, q0), n2(d1, q1), n3(d2, q2), n4(d3, q3);

    // Initialise 4 positive edge-triggered D-flipflops
    // using the default template parameters. These flipflops
    // will form our counter circuit.
    DFlipFlop<> ff1(q0, d0, clk, rst), ff2(q1, d1, q0, rst), 
                    ff3(q2, d2, q1, rst), ff4(q3, d3, q2, rst);

    // Initialise the D-flipflop modules of the shift register.
    // They are also initialised with the default template
    // parameters.
    DFlipFlop<> ff5(sr0, d1, clk, rst), ff6(sr1, sr0, clk, rst),
                ff7(sr2, sr1, clk, rst), ff8(sr3, sr2, clk, rst);

    ChangeMonitor<4> count((d0,d1,d2,d3), string("Count"), DUMP_ON);
    ChangeMonitor<4> sr((sr0,sr1,sr2,sr3), string("Register"), DUMP_ON);

    Simulation::setStopTime(4000);
    Simulation::start();

    return 0;
}

When the above code is compiled and run, the following output is obtained.

At time: 0,     Count: 1111
At time: 100,   Count: 0000
At time: 100,   Shift Register: 0001
At time: 300,   Count: 0001
At time: 300,   Shift Register: 0010
At time: 500,   Count: 0010
At time: 500,   Shift Register: 0100
At time: 700,   Count: 0011
At time: 700,   Shift Register: 1001
At time: 900,   Count: 0100
At time: 900,   Shift Register: 0011
At time: 1100,  Count: 0101
At time: 1100,  Shift Register: 0110
At time: 1300,  Count: 0110
At time: 1300,  Shift Register: 1100
At time: 1500,  Count: 0111
At time: 1500,  Shift Register: 1001
At time: 1700,  Count: 1000
At time: 1700,  Shift Register: 0011
At time: 1900,  Count: 1001
At time: 1900,  Shift Register: 0110
At time: 2100,  Count: 1010
At time: 2100,  Shift Register: 1100
At time: 2300,  Count: 1011
At time: 2300,  Shift Register: 1001
At time: 2500,  Count: 1100
At time: 2500,  Shift Register: 0011
At time: 2700,  Count: 1101
At time: 2700,  Shift Register: 0110
At time: 2900,  Count: 1110
At time: 2900,  Shift Register: 1100
At time: 3100,  Count: 1111
At time: 3100,  Shift Register: 1001
At time: 3300,  Count: 0000
At time: 3300,  Shift Register: 0011
At time: 3500,  Count: 0001
At time: 3500,  Shift Register: 0110
At time: 3700,  Count: 0010
At time: 3700,  Shift Register: 1100
At time: 3900,  Count: 0011
At time: 3900,  Shift Register: 1001

Below is the screenshot of the gtkwave plot of the generated VCD file.

4_bit_shiftregister_using_dff.jpg

Copyright © 2006, 2007 Siva Chandra