if语句使用vhdl-counter

时间:2015-03-07 21:48:12

标签: if-statement vhdl fpga

它是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". 

1 个答案:

答案 0 :(得分:1)

请注意您缺少一个模式为inc_dec的端口。

正如评论中所提到的,你的if语句不是一个并发语句,需要进入一个过程。

对于VHDL,r_next的增量和减量都不正确。

暂停不应该是异步的它会在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;

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