感觉吧,实例化就是把已经写好的元件在新的元件中调用,从而组成更大的元件。
在vhdl设计中常常将常用、典型的功能实体放在同一个目录下面,在设计复杂的电路的时候使用这些已经设计好的实例,这样就使设计变得简单了。
1.元件声明是对VHDL模块(即底层设计,也是完整的VHDL设计)的说明,使之可在其他被调用,元件声明可放在程序包中,也可在某个设计的构造体中声明。
元件例化指元件的调用。元件声明及元件例化的语法分别如下:
元件声明:
component〈元件实体名〉
prot(〈元件端口信息,同该元件实现时的实体的port部分〉);
end compnent;
元件例化:
〈例化名〉:〈实体名,即元件名〉port map(〈端口列表〉);
例如,在一个设计中调用一个模为10的计数器cntm10和一个七段译码器decode47构成如下电路,则该调用过程孥即元件例化。
VHDL描述如下:
library ieee;
use ieee.std_logic_1164.all;
entity cntvh10 is
port (rd, ci, clk : in std_logic;
co : out std_logic;
qout : out std_logic_vector (6 downto 0));
end cntvh10;
architecture arch of cntvh10 is
Component decode47 is
port (adr : in std_logic_vector (3 downto 0);
decodeout : out std_logic_vector (6 downto0));
end Component;
2.元件例化
Component cntm10 is
Port ( ci : in std_logic;
nreset : in std_logic;
clk : in std_logic;
co : out std_logic;
qcnt : buffer std_logic_vector (3 downto 0));
end Component;
signal qa: std_logic_vector (3 downto 0);
begin
u1 : cntm10 port map (ci, rd, clk, co, qa); —元件例化
u2 : decode47 port map ( decodeout=>qout, adr=>qa);
end arch;
元件例化时的端口列表可按位置关联方法,如u1,这种方法要求的实参(该设计中连接到端口的实际信号,如ci,等)所映射的形参(元件的对外接口信号)的位置同元件声明中的一样;元件例化时的端口列表也可按名称关联方法映射实参与形参,如u2。格式为(形参1=>实参1,形参2=>实参2,···)。这种方法与位置无关。
参数化元件可增加元件例化的灵活性。所谓参数化元件是指元件的规模(或特性)可以通过引用参数的形式指定的一类元件。例如,下面定义了一个位数可调的计数器:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
ues ieee.std_logic_unsigned.all;
entity cntnbits is
generic (cntwidth:integer:=4); —定义了一个可调参数
port ( ci : in std_logic;
nreset : in std_logic;
clk : in std_logic;
co : out std_logic;
qcnt : buffer std_logic_vector (3 downto 0));
end cntmbits;
architecture behave of antnbits is
constant allis1 : std_logic_vector (cntwidth-1 downto 0) := (others=>’1’);
begin
Co <=’1’ when (qcnt=all is 1 and ci=’1’) else’0’;
Process (clk)
Begin
if (nreset=’0’) then
Qcnt<= (others=>’0’);
elsif (clk’event and clk=’1’) then
if (ci=’1’) then
Qcnt<=qcnt+1;
end if;
end if;
end process;
end behave;
可以算出,该计数器同第1部分的四位计数器相比,改动不大,其中在实体处增加了一行:
generic (cntwidth:integer:=4)
该行定义了一个整数cntwidth(计数宽度)并赋初值‘4’,用它代替原来的固定的计数器长度,若想设计的计数器位数8位,仅需将cntwidth初值赋为8:
generic (cntwidth : integer :=8);
若以此计数器为元件,则元件声明为:
Component cntnbit is
generic (cntwidth:integer:=4);
port ( ci : in std_logic;
nreset : in std_logic;
clk : in std_logic;
co : out std_logic;
qcnt : buffer std_logic_vector (cntwidth-1 downto 0));
end Component;
下面基础元件是一个十进制计数器,然后用三个计数器组成了一个更大的计数器。
看代码:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity cnt10 is
port(clk,clr,en:in std_logic;
q:out std_logic_vector(3 downto 0);
co:out std_logic);
end entity;
architecture rtl of cnt10 is
signal tmp:std_logic_vector(3 downto 0);
begin
process(clk,clr,en)
begin
if clr='1' then
tmp<="0000";
elsif rising_edge(clk) then
if en='1' then
if tmp="1001" then
tmp<="0000";
else
tmp<=tmp+'1';
end if;
end if;
end if;
end process;
process(tmp)
begin
if tmp="0000" then
co<='1';
else
co<='0';
end if;
end process;
q<=tmp;
end;
生成的rtl文件如下:
然后是顶层代码:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity watch is
port(clk0,clr0,en0:in std_logic;
dataout:out std_logic_vector(11 downto 0));
end entity;
architecture rtl of watch is
component cnt10 is
port(clk,clr,en:in std_logic;
q:out std_logic_vector(3 downto 0);
co:out std_logic);
end component;
signal co1:std_logic;
signal co2:std_logic;
begin
U1:cnt10 port map(clk0,clr0,en0,dataout(3 downto 0),co1);
U2:cnt10 port map(co1, clr0,en0,dataout(7 downto 4),co2);
U3:cnt10 port map(co2, clr0,en0,dataout(11 downto 8));
end;
生成的rtl文件如下:
因为通常芯片用的晶振为50Mhz,所以肯定也是需要分频的,这次就没分频了,练习嘛