【问题标题】:How to specify an integer array as generic in VHDL?如何在 VHDL 中将整数数组指定为泛型?
【发布时间】:2023-04-09 04:34:02
【问题描述】:

我正在尝试为基于 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 初始化值等。但似乎无法弄清楚如何指定数组。

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

任何帮助将不胜感激

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

【问题讨论】:

    标签: generics instantiation vhdl


    【解决方案1】:

    您可以将数组用作泛型参数,但您不能在此处将其声明,将其作为匿名类型即时声明。

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

    下面是一个例子:

    -- 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;
    

    还要注意use架构中实例化您的实体的包。


    (选读)

    为什么像你这样写它真的是一个语法错误

    VHDL (2002) standard中的VHDL文法,每个泛型参数的声明都是interface_constant_declaration,你有如下文法规则:

    [§ 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_mark) 或现有类型的限制。

    【讨论】:

      【解决方案2】:

      我通过使用 2008 VHDL 标准和预定义属性提出了一种更通用的方法 - 这允许 传递任意长度的数组。 在你的包中定义你的类型,如下所示:

      package data_types is
          type array_of_integers is array(natural range <>) of integer;
      end package;
      

      现在在您的代码中传递通用数组,如下所示:

      generic(
          COEFFICIENTS : array_of_integers := (-1, 0, 1)
      );
      

      现在软件将使用默认索引方案。可以考虑到预定的'left 属性(参考地址:http://www.csee.umbc.edu/portal/help/VHDL/attribute.html):

      -- First coefficient
          ... <= COEFFICIENTS(COEFFICIENTS'left);
      -- Second coefficient
          ... <= COEFFICIENTS(COEFFICIENTS'left + 1);
      

      通常你应该在某种loopgenerate语句中使用这个数组:

      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;
      

      作为 Quartus II 用户的一个小提示 - 也可以在 .bdf 原理图文件中使用通用数组。将参数类型设置为 Auto 并以这种格式重写参数值 - A(D"-1", D"0", D"1"),其中 D 代表 十进制数据类型(有用链接:http://quartushelp.altera.com/14.0/mergedProjects/assign/asd/asd_tab_param.htm

      【讨论】:

        【解决方案3】:

        如果您不介意限制较少的泛型,您可以这样做:

        generic (IO_cfg : integer_vector);
        

        只要你有 VHDL-2008 编译器。

        【讨论】:

        • 确实VHDL-2008 定义了布尔、整数、实数和时间类型的数组类型(自然范围)。然而,VHDL-2008 支持仍然非常有限,至少在我使用的工具(ModelSim、QuartusII)中。
        • @wap26:“非常有限”有点强。 MOdelsim 现在支持大部分,Aldex 支持一切。 Altera 和 Xilinx 支持相当多。类型/包泛型似乎是最糟糕的方面。 integer_vector 在 2008 模式下支持我所知道的所有工具。
        • 好的,感谢您的最新消息。我会检查我的工具的最新版本!
        猜你喜欢
        • 2023-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多