3. COUNTER

 

3.1 Description

 

 

graphics24

Drawing 3.1: Counter block diagram

 

 

 

 

 

 

3.2 Creating Module

As we already said, for better designs, our recommendation is not to use the GUI module inserter. Instead of that, create a module in an text editor, rename it to module_name.vhd and insert it into your project.

  • All the steps for creating a module using an text editor are explained in Sub-chapter 2.3.1 Creating a Module Using an Text Editor.
  •  

    Counter VHDL model:

     

    library ieee;

        use ieee.std_logic_1164.all;

        use ieee.std_logic_arith.all;

        use ieee.std_logic_unsigned.all;

     

    entity counter is

        generic(

            cnt_value_g : integer; -- threshold value for counter

            depth_g       : integer – the number of samples in one period of the signal

            );

     

        port(

            clk_in    : in  std_logic; -- input clock signal       

            cnt_en  : in  std_logic; -- counter enable

            cnt_out : out std_logic_vector (depth_g - 1 downto 0)   -- current counter value

            );

    end;

     

    architecture rtl of counter is

     

        signal cnt_out_s : std_logic_vector (depth_g - 1 downto 0) := (others => '0'); -- current counter value

     

    begin

        counter_p: process

        begin

            wait until rising_edge(clk_in);

                if (cnt_en = '1') then

                    if (cnt_out_s = conv_std_logic_vector (cnt_value_g, depth_g)) then

                        cnt_out_s <= (others => '0'); -- counter reset

                    else

                        cnt_out_s <= cnt_out_s + 1;    -- counter

                    end if;

                end if;

        end process;

     

        cnt_out <= cnt_out_s;

    end;

     

     

    1. In the  Hierarchy window, left-click on the module_name (counter – rtl) and then in the Processes window double-click on the Check Syntax option
    1. Note: Please bear in mind that the Implementation option in the Hierarchy window must be selected.
    1. Correct any errors before proceeding

     

     

    3.3 Creating Test Bench

     

     

     

     

     

    We will now create a test bench file for the Counter module (counter_tb.vhd). We will use the same steps as for creating frequency_trigger_rtl.vhd module, explained in Sub-chapter 2.3.1 Creating a Module Using an Text Editor.

     

     

  • Counter test bench:

     

  • library ieee;

        use ieee.std_logic_1164.all;

        use ieee.std_logic_arith.all;

        use ieee.std_logic_unsigned.all;

  •  

        use work.modulator_pkg.all;

     

    After adding counter_tb.vhd file into modulator project, it should appear in the Hierarchy window, under the modulator project. Please bear in mind that the Simulation option in the Design View must be selected, see Illustration 3.1.

     

    graphics141

    Illustration 3.1: counter_tb.vhd file in the Hierarchy window

     

    3.4 Simulating

    After you have entered the code for the input stimulus in order to perform simulation,     follow the next steps:

    1. In the Design window, select Implementation view
    2. In the Hierarchy window, select counter – rtl module, right-click on it and choose Set as Top Module option, see Illustration 3.2
    3.  

      graphics142

      Illustration 3.2: Set as Top Module option

       

    4. In the Set as Top Module dialog box, click Yes, see Illustration 3.3
    5.  

      graphics143

      Illustration 3.3: Set as Top Module dialog box

       

    6. Turn back to the Design window and select Simulation view
    7. If necessary, select Behavioral Simulation from the drop-down list
    8. Select testbench_name (counter_tb.vhd) in the Hierarchy window
    9. In the Processes window, expand ISim Simulator and double-click on the Behavioral Check Syntax
    10. Correct any errors before proceeding
    11. In the Processes window, expand ISim Simulator and then double-click on the Simulate Behavioral Model, see Illustration 3.4
    12.  

       

      graphics5

      Illustration 3.4: Simulate Behavioral Model command

       

    13. Assuming no errors in the ISim command line, your simulation result should look similar to Illustration 3.5

     

    graphics20

    Illustration 3.5: Simulation Results

     

    Note: All the information about creating the Counter Module, generating its test bench and simulating the Counter design, you can also find in the Lab 5: “Creating Counter Module”.