VHDL向上/向下计数器错误计数

VHDL向上/向下计数器错误计数,vhdl,Vhdl,我想做一个计数器,它先数到3,然后再倒数到0等等。。 示例:01123210123210 我所做的: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity Counter is port( Clock: in std_logic; Reset: in std_logic; Output:

我想做一个计数器,它先数到3,然后再倒数到0等等。。 示例:01123210123210

我所做的:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity Counter is
   port(
     Clock: in std_logic;
     Reset: in std_logic;
     Output: out std_logic_vector(0 to 1 ));
end Counter;

architecture Behavioral of Counter is
   signal temp: std_logic_vector(0 to 1);
   signal down: std_logic := '0';
begin   process(Clock,Reset)
   begin
      if Reset='0' then
         temp <= "00";
         down<= '0';
      elsif(rising_edge(Clock)) then
            if temp="11" then
                down<= '1';
            elsif temp="00" then
                down<='0';
            end if;

            if down='0' then
                temp <= temp +1;
            else 
                temp <= temp-1;
            end if;

      end if;
   end process;
   Output <= temp;
end Behavioral;
IEEE库;
使用IEEE.STD_LOGIC_1164.ALL;
使用IEEE.STD_LOGIC_ARITH.ALL;
使用IEEE.STD_LOGIC_UNSIGNED.ALL;
实体计数器为
港口(
时钟:标准逻辑;
复位:在标准逻辑中;
输出:输出标准逻辑向量(0到1);
末端计数器;
计数器的结构是
信号温度:标准逻辑向量(0到1);
信号下降:标准逻辑:='0';
开始处理(时钟、复位)
开始
如果Reset='0',则

temp您没有查看所有的信号:查看
向下
以查看发生了什么。由于您使用的是时钟/同步逻辑,
down
在检测到
temp
的时钟周期内发生变化3,因此它将在一个时钟周期后生效。也就是说,当
temp
为3时,
down
仍将为0,因此(3+1)mod 4=0

一个可能的解决方案是提前一步:将
向下更改
一个时钟周期。。。当
temp
=2时

另一个问题是,您正在将非标准化包
STD_LOGIC_ARITH
STD_LOGIC_UNSIGNED
与反向逻辑数组相结合。这可能会产生不可预测的结果。请使用标准包装。例如:

library ieee;
use ieee.STD_LOGIC_1164.ALL;

entity counter is
    port(
        clk    : in  std_logic;
        rst_n  : in  std_logic;
        output : out std_logic_vector(1 downto 0)
    );
end entity;

architecture behavioral of counter is
    use ieee.numeric_std.ALL;
    signal temp : unsigned(output'range) := (others => '0');
    signal down : std_logic := '0';
begin
    process(clk, rst_n)
    begin
        if rst_n = '0' then -- why asynchronous reset??
            temp <= (others => '0');
            down <= '0';
        elsif(rising_edge(clk)) then
            if temp = 2 then
                down <= '1';
            elsif temp = 1 then
                down <= '0';
            end if;
            if down = '0' then
                temp <= temp + 1;
            else 
                temp <= temp - 1;
            end if;
        end if;
    end process;
    output <= std_logic_vector(temp);
end architecture;
ieee库;
使用ieee.STD_LOGIC_1164.ALL;
实体计数器为
港口(
clk:标准逻辑中;
rst_n:标准逻辑中;
输出:输出标准逻辑向量(1到0)
);
终端实体;
计数器的结构是
使用ieee.numeric_std.ALL;
信号温度:无符号(输出范围):=(其他=>'0');
信号下降:标准逻辑:='0';
开始
过程(时钟、rst\n)
开始
如果rst_n='0',那么--为什么异步重置??
温度‘0’;

也许它是溢出而不是倒计时。你认为 Lutux/Cux>是可变的而不是信号,因此使用<代码>:= /COD>(立即赋值)而不是<代码>奇怪,你可以添加任何东西到STDYLogic矢量。你用什么工具?正如@diginoise所说的。当
temp=“11”
您分配
down@Staszek时,他们使用的是std_逻辑\u未签名的旧包装。
entity counter_tb is end entity;

library ieee;
use IEEE.STD_LOGIC_1164.ALL;

architecture behavioral of counter_tb is
    signal clk : std_logic;
    signal rst_n : std_logic;
    signal output : std_logic_vector(1 downto 0);
begin
    DUT: entity work.Counter
        port map(
            clk => clk,
            rst_n => rst_n,
            output => output
        );

    rst_n <= '1';
    process
    begin
        clk <= '0', '1' after 1 ns;
        wait for 2 ns;
    end process;
end architecture;