0

我想知道是否有一种方法可以在 VHDL 中定义具有大小参数的类型。例如

type count_vector(size: Natural) is unsigned (size-1 downto 0);

然后再做类似的事情

variable int : count_vector(32) := (others => '0');
variable nibble : count_vector(4) := (others => '0');

本质上,有没有办法定义“类数组”类型,或者语法不允许这样做?

我目前正在尝试使用泛型来实现可重用性,但我希望能够最大限度地利用泛型类型(即:是否可以在 VHDL 中编写类型泛型实体?)。

提前致谢!

4

1 回答 1

4
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity foo is
     generic (
        constant INT_LEFT:    natural := 32;
        constant NIB_LEFT:    natural := 4
     );
     -- note entity declarative items are forward looking only
     -- a port declaration requires a type or subtype declared in a package
     -- you can also use a package for any constants

     -- a type or subtype can be declared as an entity declarative item here:
--     subtype int_size is unsigned(INT_LEFT downto 0);
--     subtype nib_size is unsigned(NIB_LEFT downto 0);
end entity;

architecture fum of foo is
    -- or as an architecture declarative item here:
    subtype int_size is unsigned(INT_LEFT downto 0);
    subtype nib_size is unsigned(NIB_LEFT downto 0);   

begin
USAGE:
    process 
        variable int:  int_size := (others => '0');
        variable nibble: nib_size := (others =>'0');
    begin
        int := int + 3;
        -- the function "+" is L: UNSIGNED, R: NATURAL
        --  int_size and nibble_size are subtypes of UNSIGNED
        Report integer'IMAGE(TO_INTEGER(int));
        -- ditto for TO_INTEGER
        nibble := nibble + 2;
        -- if int_size or nibble_size were types it would require
        -- operator functions for those types.
        Report integer'IMAGE(TO_INTEGER(nibble));
        wait;
    end process;

end architecture fum;

添加:

这是对 BennyBarns 在对问题的评论中断言的回应:“我想补充一点,虽然您可以在 VHDL 中使用 n 维数组,但只有第一个数组可能不受约束”。

与断言相反:

entity t1 is
end entity;

architecture foo of t1 is

    type typeI is array ( natural range <>, natural range <>) of integer;

begin
    process is
        variable sigI : typeI(0 to 1, 0 to 1);  -- 2D integer array
    begin
        sigI(0,0) := 1;
        sigI(0,1) := 2;
        sigI(1,0) := 3;
        sigI(1,1) := 4; 
        report "Initialized indiviually";
        report "sigI(0,0) = " & integer'IMAGE(sigI(0,0));
        report "sigI(0,1) = " & integer'IMAGE(sigI(0,1));
        report "sigI(1,0) = " & integer'IMAGE(sigI(1,0));
        report "sigI(1,1) = " & integer'IMAGE(sigI(1,1));
        sigI := ((11,12),(13,14));
        report "Initialized as an aggregate";
        report "sigI(0,0) = " & integer'IMAGE(sigI(0,0));
        report "sigI(0,1) = " & integer'IMAGE(sigI(0,1));
        report "sigI(1,0) = " & integer'IMAGE(sigI(1,0));
        report "sigI(1,1) = " & integer'IMAGE(sigI(1,1));
        wait;
    end process;
end architecture;

该语句不精确,并且在示例子类型声明中与 numeric_std 包中 UNSIGNED 类型声明中的延迟范围约束相关。子类型指示需要由类型标记提供或明确提供的约束。它仅对无约束类型的子类型指示类型标记有效。

无约束类型的子类型声明必须提供约束,就像您添加了

signal A: unsigned;

作为实体 foo 的架构声明项:

ghdl -a foo.vhdl
foo.vhdl:24:12: declaration of signal "a" with unconstrained array type "unsigned" is not allowed

为了让事情变得有趣,界面列表可以很特别:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity fie is
    port (
        a:  in  unsigned
    );
end entity;

architecture fee of fie is

begin

EVAL:
    process (a)
    begin
        report "Fie:a range is " & integer'IMAGE(a'LEFT) & " to " & 
                                  integer'IMAGE(a'RIGHT) ;
    end process;

end architecture fee;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity fie_tb is
end entity;

architecture fum of fie_tb is
    component fie is
        port (a: in unsigned);
    end component;

    signal aa:  unsigned (3 to 7);
begin

EUT: fie port map (a => aa);

end architecture;

'规则'可以在索引约束和离散范围的 LRM 部分找到,IEEE Std 1076-2008 5.3.2.2, -2002/-1993 3.2.1.1。

于 2013-08-15T02:35:49.917 回答