gpt4 book ai didi

vhdl-2008 通用类型的解析函数

转载 作者:行者123 更新时间:2023-12-04 14:22:35 26 4
gpt4 key购买 nike

我正在尝试制作一个使用通用类型的组件。在这个组件中,我希望能够使用之前为这些类型定义的函数。考虑以下示例:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package generic_type_pkg is
function increment(x: unsigned) return unsigned;

function increment(x: signed) return signed;

end package;

package body generic_type_pkg is
function increment(x: unsigned) return unsigned is

begin
return x + 1;
end function increment;

function increment(x: signed) return signed is

begin
return x + 1;
end function increment;
end;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

library common;
use common.generic_type_pkg.all;

entity generic_type is
generic(
type type_t
);
port(
clk : in std_logic;

din : in type_t;
dout : out type_t
);
end;

architecture rtl of generic_type is

begin

process(clk)
begin
if rising_edge(clk) then
dout <= increment(din);
end if;
end process;
end;

我使用下面的代码来实例化这个组件:

i_generic_type: entity common.generic_type(rtl)
generic map(
type_t => unsigned
)
port map(
clk => clk,
din => din,
dout => dout
);

如果我用 questasim 编译它,我会得到以下错误:

** Error: */generic_type.vhd(52): (vcom-1600) No feasible entries for subprogram "increment". Visible subprograms are: (explicit) generic_type_pkg.increment[UNSIGNED return UNSIGNED] at */generic_type.vhd(6) (explicit) generic_type_pkg.increment[SIGNED return SIGNED] at ***/generic_type.vhd(8)

VHDL-2008 Just the new stuff 一书指出我需要为实体提供一个通用函数。通过将 function increment ( x: type_t) return type_t 添加到泛型中,我能够解决编译错误。我对此不满意,因为这意味着我需要将要使用的每个函数传递给该组件(例如递增、递减、乘法、移位等)。这将很快变得无法维护。

有没有办法在编译顶级组件时解析这些泛型函数?

最佳答案

你可以做到这一点。定义泛型函数时,可以通过 <>

告诉它使用默认的可见函数
generic (
type t;
function increment(x : t) return t is <>
);

然后当您分配类型 t 时,如果您没有显式分配增量函数,它将采用与签名匹配的函数。

我这样做是为了定义一个通用的“match_x”函数,其中预期结果中的任何 X 值都与实际结果中的任何值相匹配:

function match_X_generic generic ( type data_t;
function to_string(d : data_t) return string is <>
)
parameter( act, exp : data_t )
return boolean;

function match_x is new match_X_generic generic map (std_logic_vector);
function match_x is new match_X_generic generic map (unsigned );
function match_x is new match_X_generic generic map (signed );

这里,to_string 函数自动来自 std_logic_1164 或 numeric_std 包。我可以通过连接到 to_hstring 来提供十六进制版本:

function match_x_hex  is new match_X_generic generic map (std_logic_vector, to_hstring);
function match_x_hex is new match_X_generic generic map (unsigned , to_hstring);
function match_x_hex is new match_X_generic generic map (signed , to_hstring);

所以现在,只要定义了一个 to_string 函数并且可见,我就可以为任何自定义类型创建这个函数:

function match_x is new match_X_generic generic map ( data_t => axis_trans_t        );

关于vhdl-2008 通用类型的解析函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52550088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com