Generics 如何在VHDL中将整数数组指定为泛型?

Generics 如何在VHDL中将整数数组指定为泛型?,generics,instantiation,vhdl,Generics,Instantiation,Vhdl,我正在尝试为基于SPI的IO扩展器创建一个通用驱动程序。其思想是在实例化中传递与请求的IO设置相匹配的初始化值 我当前的尝试如下所示: entity max7301_simple is generic ( IO_cfg : array (1 to 7) OF integer range 0 to 255 := (16#55#, 16#55#, 16#55#, 16#55#, 16#55#, 16#55#, 16#55#) ); port

我正在尝试为基于SPI的IO扩展器创建一个通用驱动程序。其思想是在实例化中传递与请求的IO设置相匹配的初始化值

我当前的尝试如下所示:

entity max7301_simple is
   generic ( 
        IO_cfg : array (1 to 7) OF integer range 0 to 255 := (16#55#, 16#55#, 16#55#, 16#55#, 16#55#, 16#55#, 16#55#)
           );
     port  (
        -- Application interface :
        clk_i       :   in std_logic;        -- input clock, xx MHz.
        rst_i       :   in std_logic;        -- sync reset.
        en_i        :   in std_logic;        -- enable, forces re-init of pins on MAX7301.
        output_i    :   in std_logic_vector(27 downto 0);   --data to write to output pins on MAX7301
        irq_o       :   out std_logic;       -- IRQ, TODO: what triggers, change on inputs ?
        input_o     :   out std_logic_vector(27 downto 0);  --data read from input pins on MAX7301
        -- MAX7301 SPI interface
        sclk        :   out std_logic;        -- SPI clock
        din         :   in std_logic;        -- SPI data input
        dout        :   out std_logic;       -- SPI read data
        cs          :   out std_logic        -- SPI chip select
    );
end max7301_simple;
问题在于IO_cfg数组,我尝试了各种尝试,包括w/wo init值等,但似乎无法确定如何指定数组

我相信已经读到过,可以将数组作为泛型传递,但仍然没有太多的运气。Xilinx ISE只是告诉med“数组附近的语法错误”,它的信息量不足以让我前进

任何帮助都将不胜感激


在实例化这个模块时,我总是需要7个值

您可以将数组用作泛型参数,但不允许将其动态声明为匿名类型

您必须首先在单独的包(可以在同一个文件中,也可以在单独的文件中)中声明整数数组类型,然后在实体中以及实例化该包时使用该包

下面是一个示例,说明了如何做到这一点:

-- package declaration
package mytypes_pkg is

     type my_array_t is array (1 to 7) of integer range 0 to 255;

end package mytypes_pkg;

-- entity "uses" the package   
use work.mytypes_pkg.all;

entity max7301_simple is
   generic ( 
        IO_cfg : my_array_t := (16#55#, 16#55#, 16#55#, 16#55#, 16#55#, 16#55#, 16#55#)
           );
   -- ports [...]
end max7301_simple;
还要注意
使用架构中实例化实体的包


(可选阅读)

为什么像您那样编写它真的是一个语法错误?

查看中的VHDL语法,每个泛型参数的声明都是一个
接口\常量\声明
,您有以下语法规则:

[§ 4.3.2]
interface_constant_declaration ::=  
            [ constant ] identifier_list : [ in ] subtype_indication [ := static_expression ]

[§ 4.2]
subtype_indication ::= 
            [ resolution_function_name ] type_mark [ constraint ]

类型引用只能是现有类型的名称(
type\u mark
)或现有类型的限制。

如果您不介意约束较少的泛型,可以执行以下操作:

generic (IO_cfg : integer_vector);

只要你有一个VHDL-2008编译器。

我建议使用2008 VHDL标准和预定义属性来实现更通用的方法-这允许传递任意长度的数组。 在包中定义您的类型,如下所示:

现在软件将使用默认的索引方案。它可以用预定的 属性(可在以下位置找到引用:):

——第一系数

... 确实为布尔、整数、实时和时间类型定义了数组类型(自然范围)。然而,VHDL-2008支持仍然非常有限,至少在我使用的工具(ModelSim、QuartusII)中是如此。@wap26:“非常有限”有点强。MOdelsim现在支持大部分,Aldex支持一切。Altera和Xilinx支持相当大的数量。类型/包泛型似乎是最糟糕的方面<代码>整数向量
在2008模式下支持我所知道的所有工具。好的,感谢最新消息。我会检查我的工具的最新版本!
package data_types is
    type array_of_integers is array(natural range <>) of integer;
end package;
generic(
    COEFFICIENTS : array_of_integers := (-1, 0, 1)
);
-- First coefficient
    ... <= COEFFICIENTS(COEFFICIENTS'left);
-- Second coefficient
    ... <= COEFFICIENTS(COEFFICIENTS'left + 1);
GENERATE_STATEMENT: for entry in 0 to COEFFICIENTS'length-1 generate
    out(entry) <= std_logic_vector(to_signed(COEFFICIENTS(COEFFICIENTS'left + entry), out(entry)'length));
end generate;