VHDL 中的参数化常量
Parameterized constants in VHDL
我正在设计一个通用的数据路径,作为其中的一部分,不同的解码器使用了恒定的模式。例如:
NBITS = 4 -> 常量为“1000”
NBITS = 5 -> 常量为“10000”
NBITS = 16 -> 常数将是“1000_0000_0000_0000”
我想像下面这样的东西可以解决问题:
constant NaR : std_logic_vector(NBITS - 1 downto 0) := (NBITS - 1 => '1', others => '0');
但遗憾的是,它无法编译并生成错误:非静态选择排除其他选择。
如何指定作为通用参数函数的常量位模式?
当有多个选择时,要求聚合具有本地静态选择。 (确定表达式值的代码是在分析时构造的。)
IEEE Std 1076-2008 9.3.3.3 阵列聚合第 6 段:
Apart from a final element association with the single choice others, the rest (if any) of the element associations of an array aggregate shall be either all positional or all named. A named association of an array aggregate is allowed to have a choice that is not locally static, or likewise a choice that is a null range, only if the aggregate includes a single element association and this element association has a single choice. An others choice is locally static if the applicable index constraint is locally static.
精化时(14.4.2.5)可以通过函数调用(表达式,9.3.4 函数调用)提供常量的值表达式(6.4.2.2 常量声明)或变量或信号的初始表达式对象声明)。函数可以是不纯的(4. 子程序和包,4.1 通用,9.4.3 全局静态原色,注 2)。
构造一个 Minimal, Complete, and Verifiable example 可以证明这一点:
library ieee;
use ieee.std_logic_1164.all;
ravenwater_mcve is
generic (NBITS: natural := 42);
end entity;
architecture foo of ravenwater_mcve is
impure function leftbit return std_logic_vector is
variable retv: std_logic_vector (NBITS - 1 downto 0) :=
(others => '0');
begin
retv(retv'LEFT) := '1';
return retv;
end function;
constant NaR: std_logic_vector(NBITS - 1 downto 0) := leftbit;
-- (NBITS - 1 => '1', others => '0');
begin
end architecture;
MCVe 分析、阐述和模拟。
您可以添加一个过程来判断值表达式是否正确:
RESULT:
process
begin
report "NaR = " & to_string(NaR);
wait;
end process;
如果使用 -2008 之前的 VHDL 标准修订版,请提供 to_string 函数:
function to_string (inp: std_logic_vector) return string is
variable image_str: string (1 to inp'length);
alias input_str: std_logic_vector (1 to inp'length) is inp;
begin
for i in input_str'range loop
image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
end loop;
return image_str;
end function;
这会产生:
ghdl -r ravenwater_mcve
ravenwater_mcve.vhdl:33:9:@0ms:(report note): NaR = 100000000000000000000000000000000000000000
(也许我应该为通用常量使用较小的默认值。)
您也可以将泛型的值作为参数传递给纯函数。
我正在设计一个通用的数据路径,作为其中的一部分,不同的解码器使用了恒定的模式。例如:
NBITS = 4 -> 常量为“1000”
NBITS = 5 -> 常量为“10000”
NBITS = 16 -> 常数将是“1000_0000_0000_0000”
我想像下面这样的东西可以解决问题:
constant NaR : std_logic_vector(NBITS - 1 downto 0) := (NBITS - 1 => '1', others => '0');
但遗憾的是,它无法编译并生成错误:非静态选择排除其他选择。
如何指定作为通用参数函数的常量位模式?
当有多个选择时,要求聚合具有本地静态选择。 (确定表达式值的代码是在分析时构造的。)
IEEE Std 1076-2008 9.3.3.3 阵列聚合第 6 段:
Apart from a final element association with the single choice others, the rest (if any) of the element associations of an array aggregate shall be either all positional or all named. A named association of an array aggregate is allowed to have a choice that is not locally static, or likewise a choice that is a null range, only if the aggregate includes a single element association and this element association has a single choice. An others choice is locally static if the applicable index constraint is locally static.
精化时(14.4.2.5)可以通过函数调用(表达式,9.3.4 函数调用)提供常量的值表达式(6.4.2.2 常量声明)或变量或信号的初始表达式对象声明)。函数可以是不纯的(4. 子程序和包,4.1 通用,9.4.3 全局静态原色,注 2)。
构造一个 Minimal, Complete, and Verifiable example 可以证明这一点:
library ieee;
use ieee.std_logic_1164.all;
ravenwater_mcve is
generic (NBITS: natural := 42);
end entity;
architecture foo of ravenwater_mcve is
impure function leftbit return std_logic_vector is
variable retv: std_logic_vector (NBITS - 1 downto 0) :=
(others => '0');
begin
retv(retv'LEFT) := '1';
return retv;
end function;
constant NaR: std_logic_vector(NBITS - 1 downto 0) := leftbit;
-- (NBITS - 1 => '1', others => '0');
begin
end architecture;
MCVe 分析、阐述和模拟。
您可以添加一个过程来判断值表达式是否正确:
RESULT:
process
begin
report "NaR = " & to_string(NaR);
wait;
end process;
如果使用 -2008 之前的 VHDL 标准修订版,请提供 to_string 函数:
function to_string (inp: std_logic_vector) return string is
variable image_str: string (1 to inp'length);
alias input_str: std_logic_vector (1 to inp'length) is inp;
begin
for i in input_str'range loop
image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
end loop;
return image_str;
end function;
这会产生:
ghdl -r ravenwater_mcve
ravenwater_mcve.vhdl:33:9:@0ms:(report note): NaR = 100000000000000000000000000000000000000000
(也许我应该为通用常量使用较小的默认值。)
您也可以将泛型的值作为参数传递给纯函数。