模数通用计数器

时间:2014-05-27 06:47:56

标签: generics vhdl counter modulo

我需要设计一个模数" n"计数器与通用参数。我无法修复将保存输出的std_logic_vector的长度。首先,我在数字类型上使用airthmetic运算符时遇到错误。其次,我不允许在向量的范围规范中使用动态表达式。到目前为止,这是我的代码:

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;  
use IEEE.math_real.all;

entity counter_mod is
  generic(n: integer range 1 to integer'right);
  port(clk,reset,load:in std_logic;
  data_in:in std_logic_vector(ceil(log2(1.0*n))-1 downto 0);
  q:out std_logic_vector(ceil(log2(1.0*n))-1 downto 0));
end counter_mod;

architecture behavioral of counter_mod is
begin
  process(clk,reset,load)
  variable count:std_logic_vector(ceil(log2(1.0*n))-1 downto 0):=(others=>'0');
  begin
      if(reset='1') then
          count:=(others=>'0');
      else if(load='1') then
          count:=data_in;
      else if(clk'event and clk='1') then
          if(conv_integer(count)=n-1) then
            count:=0;
          else
            count:=count+1;
          end if;
      end if;
      end if;
      end if;
      q<=count;
  end process;
end architecture;

1 个答案:

答案 0 :(得分:6)

一些建议:

  • 必须将ceil(log2(1.0*n))更改为natural(ceil(log2(real(n))))才能解决问题 类型不匹配

  • 必须将count := 0更改为count := (others => '0'),因为它不是 可以直接将0分配给std_logic_vector

  • 可以将integer range 1 to integer'right更改为positive,因为使用了。{ standard包类型明确说明意图

  • 可以将变量声明中的范围更改为data_in'range,因为使用了。{ VHDL属性明确说明意图

  • 可能会将if(conv_integer(count)=n-1) then更改为if (count = n-1) then, 因为使用时不需要conv_integer ieee.std_logic_unsigned.all

  • 可能会将if(...) then更改为if ... then,因为()不需要if if因为clk'event and clk = '1'是一个陈述而不是一个函数

  • 可以将rising_edge(clk)更改为std_logic_1164,因为使用了。{ else if包函数明确说明意图

  • 可能会更改elsif使用library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_unsigned.all; use IEEE.math_real.all; entity counter_mod is generic(n : positive); port(clk, reset, load : in std_logic; data_in : in std_logic_vector(natural(ceil(log2(real(n))))-1 downto 0); q : out std_logic_vector(natural(ceil(log2(real(n))))-1 downto 0)); end counter_mod; architecture behavioral of counter_mod is begin process(clk, reset, load) variable count : std_logic_vector(data_in'range) := (others => '0'); begin if reset = '1' then count := (others => '0'); elsif load = '1' then count := data_in; elsif rising_edge(clk) then if count = n-1 then count := (others => '0'); else count := count+1; end if; end if; q <= count; end process; end architecture; 以获得更清晰,更简洁的详细信息 式

然后可以将代码更新为:

ieee.std_logic_unsigned

考虑将ieee.numeric_std.all更改为ieee.std_logic_unsigned ieee不是标准包,所以在...中的位置 if库有误导性。它需要将内部if to_integer(unsigned(count)) = n-1 then count := (others => '0'); else count := std_logic_vector(unsigned(count)+1); end if; 更改为:

{{1}}
相关问题