【问题标题】:vhdl-2008 resolve function for generic type泛型类型的 vhdl-2008 解析函数
【发布时间】:2018-09-28 07:05:14
【问题描述】:

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

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) 子程序“增量”没有可行的条目。可见的子程序有: (显式)generic_type_pkg.increment[UNSIGNED return UNSIGNED] at */generic_type.vhd(6) (显式)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 添加到泛型中,我能够解决编译错误。我对此不满意,因为这意味着我需要将要使用的每个函数传递给该组件(例如递增、递减、乘法、移位……)。这将很快变得无法维护。

有没有办法在编译顶级组件时解决这些通用函数?

【问题讨论】:

  • No。参见 IEEE 标准 1076-2008。 type_t 的子类型(6.5.3 接口类型声明)直到关联(6.5.7)才知道。如果没有在通用映射方面(6.5.7.2、14.3.3.3)提供子类型(这里有符号或无符号),则不知道基类型类(5.1、标量、复合、访问、文件、受保护)和 type_t 的基类型.如果没有这些,则在分析期间无法识别函数增量签名 (4.5.3)(4.5、12.5、4.10)。
  • generic_type_pkg.vhdl:24:18:error: no function declarations for operator "+"。 (如果你尝试定义它,你会发现类型类是未知的,type_t 的元素类型是什么?)

标签: vhdl fpga


【解决方案1】:

你可以这样做。定义泛型函数时,可以使用

告诉它使用默认的可见函数
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        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多