理论部分
一部分是元件声明部分,通常放在结构体声明部分;另一部分是元件例化部分,放在结构体描述部分。
元件声明
component 元件名 is
port(例化元件端口名表);
end component 元件名;
元件例化
例化名:元件名
port map(元件端口=>当前实体端口,...,元件端口=>当前实体端口);
上述端口映射方式称为名称关联,即根据名称将相应的端口对应起来。
注:当顶层元件的端口、信号或参数排列顺序完全一致时,可以这样写:
port map(当前实体端口1,...,当前实体端口n);
实战部分
代码展示:
------------------half_adder.vhd----------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity half_adder is
port(a,b: in std_logic;
sum,carry:out std_logic
);
end entity;
architecture rtl of half_adder is
begin
sum<=a xor b;
carry<=a and b;
end architecture;
--------------full_adder.vhd-----------------------------------
library ieee;
use ieee.std_logic_1164.all;
--定义实体
entity full_adder is
port(a,b,cin: in std_logic;
sum,carry: out std_logic
);
end entity;
architecture rtl of full_adder is
begin
sum<=a xor b xor cin;--求两个一位的和
carry<= (a and b) or (b and cin) or(cin and a);--计算进位值
end architecture;
----------------top.vhd------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity top is
port(A,B: in std_logic_vector(1 downto 0);
sum: out std_logic_vector(1 downto 0);
cout : out std_logic
);
end entity;
architecture structure of top is
---这部分完成元件声明
component half_adder is
port(A,B: in std_logic;
Sum,Carry:out std_logic
);
end component;
component full_adder is
port(A,B,Cin: in std_logic;
Sum,Carry: out std_logic
);
end component;
signal C: std_logic_vector(2 downto 0);
--这部分进行元件例化
begin
U0: half_adder port map(A=>A(0),B=>B(0),Sum=>sum(0),Carry=>C(0));
U1: full_adder port map(A=>A(1),B=>B(1),Cin=>C(0),Sum=>sum(1),Carry=>cout);
end structure;
注意:
1.对于元件例化部分的关联问题,要区分底层元件与顶层元件端口之间的关系,明确哪个量与哪个量之间的关系问题,这尤为重要。
2.要将top.vhd设置为顶层元件