模数通用计数器 [英] modulo n generic counter
问题描述
我需要设计一个带有通用参数的模数"n"计数器.我无法固定将保存输出的std_logic_vector的长度.首先,我在数字类型上使用气浮运算符时遇到错误.其次,不允许在向量的范围规范中使用动态表达式.到目前为止,这是我的代码:
I am required to design a modulo "n" counter with generic parameters. I am having trouble fixing the length of the std_logic_vector which will hold the output. Firstly, I get errors regarding the use of airthmetic operators on numeric types. And secondly, I am not allowed to use a dynamic expression in the range specification of the vector. Here is my code so far:
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;
推荐答案
一些建议:
-
必须将
ceil(log2(1.0*n))
更改为natural(ceil(log2(real(n))))
,以寻址 类型不匹配
Must change
ceil(log2(1.0*n))
tonatural(ceil(log2(real(n))))
, to address the type mismatches
必须将count := 0
更改为count := (others => '0')
,因为它不是
可以直接将0
分配给std_logic_vector
.
Must change count := 0
to count := (others => '0')
, since it is not
possible to assign 0
directly to std_logic_vector
.
由于使用了
standard
程序包类型清楚地说明了意图
May change integer range 1 to integer'right
to positive
, since use of the
standard
package type states the intention clearly
由于使用了 VHDL属性清楚地说明了意图
May change range in variable declaration to data_in'range
, since use of the
VHDL attribute states the intention clearly
可以将if(conv_integer(count)=n-1) then
更改为if (count = n-1) then
,
因为使用时不需要conv_integer
ieee.std_logic_unsigned.all
May change if(conv_integer(count)=n-1) then
to if (count = n-1) then
,
since the conv_integer
is not required when using
ieee.std_logic_unsigned.all
可能会将if(...) then
更改为if ... then
,因为在
if
因为if
是语句而不是函数
May change if(...) then
to if ... then
, since ()
are not required in
if
because if
is a statement and not a function
由于使用了
std_logic_1164
软件包功能清楚地说明了意图
May change clk'event and clk = '1'
to rising_edge(clk)
, since use of the
std_logic_1164
package function states the intention clearly
可能会将else if
更改为使用elsif
,以使内容更清晰明了
样式
May change the else if
to use of elsif
for a clearer and less verbose
style
然后可以将代码更新为:
The code can then be updated to:
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
更改为:
Consider changing ieee.std_logic_unsigned
to ieee.numeric_std.all
, since
the ieee.std_logic_unsigned
is not a standard package, so the place in the
ieee
library is misleading. It requires change in inner if
to:
if to_integer(unsigned(count)) = n-1 then
count := (others => '0');
else
count := std_logic_vector(unsigned(count)+1);
end if;
这篇关于模数通用计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!