If statement 使用vhdl计数器的If语句

If statement 使用vhdl计数器的If语句,if-statement,vhdl,fpga,If Statement,Vhdl,Fpga,它的DFF计数器计数从0到10,从10到0。有z开关在上升/下降之间切换。在这个网站上,有几个人帮我解决了if语句的问题,但看起来itz不允许在流程之外使用它,si如果有人可以帮助我,并且有任何想法在istead时使用它。那太好了。使用planahead设计此计数器 library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity counter_10 is port( clk, reset, pau

它的DFF计数器计数从0到10,从10到0。有z开关在上升/下降之间切换。在这个网站上,有几个人帮我解决了if语句的问题,但看起来itz不允许在流程之外使用它,si如果有人可以帮助我,并且有任何想法在istead时使用它。那太好了。使用planahead设计此计数器

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity counter_10 is
port(
     clk, reset, pause: in std_logic;
     q: out std_logic_vector(3 downto 0)
);
end counter_10;

architecture arc_counter of counter_10 is
constant M: integer:=10;
signal r_reg: unsigned(3 downto 0);
signal r_next: unsigned(3 downto 0);
begin
process(clk, reset, pause)
begin
if(reset='1') then r_reg <=(others=>'0');
     elsif pause = '1' then
     r_reg<=r_reg;
     elsif (clk'event and clk='1') then
           r_reg<=r_next;
end if;
end process;
------------------------------------------------------------------------

if (inc_dec='1') then
   if (r_reg=(M-1)) then
        r_next <= (others=>'0');
   else 
        r_reg+1;
   end if; 
elsif (inc_dec='0') then
   if (r_reg=(M-10)) then
        r_next <=  to_unsigned(9, 4);
   else
        r_reg-1;
   end if;
end if;
------------------------------------------------------------------------

--Output logic
q<= std_logic_vector(r_reg);
end arc_counter;

请注意,在for inc_dec中缺少一个模式为的端口

正如注释中提到的,if语句不是并发语句,需要在一个过程中执行

您的r_next的增量和减量对于VHDL不正确

暂停不应该是异步的,因为它会推断r_reg寄存器后面有一个锁存

修复所有这些问题,它看起来像这样:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter_10 is
    port (
         clk:       in  std_logic;
         reset:     in  std_logic;
         pause:     in  std_logic;
         inc_dec:   in  std_logic;                    -- ADDED
         q:         out std_logic_vector(3 downto 0)
    );
end counter_10;

architecture arc_counter of counter_10 is
    -- constant M: integer := 10;  -- not needed
    signal r_reg: unsigned(3 downto 0);
    signal r_next: unsigned(3 downto 0);
begin
UNLABELED:
    process(clk, reset)
    begin
    if reset = '1' then 
        r_reg <= (others=>'0');
    -- elsif pause = '1' then
        -- r_reg <= r_reg;
    elsif clk'event and clk = '1' and  not pause = '1' then
        r_reg <= r_next;
    end if;
    end process;
ADDED_PROCESS:
    process  (inc_dec, r_reg)
    begin
        if inc_dec = '1' then
            if r_reg = 9 then -- r_reg = M - 1 then
                 r_next <= (others => '0');
            else 
                 r_next <= r_reg + 1;  -- r_reg+1;
            end if; 
        elsif inc_dec = '0' then
            if r_reg = 0 then -- r_reg = M - 10 then
                 r_next <=  to_unsigned(9, 4);
            else
                 r_next <= r_reg - 1;   -- r_reg-1;
            end if;
        end if;
    end process;
--Output 
    q<= std_logic_vector(r_reg);
end arc_counter;
ieee库;
使用ieee.std_logic_1164.all;
使用ieee.numeric_std.all;
实体计数器_10为
港口(
clk:标准逻辑中;
复位:在标准逻辑中;
暂停:在标准逻辑中;
inc_dec:在标准逻辑中;--添加
q:输出标准逻辑向量(3到0)
);
末端计数器10;
计数器10的架构arc_计数器为
--常数M:整数:=10;--不需要
信号r_reg:无符号(3到0);
信号r_next:无符号(3到0);
开始
未标记:
过程(时钟、复位)
开始
如果重置='1',则
r_reg“0”);
--如果暂停='1',则

--r_reg if语句是一个顺序语句,可以在进程语句、循环语句或子程序中找到,而不在体系结构主体中。下一次考虑提供一个最小的、可验证的、完整的例子,你的第一个问题就不见了。
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter_10 is
    port (
         clk:       in  std_logic;
         reset:     in  std_logic;
         pause:     in  std_logic;
         inc_dec:   in  std_logic;                    -- ADDED
         q:         out std_logic_vector(3 downto 0)
    );
end counter_10;

architecture arc_counter of counter_10 is
    -- constant M: integer := 10;  -- not needed
    signal r_reg: unsigned(3 downto 0);
    signal r_next: unsigned(3 downto 0);
begin
UNLABELED:
    process(clk, reset)
    begin
    if reset = '1' then 
        r_reg <= (others=>'0');
    -- elsif pause = '1' then
        -- r_reg <= r_reg;
    elsif clk'event and clk = '1' and  not pause = '1' then
        r_reg <= r_next;
    end if;
    end process;
ADDED_PROCESS:
    process  (inc_dec, r_reg)
    begin
        if inc_dec = '1' then
            if r_reg = 9 then -- r_reg = M - 1 then
                 r_next <= (others => '0');
            else 
                 r_next <= r_reg + 1;  -- r_reg+1;
            end if; 
        elsif inc_dec = '0' then
            if r_reg = 0 then -- r_reg = M - 10 then
                 r_next <=  to_unsigned(9, 4);
            else
                 r_next <= r_reg - 1;   -- r_reg-1;
            end if;
        end if;
    end process;
--Output 
    q<= std_logic_vector(r_reg);
end arc_counter;
architecture foo of counter_10 is
    -- constant M: integer := 10;  -- not needed
    signal r_reg: unsigned(3 downto 0);
    signal r_next: unsigned(3 downto 0);
begin
SINGLE_PROCESS:
    process(clk, reset)
    begin
    if reset = '1' then 
        r_reg <= (others=>'0');
    -- elsif pause = '1' then
        -- r_reg <= r_reg;
    elsif clk'event and clk = '1' and  not pause = '1' then
        if inc_dec = '1' then
            if r_reg = 9 then
                r_reg <= (others => '0');
            else
                r_reg <= r_reg + 1;
            end if;
        elsif inc_dec = '0' then   -- and this could be simply else
            if r_reg = 0 then
                r_reg <= to_unsigned(9, 4);
            else
                r_reg <= r_reg - 1;
            end if;
        end if;
        r_reg <= r_next;
    end if;
    end process;
--Output 
    q<= std_logic_vector(r_reg);
end architecture;