我知道可以创建一个定义范围的子类型,例如:
subtype ADDRESS is UNSIGNED range (32 downto 0);
是否可以创建可参数化的类型/子类型?基本上保留'32'的值由用户在创建ADDRESS类型的变量时定义。例如:
subtype ADDRESS is UNSIGNED range ( f(x) ) downto 0;
其中f()是另一个包中预定义的函数,X是用户在创建ADDRESS实例时提供的自然整数。
基本上我想缩短......
y : UNSIGNED(f(X) downto 0);
到
y : ADDRESS(X);
其中X是自然的。
答案 0 :(得分:1)
嗯,是的。但你不能写:
subtype ADDRESS is UNSIGNED range ( f(x) ) downto 0;
因为这不是正确的VHDL。你必须写:
subtype ADDRESS is UNSIGNED ( f(x) downto 0);
其中x
必须静态。即x
必须是文字,常数或通用。
您可以通过这种方式使用函数来定义常量和其他静态值的值。这种功能的所有输入必须是静态的。有趣的是,这样的函数在精化过程中执行,这使得调试变得困难。如果您在调试此类函数时遇到问题,可以暂时将函数返回值的目标值更改为非静态值(例如变量);然后该函数将在时间0之后执行,使其更容易调试。 这是一个真实的例子:
package P is
function F(I : integer) return integer;
end package P;
package body P is
function F(I : integer) return integer is
begin
if I > 16 then
return I - 1;
else
return (I * 2) - 1;
end if;
end function F;
end package body P;
library IEEE;
use IEEE.numeric_std.all;
use work.P.all;
entity E is
constant X : integer := 16;
end entity E;
architecture E of E is
subtype ADDRESS is UNSIGNED ( f(x) downto 0);
begin
process
begin
report "ADDRESS'left= " & integer'image(ADDRESS'left);
wait;
end process;
end architecture E;
答案 1 :(得分:1)
马修·泰勒的答案已经是正确的,但我想回答你的最后一个问题
y : ADDRESS(X);
在C中你使用的是宏,但在VHDL中这是不可能的。它需要函数ADDRESS
来返回子类型。
IEEE1076-2008定义:
function_specification ::=
[ pure | impure ] function designator
subprogram_header
[ [ parameter ] ( formal_parameter_list ) ] return type_mark
其中:
type_mark ::=
type_name
| subtype_name
即。返回类型必须是已定义的 类型 。它不能是新的[sub]类型。 您可以使用子类型定义执行某些操作,例如Matthew解释:
subtype ADDRESS_X is unsigned(f(X) downto 0);
signal new_signal : ADDRESS_X;