我需要设计一个带有通用参数的模"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;
Run Code Online (Sandbox Code Playgroud)