VHDL coding tips and tricks: VHDL: 4 bit Johnson Counter with Testbench

Monday, September 13, 2010

VHDL: 4 bit Johnson Counter with Testbench

    A Johnson counter is a digital circuit which consists of a series of flip flops connected together in a feedback manner. This is very similar to a ring counter with just one tiny difference. The circuit is special type of shift register where the complement output of the last flipflop is fed back to the input of first flipflop. When the circuit is reset, all the flipflop outputs are made zero. With a n-flipflop Johnson counter we get a MOD-2n counter. That means the counter has 2n different states.

Block Diagram:


The circuit diagram for a 3 bit Johnson counter is shown below:

block diagram for 3 bit johnson counter

     
    The counter goes through the following states over and over when the reset is not applied: 001  -   011  -  111   -   110  -  100  - 000  ...

Johnson Counter:


library ieee;
use ieee.std_logic_1164.all;

entity johnson_counter is
port(clk : in std_logic;
    reset : in std_logic;
    count : out std_logic_vector(3 downto 0)
    );
end johnson_counter;

architecture Behavioral of johnson_counter is

signal temp : std_logic_vector(3 downto 0) := (others => '0');

begin

--assign the temparary signal to output port.
--In VHDL-1997, output ports cannot be read. Thats why we use temp here.
count <= temp; 

process(clk)
begin
if(rising_edge(clk)) then
    if (reset = '1') then ---synchronous reset
        temp <= (others => '0');
    else
        --these are concurrent statements. 
        --which means they all execute at the same time.
        temp(1) <= temp(0);
        temp(2) <= temp(1);
        temp(3) <= temp(2);
        temp(0) <= not temp(3);
    end if;
end if;
end process;
   
end Behavioral;

Testbench for the Johnson Counter:


library ieee;
use ieee.std_logic_1164.all;

--testbench has empty entity
entity tb_johnson_counter is
end entity tb_johnson_counter;

architecture behavioral of tb_johnson_counter is

signal clk,reset : std_logic := '0';
signal count :std_logic_vector(3 downto 0) := "0000";
constant clk_period : time := 10 ns;

begin

--entity instantiation with named association port mapping
counter_uut: entity work.johnson_counter
    port map(clk => clk,
        reset => reset,
        count => count);

--generate clock
Clk_generation: process
begin
    wait for clk_period/2;
    clk <= not clk; --toggle clock when half of clk_period is over
end process;

stimulus: process
begin 
    reset <= '1';  wait for clk_period;
    reset <= '0';   wait for Clk_period*10;
    reset <= '1';   wait for Clk_period;
    reset <= '0';   
    wait;  --testing done. wait endlessly
end process;

end behavioral;

Lets take a look at the simulation waveform from modelsim.

Simulation Waveform from Modelsim:


johnson counter simulation waveform from modelsim vhdl

Post Synthesis Schematic from Xilinx Vivado:


The schematic looks quite simple, few flipflops connected in a sequence, along with some input and output buffers.

technology schematic for 4 bit johnson counter

5 comments:

  1. cud ny1 guide plz y here the word other being used,is it a keyword??

    ReplyDelete
  2. Check in this link:
    http://vhdlguru.blogspot.com/2010/02/arrays-and-records-in-vhdl.html

    ReplyDelete
  3. how to convert ring counter circuit to johnson counter circuit?can you show are circuit?step by step??

    ReplyDelete
  4. signal temp : unsigned(3 downto 0):=(others => '0');


    why other =>'0' is used in this signal statement

    ReplyDelete
  5. can anyone explain the each line meaning for understanding?

    ReplyDelete