VHDL:使用 std_logic 的数组与使用 std_logic_vector 的子类型
VHDL: Using array of std_logic vs. using subtype of std_logic_vector
我有多个属于一起的输入(在同一个时钟上采样等)但在我需要修改的一段现有代码中在逻辑上不是向量(即不是并行总线)。
以前,它们被定义为
type my_type is array (my_width - 1 downto 0) of std_logic;
signal my_signal : my_type;
直到现在,为了这个目的,我总是使用这个:
subtype my_subtype is std_logic_vector(my_width - 1 downto 0);
signal my_signal : my_subtype;
对于大多数意图和目的,数组和向量的处理方式几乎相同,所以我的问题是:
这两种做事方式有什么好处吗?有 preferred/standard 方法吗?
有区别,与strong typing有关。
通过您的第一段代码,您创建了一个新类型。它是独立的,与其他类型不兼容。只需考虑下一段代码:
entity e is end entity;
library ieee;
architecture a of e is
use ieee.std_logic_1164.all;
type slv1_t is array (15 downto 0) of std_logic;
signal slv1 : slv1_t;
type slv2_t is array (7 downto 0) of std_logic;
signal slv2 : slv2_t;
begin
slv2 <= slv1(7 downto 0);
end architecture;
在modelsim中编译时,这段代码会报错:
Error: C:/HDL/TypeVsSubtype/Type.vhd(10): Cannot resolve slice name as type slv2_t.
对于第二段代码,底层类型仍然是std_logic_vector
。因此子类型是兼容的。考虑下一段代码:
entity e is end entity;
library ieee;
architecture a of e is
use ieee.std_logic_1164.all;
subtype slv1_t is std_logic_vector(15 downto 0);
signal slv1 : slv1_t;
subtype slv2_t is std_logic_vector(7 downto 0);
signal slv2 : slv2_t;
begin
slv2 <= slv1(7 downto 0);
end architecture;
这确实可以编译(即没有错误)。
我有多个属于一起的输入(在同一个时钟上采样等)但在我需要修改的一段现有代码中在逻辑上不是向量(即不是并行总线)。
以前,它们被定义为
type my_type is array (my_width - 1 downto 0) of std_logic;
signal my_signal : my_type;
直到现在,为了这个目的,我总是使用这个:
subtype my_subtype is std_logic_vector(my_width - 1 downto 0);
signal my_signal : my_subtype;
对于大多数意图和目的,数组和向量的处理方式几乎相同,所以我的问题是:
这两种做事方式有什么好处吗?有 preferred/standard 方法吗?
有区别,与strong typing有关。
通过您的第一段代码,您创建了一个新类型。它是独立的,与其他类型不兼容。只需考虑下一段代码:
entity e is end entity;
library ieee;
architecture a of e is
use ieee.std_logic_1164.all;
type slv1_t is array (15 downto 0) of std_logic;
signal slv1 : slv1_t;
type slv2_t is array (7 downto 0) of std_logic;
signal slv2 : slv2_t;
begin
slv2 <= slv1(7 downto 0);
end architecture;
在modelsim中编译时,这段代码会报错:
Error: C:/HDL/TypeVsSubtype/Type.vhd(10): Cannot resolve slice name as type slv2_t.
对于第二段代码,底层类型仍然是std_logic_vector
。因此子类型是兼容的。考虑下一段代码:
entity e is end entity;
library ieee;
architecture a of e is
use ieee.std_logic_1164.all;
subtype slv1_t is std_logic_vector(15 downto 0);
signal slv1 : slv1_t;
subtype slv2_t is std_logic_vector(7 downto 0);
signal slv2 : slv2_t;
begin
slv2 <= slv1(7 downto 0);
end architecture;
这确实可以编译(即没有错误)。