Counters

The counters are responsible for… counting (badum tsss). And, they do it properly…. I think !

Counters

counter32

counter.vhd

counter32 module

Imports

Imports 2 elements

  • IEEE

  • ieee std_logic_1164 numeric_std

Parameters

Parameters 2 elements

• RESET Type : INTEGER Default : 0

! @brief Configure the value exposed by the counter on reset state.

• INCREMENT Type : INTEGER Default : 1

! @brief Configure the value that is added on each iteration cycle.

Ports

Ports list 6 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clock

Input

! @brief clock enable from the core clock controller. Used to not create two clock domains from the master clock and the auxilliary clock.

STD_LOGIC

-

Combinatorial

-

Fanout : 0

clock_en

Input

STD_LOGIC

-

Register clock

-

Fanout : 0

nRST

Input

! @brief Enable input. When set to '1', the counter is authorized to count.

STD_LOGIC

-

Combinatorial

Negative

Fanout : 0

enable

Input

STD_LOGIC

-

Register clock

-

Fanout : 0

valueH

Output

! @brief Lower 32 bits of the value.

STD_LOGIC_VECTOR

[31 : 0]

Combinatorial

-

Fanout : 0

valueL

Output

STD_LOGIC_VECTOR

[31 : 0]

Combinatorial

-

Fanout : 0

Processes

Process list 1 element

• P1 Clocked Clocked by clock Reset by nRST

======================================================================================== ! @brief P1 handle the incrementation of the counter.

Signals

Signals list 1 element

Name

Description

Type

Size

Kind

Fanout

value

unsigned

[63 : 0]

Register clock nRST

Fanout : 0

Assignments

Assignment list 2 elements

• valueH(31 DOWNTO 0) Combinatorial

static assignements

<br>Source : value

• valueL(31 DOWNTO 0) Combinatorial

Source : value

Instantiation templates

Instantiation templates 2 elements

counter32 <name> #(
    .RESET     ( 0),
    .INCREMENT ( 1)

)(
    .clock    (i_clock),
    .clock_en (i_clock_en),
    .nRST     (i_nRST),
    .enable   (i_enable),
    .valueH   (i_valueH),
    .valueL   (i_valueL)
);
<name>: entity work.counter32
    generic map (
        RESET      =>  0,
        INCREMENT  =>  1
    )
   port map (
        clock     => i_clock,
        clock_en  => i_clock_en,
        nRST      => i_nRST,
        enable    => i_enable,
        valueH    => i_valueH,
        valueL    => i_valueL
    );

Source

Raw source Full file

--! @file src/core/core.vhd
--! @brief The base file that assemble all of the components of the core. Does not include any form of memory.
--! @author l.heywang <leonard.heywang@proton.me>
--! @date 05-10-2025

LIBRARY IEEE;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;

ENTITY counter32 IS
    GENERIC (
        --! @brief Configure the  value exposed by the counter on reset state.
        RESET : INTEGER := 0;
        --! @brief Configure the value that is added on each iteration cycle.
        INCREMENT : INTEGER := 1
    );
    PORT (
        --------------------------------------------------------------------------------------------------------
        -- Clocks
        --------------------------------------------------------------------------------------------------------
        --! @brief clock input of the core. Must match the INPUT_FREQ generics within some tolerance.
        clock : IN STD_LOGIC;
        --! @brief clock enable from the core clock controller. Used to not create two clock domains from the master clock and the auxilliary clock.
        clock_en : IN STD_LOGIC;

        --------------------------------------------------------------------------------------------------------
        -- Control signals
        --------------------------------------------------------------------------------------------------------
        --! @brief reset input, active low. When held to '0', the system will remain in the reset state until set to '1'.
        nRST : IN STD_LOGIC;
        --! @brief Enable input. When set to '1', the counter is authorized to count.
        enable : IN STD_LOGIC;

        --------------------------------------------------------------------------------------------------------
        -- IO ports
        --------------------------------------------------------------------------------------------------------
        --! @brief Higher 32 bits of the value.
        valueH : OUT STD_LOGIC_VECTOR(31 DOWNTO 0);
        --! @brief Lower 32 bits of the value.
        valueL : OUT STD_LOGIC_VECTOR(31 DOWNTO 0)

    );
END ENTITY counter32;

ARCHITECTURE rtl OF counter32 IS

    SIGNAL value : unsigned(63 DOWNTO 0) := (OTHERS => '0');

BEGIN

    --========================================================================================
    --! @brief P1 handle the incrementation of the counter.
    --========================================================================================
    P1 : PROCESS (clock, nRST)
    BEGIN

        IF (nRST = '0') THEN
            value <= to_unsigned(RESET, value'length);

        ELSIF rising_edge(clock) AND (clock_en = '1') THEN

            IF (enable = '1') THEN
                value <= value + INCREMENT;
            END IF;

        END IF;

    END PROCESS;

    -- static assignements
    valueH(31 DOWNTO 0) <= STD_LOGIC_VECTOR(value(63 DOWNTO 32));
    valueL(31 DOWNTO 0) <= STD_LOGIC_VECTOR(value(31 DOWNTO 0));

END ARCHITECTURE rtl;

counter_tb

counter_tb.vhd

counter_tb testbench


Imports

Imports 2 elements

  • IEEE std_logic_1164 numeric_std

Signals

Signals list 7 elements

Name

Description

Type

Size

Kind

Fanout

value_t

STD_LOGIC_VECTOR

[63 : 0]

Combinatorial

Fanout : 0

valueH_t

STD_LOGIC_VECTOR

[31 : 0]

Combinatorial

Fanout : 0

valueL_t

STD_LOGIC_VECTOR

[31 : 0]

Combinatorial

Fanout : 0

nRST_t

STD_LOGIC

-

Combinatorial

Fanout : 0

enable_t

STD_LOGIC

-

Combinatorial

Fanout : 0

clock 2 elements

Name

Description

Type

Size

Kind

Fanout

clock_t

STD_LOGIC

-

Combinatorial

Fanout : 0

clock_en_t

STD_LOGIC

-

Register clk rst_n

Fanout : 0

Modules

Modules 2 elements

• clock : U0

Connections :

• counter32 : U1

Processes

Process list 3 elements

• P1

-

• P2

-

• P3

-

Source

Raw source Full file

-- Recommended sim lenght : 1 us

LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;

ENTITY counter_tb IS
END ENTITY;

ARCHITECTURE behavioral OF counter_tb IS

    SIGNAL value_t : STD_LOGIC_VECTOR(63 DOWNTO 0) := (OTHERS => '0');
    SIGNAL valueH_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
    SIGNAL valueL_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
    SIGNAL clock_t : STD_LOGIC := '0';
    SIGNAL clock_en_t : STD_LOGIC := '0';
    SIGNAL nRST_t : STD_LOGIC := '0';
    SIGNAL enable_t : STD_LOGIC := '0';

BEGIN

    U0 : ENTITY work.clock(behavioral)
        PORT MAP(
            clk => clock_t,
            clk_en => clock_en_t,
            nRST => nRST_t
        );

    U1 : ENTITY work.counter32(rtl)
        GENERIC MAP(
            RESET => 2147483392,
            INCREMENT => 65535
        )
        PORT MAP(
            valueL => valueL_t,
            valueH => valueH_t,
            clock => clock_t,
            clock_en => clock_en_t,
            nRST => nRST_t,
            enable => enable_t
        );

    P1 : PROCESS
    BEGIN
        clock_t <= NOT clock_t;
        WAIT FOR 5 ns;
    END PROCESS;

    P2 : PROCESS
    BEGIN
        nRST_t <= '0';
        WAIT FOR 14 ns;
        nRST_t <= '1';
        WAIT;
    END PROCESS;

    P3 : PROCESS
    BEGIN
        WAIT FOR 50 ns;
        enable_t <= '1';
        WAIT;
    END PROCESS;

    value_t <= valueH_t & valueL_t;

END ARCHITECTURE;

Program counter

pcounter_vhd

pcounter.vhd

pcounter_vhd module

Imports

Imports 2 elements

  • IEEE std_logic_1164 numeric_std

Parameters

Parameters 3 elements

• XLEN Type : INTEGER Default : 32

! @brief Configure the data width in the core.

• RESET_ADDR Type : INTEGER Default : 0

! @brief Configure the address exposed by the counter on startup. Must match the linker script file provided.

• INCREMENT Type : INTEGER Default : 4

! @brief Configure the value that is added on each iteration cycle. This enable the ability to modify the memory alignement used.

Ports

Ports list 8 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

address

Output

! @brief Address input, this value is copied synchronously into the counter when load = '1'

STD_LOGIC_VECTOR

[(XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

address_in

Input

STD_LOGIC_VECTOR

[(XLEN - 1) : 0]

Register clock

-

Fanout : 0

nOVER

Output

STD_LOGIC

-

Combinatorial

-

Fanout : 0

clock

Input

! @brief clock enable from the core clock controller. Used to not create two clock domains from the master clock and the auxilliary clock.

STD_LOGIC

-

Combinatorial

-

Fanout : 0

clock_en

Input

STD_LOGIC

-

Register clock

-

Fanout : 0

nRST

Input

! @brief Load enable input. When set to '1', the counting is disabled and the value of address_in is set into the counter.

STD_LOGIC

-

Combinatorial

Negative

Fanout : 0

load

Input

! @brief Enable input. When set to '1', the counter is authorized to count.

STD_LOGIC

-

Register clock

-

Fanout : 0

enable

Input

STD_LOGIC

-

Register clock

-

Fanout : 0

Processes

Process list 1 element

• P1 Clocked Clocked by clock Reset by nRST

-

Signals

Signals list 3 elements

Name

Description

Type

Size

Kind

Fanout

address_maxval

! @brief Internal value for the maximal address, computed on the runtime.

unsigned

[(XLEN - 1) : 0]

Register clock nRST

Fanout : 0

internal 2 elements

Name

Description

Type

Size

Kind

Fanout

internal_address

! @brief Internal counter for the address value, as an unsigned of XLEN bits.

unsigned

[(XLEN - 1) : 0]

Register clock nRST

Fanout : 0

internal_nOVER

! @brief Internal overflow status. Used to block the future evolutions of the counter.

STD_LOGIC

-

Register clock nRST

Fanout : 0

Assignments

Assignment list 2 elements

• address Combinatorial

Output assignement, in any time.

<br>Source : internal_address

• nOVER Combinatorial

Source : internal_nover

Instantiation templates

Instantiation templates 2 elements

pcounter_vhd <name> #(
    .XLEN       ( 32),
    .RESET_ADDR ( 0),
    .INCREMENT  ( 4)

)(
    .address    (i_address),
    .address_in (i_address_in),
    .nOVER      (i_nOVER),
    .clock      (i_clock),
    .clock_en   (i_clock_en),
    .nRST       (i_nRST),
    .load       (i_load),
    .enable     (i_enable)
);
<name>: entity work.pcounter_vhd
    generic map (
        XLEN        =>  32,
        RESET_ADDR  =>  0,
        INCREMENT   =>  4
    )
   port map (
        address     => i_address,
        address_in  => i_address_in,
        nOVER       => i_nOVER,
        clock       => i_clock,
        clock_en    => i_clock_en,
        nRST        => i_nRST,
        load        => i_load,
        enable      => i_enable
    );

Source

Raw source Full file

--! @file src/core/core.vhd
--! @brief The base file that assemble all of the components of the core. Does not include any form of memory.
--! @author l.heywang <leonard.heywang@proton.me>
--! @date 05-10-2025

LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;

ENTITY pcounter_vhd IS
    GENERIC (
        --! @brief Configure the data width in the core.
        XLEN : INTEGER := 32;
        --! @brief Configure the address exposed by the counter on startup. Must match the linker script file provided.
        RESET_ADDR : INTEGER := 0;
        --! @brief Configure the value that is added on each iteration cycle. This enable the ability to modify the memory alignement used.
        INCREMENT : INTEGER := 4
    );
    PORT (
        --------------------------------------------------------------------------------------------------------
        -- IO ports
        --------------------------------------------------------------------------------------------------------
        --! @brief Address output, to be sent to the instruction memory.
        address : OUT STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
        --! @brief Address input, this value is copied synchronously into the counter when load = '1'
        address_in : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);

        --------------------------------------------------------------------------------------------------------
        -- Status
        --------------------------------------------------------------------------------------------------------
        --! @brief Overflow flag output. Asserted when the address is greater than 4 294 967 288.
        nOVER : OUT STD_LOGIC;

        --------------------------------------------------------------------------------------------------------
        -- Clocks
        --------------------------------------------------------------------------------------------------------
        --! @brief clock input of the core. Must match the INPUT_FREQ generics within some tolerance.
        clock : IN STD_LOGIC;
        --! @brief clock enable from the core clock controller. Used to not create two clock domains from the master clock and the auxilliary clock.
        clock_en : IN STD_LOGIC;

        --------------------------------------------------------------------------------------------------------
        -- Control signals
        --------------------------------------------------------------------------------------------------------
        --! @brief reset input, active low. When held to '0', the system will remain in the reset state until set to '1'.
        nRST : IN STD_LOGIC;
        --! @brief Load enable input. When set to '1', the counting is disabled and the value of address_in is set into the counter.
        load : IN STD_LOGIC;
        --! @brief Enable input. When set to '1', the counter is authorized to count.
        enable : IN STD_LOGIC
    );
END ENTITY;

ARCHITECTURE behavioral OF pcounter_vhd IS

    --! @brief Internal counter for the address value, as an unsigned of XLEN bits.
    SIGNAL internal_address : unsigned((XLEN - 1) DOWNTO 0);
    --! @brief Internal value for the maximal address, computed on the runtime.
    SIGNAL address_maxval : unsigned((XLEN - 1) DOWNTO 0) := (OTHERS => '1');
    --! @brief Internal overflow status. Used to block the future evolutions of the counter.
    SIGNAL internal_nOVER : STD_LOGIC;

BEGIN

    --========================================================================================
    --! @brief P1 handle any operations of the program counter.
    --! @details
    --! On rising edges of the clocks, if authorized, the counter is increment by INCREMENT (typ. 4).
    --! If the load command is active, the counter is NOT incremented and the value is copied into the counter.
    --! This effect is shown immediately on the output, since the assignement is done asynchrounsly.
    --! When the value is greater than 0xFFFFFFF8, the overflow is asserted. This enable the ability to handle it
    --! within the core_controller. 
    --========================================================================================
    P1 : PROCESS (clock, nRST)
    BEGIN
        IF (nRST = '0') THEN
            internal_address <= to_unsigned(RESET_ADDR, internal_address'length);
            address_maxval <= (OTHERS => '1');
            address_maxval(2 DOWNTO 0) <= (OTHERS => '0');
            internal_nOVER <= '0';

        ELSIF rising_edge(clock) AND (clock_en = '1') THEN

            IF (load = '1') THEN
                internal_address <= unsigned(address_in);
                internal_nOVER <= '0';

            ELSIF (enable = '1') AND (internal_nOVER = '0') THEN
                internal_address <= internal_address + INCREMENT;

                IF (internal_address >= address_maxval) THEN
                    internal_nOVER <= '1';
                END IF;

            END IF;

        END IF;

    END PROCESS;

    -- Output assignement, in any time.
    address <= STD_LOGIC_VECTOR(internal_address);
    nOVER <= internal_nOVER;

END ARCHITECTURE;

pcounter_tb

pcounter_tb.vhd

pcounter_tb testbench


Imports

Imports 2 elements

  • IEEE std_logic_1164 numeric_std

Signals

Signals list 7 elements

Name

Description

Type

Size

Kind

Fanout

clock_t

STD_LOGIC

-

Combinatorial

Fanout : 0

nRST_t

STD_LOGIC

-

Combinatorial

Fanout : 0

load_t

STD_LOGIC

-

Combinatorial

Fanout : 0

enable_t

STD_LOGIC

-

Combinatorial

Fanout : 0

nOVER_t

STD_LOGIC

-

Combinatorial

Fanout : 0

address 2 elements

Name

Description

Type

Size

Kind

Fanout

address_t

STD_LOGIC_VECTOR

[31 : 0]

Register clk rst_n

Fanout : 0

address_in_t

STD_LOGIC_VECTOR

[31 : 0]

Combinatorial

Fanout : 0

Modules

Modules 1 element

• pcounter : U1

Processes

Process list 3 elements

• P1

-

• P2

-

• P3

-

Source

Raw source Full file

-- Recommended sim lenght : 1 us

LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;

ENTITY pcounter_tb IS
END ENTITY;

ARCHITECTURE behavioral OF pcounter_tb IS

    SIGNAL address_t : STD_LOGIC_VECTOR(31 DOWNTO 0);
    SIGNAL address_in_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := X"deadbeef";
    SIGNAL clock_t : STD_LOGIC := '0';
    SIGNAL nRST_t : STD_LOGIC := '0';
    SIGNAL load_t : STD_LOGIC := '0';
    SIGNAL enable_t : STD_LOGIC := '0';
    SIGNAL nOVER_t : STD_LOGIC;

BEGIN

    U1 : ENTITY work.pcounter(behavioral)
        GENERIC MAP(
            XLEN => 32,
            RESET_ADDR => 255
        )
        PORT MAP(
            address => address_t,
            address_in => address_in_t,
            clock => clock_t,
            nRST => nRST_t,
            load => load_t,
            enable => enable_t,
            nOVER => nOVER_t
        );

    P1 : PROCESS
    BEGIN
        clock_t <= NOT clock_t;
        WAIT FOR 10 ns;
    END PROCESS;

    P2 : PROCESS
    BEGIN
        nRST_t <= '0';
        WAIT FOR 100 ns;
        nRST_t <= '1';
        WAIT FOR 900 ns;
    END PROCESS;

    P3 : PROCESS
    BEGIN
        WAIT FOR 110 ns;
        enable_t <= '1';
        WAIT FOR 90 ns;
        enable_t <= '0';
        load_t <= '1';

        WAIT FOR 60 ns;
        load_t <= '0';
        WAIT FOR 40 ns;
        enable_t <= '1';

        WAIT FOR 100 ns;
        address_in_t <= X"FFFFFFF0";
        load_t <= '1';

        WAIT FOR 20 ns;
        load_t <= '0';
        WAIT FOR 380 ns;
        load_t <= '1';

        WAIT FOR 60 ns;
        load_t <= '0';
        WAIT FOR 140 ns;
    END PROCESS;

END ARCHITECTURE;