sách gpt4 ai đã đi

VHDL - 适用于任何类型数组的函数/过程

In lại 作者:行者123 更新时间:2023-12-02 00:00:12 28 4
mua khóa gpt4 Nike

câu hỏi:

如果可能的话,如何声明一个用于任何类型参数的函数 TTRONG T 的唯一约束是它被定义为 1D mảnggiống

type T is array ( integer range <> ) of a_random_type;

哪里a_random_type可以是任何kiểu .

以下语法错误的函数是所需的示例

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;

然后可以在任何 mảng 上使用

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 .

<小时>

可能的解决方案:

TừAshenden's VHDL-2008: Just the New Stuff

Generic types can be specified for subprograms.

We can declare a formal generic type in a generic list in the following way:

type indentifier

A function with a generic list takes the form:

function indentifier
generic ( ... )
parameter ( ... ) return result_type is
... -- declarations
begin
... -- statements
end function identifier

这将允许以下定义

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);

这提供了在不同调用之间共享函数体的所需行为,无论 mảng 的元素类型如何。但仍然需要一个实例化函数(可以根据需要在本地运行,所以还不错)。

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

收到第一个答案后发表评论:

前面的信息是我试图找到一种编写 VHDL 子程序的方法,该子程序接受任何参数,只要它是 mảng (Ngay lập tức回答最初的问题)。预期的答案不一定要使用相同的方法(即使用 VHDL-2008 通用子程序)!

1 Câu trả lời

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。因此,您phải使用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 - 适用于任何类型数组的函数/过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43324918/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com