函数的用法
函数首:
FUNCTION 函数名(参数表)RETURN 数据类型;
函数体
FUNCTION 函数名(参数表)RETURN 数据类型 IS
[说明部分];
BEGIN
return 顺序语句;
END 函数名;
注意:参数表中参数一般是输入参数或输入信号,函数调用时参数按常数处理
如果将一个已编制好的函数并入程序包,函数首必须放在程序包的说明部分,而函数体需放在程序包的包体内。如果只在一个结构体中定义并调用函数,则仅需函数体。
例:
library ieee;
use ieee.std_logic_1164.all;
entity func is
port ( a : in bit_vector (0 to 2) ;
m : out bit_vector (0 to 2));
end func;
architecture demo of func is
function sam (x, y, z : bit) return bit is
begin
return (x and y) or y;
end sam;
begin
process (a)
begin
m(0) <= sam ( a(0), a(1), a(2) );
m(1) <= sam ( a(2), a(0), a(1) );
m(2) <= sam ( a(1), a(2), a(0) );
end process;
end demo;
过程
过程(PROCEDURE)与函数一样,也有两部分组成,过程首和过程体
PROCEDURE 过程名(参数表);
PROCEDURE 过程名(参数表)IS
[定义语句]; --定义变量、常量、数据类型
BEGIN
[顺序语句];
END 过程名;
过程参数表中的参数需要IN、OUT、INOUT定义其工作模式。
如果一个过程是在进程中调用,且这个进程已列出敏感参数表,则不能在此过程中使用WAIT语句。
例:
procedure and2 ( x, y : in bit; O : out bit); -- 过程首
procedure and2 ( x, y : in bit; O : out bit) is -- 过程体
begin
if x =‘1’ and y=‘1’ then O<= ‘1’;
else O<=‘0’;
end if ;
end and2;
过程调用:
signal a : bit := ‘1’;
signal b : bit :=‘0’;
signal c : bit;
……
And2 ( a , b , c); -- 过程调用时,注意形参和实参数据类型的一致性
函数和过程的异同点
异:
过程常用来定义一个算法,而函数则常用来产生一个特定的返回值;
过程中参数的端口模式一般是IN、OUT或INOUT,而函数中参数的模式只能是IN。
过程中可以有WAIT语句(但综合器一般不支持),函数中不能;
过程有多个返回值,函数只有一个返回值;
函数调用语句只能用在赋值语句或表达式中,过程调用语句可独立存在。
同
函数和过程中都必须是顺序语句;
函数和过程的定义语句中都不能定义信号。
结构体的描述方法
行为描述:
描述输入与输出之间的转换行为,不包含内部的电路元件、电路的结构信息,一般将结构体命名为“behav”
begin
process ( x, y, ci)
variable n: integer;
constant sum_vector : std_logic_vector (0 to 3):=“0101”;
constant carry_vector : std_logic_vector(0 to 3):=“0011”;
begin
n:=0;
if x= ’1’ then n:=n+1; end if;
if y= ‘1’ then n:=n+1; end if;
if ci= ’1’ then n:=n+1; end if;
s <= sum_vector (n);
co <= carry_vector (n);
end process;
end behav;
数据流描述:
既表示行为,又隐含着结构;体现数据的流动路径和方向,一般将结构体命名为“dataflow”
begin
s <= x XOR y XOR ci;
co <= (x AND y) OR (x AND ci) OR (y AND ci);
end dataflow;
结构描述法:
描述电路元件与它们之间的连接关系,一般将结构体命名为“stru数据类型(元件例化语句)
Architecture structure of full_adder is
Signal temp_sum: bit; -- 定义语句
Signal temp_carry1: bit;
Signal temp_carry2: bit;
Component half_adder
Port (X,Y: in bit; sum, carry: out bit);
End component;
Component or_gate
Port ( in1,in2: in bit; out1: out bit);
End component;
Begin--并行语句
U0 : half_adder
Port map (X=>A, Y=>B, sum=>temp_sum, carry=>temp_carry1);
U1 : half_adder
Port map (X=>temp_sum, Y=>carry_in, sum=>AB, carry=>temp_carry2);
U2 : or_gate
Port map (in1=>temp_carry1, in2=>temp_carry2, out1=>carry_out);
End structure;