Counter with overflow signal at 1001

OP #7001144
Rate this post
• ▲ useful
▼ not useful
I am trying to make a counter that increases by one on every rising edge 
if enable = 1. Additionally, if reset = 1 on a rising edge, the counter 
should be reset. If the counter's current state is 9 (1001), overflow 
should be one.
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4

5
entity BCDCounter is
6

7
port (
8
    clk : in std_logic;
9
    reset : in std_logic;
10
    count : out std_logic_vector(3 downto 0);
11
    enable : in std_logic;
12
    overflow : out std_logic
13
);
14
end BCDCounter;
15

16
architecture rtl of BCDCounter is
17

18
signal countr : unsigned(3 downto 0);
19

20
begin
21
    process(clk)
22
    begin
23
        if(rising_edge(clk)) then
24
            if enable = '1' then
25
                countr <= countr + 1;
26
                if std_logic_vector(countr) = "1001" then
27
                    countr <= (others => '0');
28
                end if;
29
            end if;
30
            if reset = '1' then
31
                countr <= (others => '0');
32
            end if;
33
        end if;
34
        if std_logic_vector(countr) = "1001" then
35
            overflow <= '1';
36
        else
37
            overflow <= '0';
38
        end if;
39
    end process;
40
    count <= std_logic_vector(countr);
41
end architecture rtl;

Upon running this code, I get a delayed overflow output on the switch 
from 9 to 10, and I have no idea why.
1
ENABLED  5 -> 0101 OVERFLOW 0 OK.
2
ENABLED  6 -> 0110 OVERFLOW 0 OK.
3
ENABLED  7 -> 0111 OVERFLOW 0 OK.
4
ENABLED  8 -> 1000 OVERFLOW 0 OK.
5
ENABLED  9 -> 1001 OVERFLOW 0 FALSE!
6
ENABLED  0 -> 0000 OVERFLOW 1 FALSE!
7
RESET to ENABLED  0 -> 0000 OVERFLOW 0 OK.
8
DISABLED 0 -> 0000 OVERFLOW 0 OK.
9
ENABLED  1 -> 0001 OVERFLOW 0 OK.
10
DISABLED 1 -> 0001 OVERFLOW 0 OK.
Moderator (Company: Titel) #7002632
Rate this post
• ▲ useful
▼ not useful
Eric J. wrote:
> I have no idea why.
Congratulations, you found the thing called "latency". This is due to 
the values you are preparing in a synchronous process will happen "later 
on" at the very next active clock edge.

So lets play "synthesizer": you enter the process with number 9 and find 
that the overflow flag must be set.
Additionally in the very same process you find that 9 is enough and set 
0 as the next value.
And of course now the overflow flag will be active together with the 
counters value 0.

Have a look at the simulations waveform and think about what you see and 
I wrote. Sooner or later you will get the trick.

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.