如何简化VHDL中端口的泛型使用?

时间:2012-09-14 01:00:58

标签: generics vhdl

说我有一个实体:

entity myblock is
    port(
        input1 : std_logic_vector(15 downto 0);
        input2 : std_logic_vector(15 downto 0);
        input3 : std_logic_vector(15 downto 0);
        -- ...
        output : std_logic_vector(15 downto 0);
    );
end myblock;

我现在想要使输入的大小通用,所以我可能会这样做:

entity myblock is
    generic(
        WIDTH : natural;
    );
    port(
        input1 : std_logic_vector(WIDTH-1 downto 0);
        input2 : std_logic_vector(WIDTH-1 downto 0);
        input3 : std_logic_vector(WIDTH-1 downto 0);
        -- ...
        output : std_logic_vector(WIDTH-1 downto 0);
    );
end myblock;

理想情况下,我想简化一下,并说:

subtype calc_data is std_logic_vector(WIDTH-1 downto 0);
port(
    input1 : calc_data;
    input2 : calc_data;
    input3 : calc_data;
    -- ...
    output : calc_data;
);

在这种情况下,这是一个非常简单的例子,其好处并不大。但在更复杂的情况下,它确实会有所帮助。

这在VHDL中是否可行?

2 个答案:

答案 0 :(得分:3)

您可以使用单一类型规范命名多个端口:

entity myblock is
    generic(
        WIDTH : natural;
    );
    port(
        input1, input2, input3 : in std_logic_vector(WIDTH-1 downto 0);
        -- ...
        output : out std_logic_vector(WIDTH-1 downto 0);
    );
end myblock;

答案 1 :(得分:1)

当输入只是矢量

时,通常会使用选项2(通用)

如果您的子类型用于携带“意义到读者”(而不仅仅是“宽度信息”),则将子类型存储在包中。

另一种方法是使用没有宽度规范的std_logic_vector,并使宽度从较高级别向下传播。

(有一个关于anonymous types的VHDL新迭代的提议,你可能会觉得这很有趣)