【问题标题】:If statement using vhdl-counter使用 vhdl-counter 的 if 语句
【发布时间】:2015-03-07 21:48:11
【问题描述】:

It'z DFF 计数器从 0 计数到 10,从 10 计数到 0。有 z 在升序/降序之间切换。这个网站上的一些人帮助我解决了 if 语句问题,但看起来不允许在进程之外使用它,如果有人可以提供帮助,并且有任何想法可以在 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;

错误还是一样:

 [HDLCompiler 806] Syntax error near "if". 
 [HDLCompiler 806] Syntax error near "then". 
 [HDLCompiler 806] Syntax error near "else". 
 [HDLCompiler 806] Syntax error near "then". 

 [HDLCompiler 806] Syntax error near "then". 
 [HDLCompiler 806] Syntax error near "else". 

【问题讨论】:

  • if 语句是顺序语句,可以在流程语句、循环语句或子程序中找到,而不是在体系结构主体中。下次考虑提供一个最小的、可验证的和完整的示例,您的第一个问题缺少该示例。

标签: if-statement vhdl fpga


【解决方案1】:

请注意您缺少一个带有 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;

现在肯定有人会插话并写下这两个过程可以合并。

这可能看起来像:

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;

有待进一步改进或替代实施。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多