我有多个属于一起的输入(在同一时钟等处采样),但在我需要修改的一段现有代码中逻辑上不是向量(即不是并行总线)。
以前,它们被定义为
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;
对于大多数意图和目的,数组和向量的处理方式几乎相同,所以我的问题是:
这两种做事方式有什么优点吗?有首选/标准方式吗?
最佳答案
有一个区别,与 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;
这确实可以编译(即没有错误)。
关于types - VHDL:使用 std_logic 数组与使用 std_logic_vector 子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46198216/