
--wait until clk='0' 可以替代clk'event and clk='1'做下降沿。具体用法如下:
--74hc161功能芯片的VHDL程序:
Library ieee;
use ieee.std_logic_11.all;
use ieee.std_logic_unsigned.all;
entity d_ff is
Port(clk,cr,ld:in std_logic;
d:in std_logic_vector(3 downto 0);
q:out std_logic_vector(3 downto 0));
end;
architecture dd of d_ff is
signal s,ss:std_logic_vector(3 downto 0);
begin
process
begin
wait until clk='1'; --这句意思是等待到clk等于1时完成下面语句
if ld='0' then s<=d;
else s<=s+1 ;
end if;
if cr='0' then
s<="0000";
end if;
end process;
q<=s;
end dd;
2--子程序也是很多编程语言中常用的程序模块,VHDL子程序用法如下:
1)---------------子程序function的用法---------------------
Library ieee;
use ieee.std_logic_11.all;
use ieee.std_logic_unsigned.all;
entity full_adder is
Port(a,b,cin:in std_logic;
sum,co:out std_logic);
end;
architecture add of full_adder is
Function sam1(x,y:std_logic)return std_logic is --子程序1,sam1为程序名
begin --x,y为子程序变量,冒号后面是变量类型
return (x xor y); --子程序返回值
end sam1; --结束子程序1
function sam2(x,y:std_logic)return std_logic is --子程序2
begin
return (x and y);
end sam2; --结束子程序1
signal s1,co1,s2,co2,ss:std_logic;
begin
process(a,b,cin)
begin
s1<=sam1(a,b); --调用子程序1
co1<=sam2(a,b); --调用子程序2
s2<=sam1(s1,cin);
co2<=sam2(s1,cin);
end process;
sum<=s2;
co<=co1 or co2;
end dd;
2)---------------子程序Procedure的用法---------------------
Library ieee;
use ieee.std_logic_11.all;
use ieee.std_logic_unsigned.all;
entity d_ff is
Port(a,b,cin:in std_logic;
sum,co:out std_logic);
end;
architecture dd of d_ff is
---------------------------------------
Procedure sam1(x,y:in std_logic;
z: out std_logic) is --这个子程序用法变量必须设置方向
begin
z:=(x xor y); --赋值必须采用变量Veriable格式:=
end sam1;
----------------------------------------
Procedure sam2(x1,y1:in std_logic;
z1: out std_logic) is
begin
z1:=(x1 and y1);
end sam2;
-----------------------------------------
begin
process(a,b,cin)
variable s1,s2,co1,co2:std_logic;--运行过程中需要的话,必须使用局部变量,在Process后设置。
begin
sam1(a,b,s1);
sam2(a,b,co1);
sam1(s1,cin,s2);
sam2(s1,cin,co2);
sum<=s2;
co<=co1 or co2;
end process;
end dd;
