VHDL - 适用于任何类型阵列的功能/过程



问题:

如果可能的话,如何声明用于任何类型的参数的函数TT的唯一约束是它被定义为一维array

type T is array ( integer range <> ) ofa_random_type;

其中a_random_type可以是任何type.

以下语法不正确的函数是所需内容的示例

function measure_size_of_any_array ( a :array ( integer range <> )) return natural is
variable r : natural := 0;
begin
for i in a'range loop
r := r + 1;
end loop;
return r;
end function;

然后可以在任何array上使用

type natural_array is array ( integer range <> ) of natural;
type stdlogv_array is array ( integer range <> ) of std_logic_vector;
[...]
variable some_natural_array : natural_array;
variable some_stdlogv_array : stdlogv_array;
[...]
constant size_1 : natural := measure_size_of_any_array(some_natural_array);
constant size_2 : natural := measure_size_of_any_array(some_stdlogv_array);

显然,这个问题是关于定义函数的方式,而不是关于函数本身:我不是在寻找a'length


可能的解决方案:

来自Ashenden的VHDL-2008:只是新的东西

可以为子程序指定泛型类型。

我们可以通过以下方式在泛型列表中声明一个正式的泛型类型

typeindentifier

具有泛型列表的函数采用以下形式:

functionindentifier
generic   ( ... )
parameter ( ... ) returnresult_typeis
... -- declarations
begin
... -- statements
end functionidentifier

允许以下定义

function measure_size_of_any_array
generic   ( type arr_type )
parameter ( arr : arr_type );

和以下用途

function measure_size_of_natural_array is new measure_size_of_any_array
generic ( arr_type => natural_array );
function measure_size_of_stdlogv_array is new measure_size_of_any_array
generic ( arr_type => stdlogv_array );
constant size_1 : natural := measure_size_of_natural_array(some_natural_array);
constant size_2 : natural := measure_size_of_stdlogv_array(some_stdlogv_array);

这提供了在不同调用之间共享函数主体的所需行为,而不管array元素的类型如何,但仍需要一个实例化函数(它可以根据需要是本地的,所以它不是那么糟糕)。

由于主要供应商对 VHDL-2008的支持很少(我尝试过的编译器不理解以前的解决方案),因此首选 VHDL-87、-93 或 -2002 解决方案。

收到第一个答案后的评论:

前面的信息是我试图找到一种方法来编写一个 VHDL 子程序,只要它是array(回答最初的问题),它接受任何参数。预期的答案不一定应该使用相同的方法(即使用 VHDL-2008 通用子程序)!

在 2008 年之前,只允许在实体级别定义泛型。因此,您可以通过为函数创建特殊实体来将泛型传递给该函数。例如

entity function_ent is
generic(return_value : natural);
port(output : out natural);
end entity;
architecture func_def of function_ent is
function measure_size_of_any_array return natural is
begin
return return_value;
end function;
begin
output <= measure_size_of_any_array;
end architecture;

但是,您希望将类型作为泛型参数传递...这在 2008 <是不可能的。因此,>您必须使用VHDL-2008。

但是,在 VHDL 中使用泛型时,始终必须将某个值(或在 vhdl-2008 中键入)映射到它们。没有智能(预)编译器会自动检测输入类型,就像C++一样。

当您最终决定使用 VHDL-2008 时,您可以问自己:"如果不先定义数组,我会使用该函数吗?应该不会。因此,您可以使用泛型("模板化")包作为与C++类相当的东西。例:

package array_pkg is
generic (type element_type);
type array_type is array (natural range <>) of element_type;
function measure_size_of_array(
arr : array_type) return natural;
end package;
package body array_pkg is
function measure_size_of_array(
arr : array_type) return natural is
begin
return arr'length;
end function;
end package body;
entity test is
end entity test;
library ieee;
architecture beh of test is
package natural_array_pkg is new work.array_pkg
generic map (element_type => natural);
signal test_sig1 : natural_array_pkg.array_type(0 to 10);
constant test_out1 : natural := natural_array_pkg.measure_size_of_array(test_sig1);
use ieee.std_logic_1164.all;
package slv_array_pkg is new work.array_pkg
generic map (element_type => std_logic_vector);
signal test_sig2 : slv_array_pkg.array_type(0 to 12)(5 downto 0);
constant test_out2 : natural := slv_array_pkg.measure_size_of_array(test_sig2);
begin
end architecture;

我认为这是当前 VHDL 中最接近模板化参数的参数。

在 VHDL-2008 中,泛型类型的实现非常少。我已经写了几页LRM更改,以便在VHDL-2017中获得更好的泛型类型。新标准将在几个月内进行投票。

更改集在 LCS-2016-059 中指定。

function foo
generic (
type array_type is array(type is (<>)) of type is private
)
parameter (
input    : array_type;
selected : array_type'index
)
return array_type'element is
begin
return input(selected);
end function;

最新更新