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

标签 vhdl fpga

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

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/

相关文章:

vhdl - 等到 <signal>=1 在 VHDL 仿真中永远不会为真

linux - 如何设置 "something"使用的模块

vhdl - 从同一语句分配两个信号

vhdl - 为什么 Modelsim 10 不编译旧代码?

英特尔 CPU 上的 OpenCL 管道

c - 通过 "make"合并 C 程序和 VHDL 比特流(即使用 Makefile)

vhdl - 模拟器和合成器之间初始化状态机的差异

hardware - 容错(辐射)软核?

filter - CONSTANT 声明是将值存储在 Block-RAM 中还是存储在 FPGA 的触发器中?

compiler-errors - 错误10818-无法推断寄存器,因为它不在时钟沿以外保持其值