VHDL 8 位无符号计数器溢出检测

VHDL 8-bit unsigned counter overflow detection

提问人:dannyph 提问时间:10/16/2023 更新时间:10/16/2023 访问量:84

问:

我正在尝试创建一个 8 位无符号计数器,该计数器的计数步长取决于 3 位控制输入。问题是 RST、控制、加法、减法和下溢可以正常工作,但溢出根本不起作用。在测试台上,当我得到溢出时,溢出值本身似乎并没有像它应该的那样从“0”变为“1”,但它只是从头开始计数。

use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Counter is
    Port ( Clk : in  STD_LOGIC;
           RST : in  STD_LOGIC;
           Control : in  STD_LOGIC_VECTOR (2 downto 0);
           Count : out  STD_LOGIC_VECTOR (7 downto 0);
           Overflow : out  STD_LOGIC;
           Underflow : out  STD_LOGIC;
           Valid : out  STD_LOGIC);
end Counter;

architecture Behavioral of Counter is
    signal tmpCount : UNSIGNED(7 downto 0) := (others => '0');
    signal tmpOverflow : STD_LOGIC := '0';
    signal tmpUnderflow : STD_LOGIC := '0';
    signal tmpValid : STD_LOGIC := '1'; 

begin

    process 
    begin
    

    wait until Clk'EVENT and Clk = '1';
    
    
    if tmpOverflow='1' or tmpUnderflow='1' or RST='1' then
        if RST='1' then 
            tmpCount<=(others=>'0');
            tmpOverflow<='0';
            tmpUnderflow<='0';
            tmpValid<='1';
        end if;
        
        else 
            if Control="000" then
                tmpCount<= tmpCount - 5;
            elsif Control="001" then
                tmpCount<=tmpCount - 2;
            elsif Control="011" then
                tmpCount<=tmpCount + 1;
            elsif Control="100" then
                tmpCount<=tmpCount + 2;
            elsif Control="101" then
                tmpCount<=tmpCount + 5;
            elsif Control="110" then
                tmpCount<=tmpCount + 6;
            elsif Control="111" then
                tmpCount<=tmpCount + 29;
        end if;
                
        if tmpCount > 255 then
            tmpOverflow <= '1';
        elsif tmpCount < 0 then
            tmpUnderflow <= '1';
        end if;

        end if;
        
        if tmpUnderflow='1' or tmpOverflow='1' then
            tmpValid<='0';
            tmpCount<= (others =>'U');
        end if;

    
    end process;
    
    Count<=std_logic_vector(tmpCount);
    Overflow<=tmpOverflow;
    Underflow<=tmpUnderflow;
    Valid<=tmpValid;

end Behavioral;

溢出 VHDL 计数器 未签名

评论

0赞 user16145658 10/16/2023
溢出发生在二进制的补码(有符号)二进制数(不是无符号,可以翻转)中。请注意,对于二进制数值范围为 0 到 255 的信号 tmpCount : UNSIGNED(7 downto 0),tmpCount > 255 不应为 TRUE。提供一个最小的可重现示例以及一个特定问题。
1赞 Harry 10/16/2023
溢出发生在有符号和无符号数字中。维基百科的“整数溢出”文章有一张漂亮的图片,一个(无符号的)里程表从 99999.9 溢出到 0.0。
0赞 Renaud Pacalet 10/16/2023
代码的缩进非常奇怪;第一个应该缩进为第一个,你可能应该解决这个问题。elseif
2赞 Renaud Pacalet 10/16/2023
您的代码存在几个问题(不提及缩进)。1. 您声明为 ,因此它不能小于 0,您的测试将永远不会通过。您将其声明为 8 位长,因此它不能大于 255,并且您的测试将永远不会通过。您显然认为VHDL信号是即时分配的,就像大多数编程语言中的变量一样。它们不是,在用这种编程语言编写任何内容之前,您可能应该阅读 VHDL 书籍。tmpCountunsignedelsif tmpCount < 0 thenif tmpCount > 255 then

答: 暂无答案