繁体   English   中英

vhdl-2008泛型类型的解析函数

[英]vhdl-2008 resolve function for generic type

我正在尝试使用通用类型的组件。 在该组件内部,我希望能够使用以前为这些类型定义的功能。 考虑以下示例:

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进行编译,则会出现以下错误:

**错误: * / generic_type.vhd(52):(vcom-1600)子程序“ increment”没有可行的条目。 可见的子程序为:(显式)* / generic_type.vhd(6)处的generic_type_pkg.increment [未签名返回unsigned](显式)*** / generic_type.vhd(8)处的(explicit)generic_type_pkg.increment [SIGNED return SIGNED]

VHDL-2008》这本书刚出现的新事物指出,我需要为实体提供通用功能。 通过添加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        );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM