VHDL子程序(Subprograms)

VHDL子程序(Subprograms)

VHDL语言提供了两个子程序工具:函数(functions)和过程(procedures)。
The VHDL language provides two subprogram facilities: functions and procedures.

函数(Function)

函数由一组顺序语句组成,并有一个返回值。
A function is a subprogram to group sequential statements and returns a value.

语法(Syntax)

[ 性质 ] function 函数名 [ ( 参数声明; ) ] return 类型名;     -- 声明
[ 性质 ] function 函数名 [ ( 参数声明; ) ] return 类型名 is
  [ 函数声明 ]
begin
  顺序语句
end [ function ] [ 函数名 ];                                                  -- 主体 

性质 = pure | impure
[ kind ] function function_name [ ( parameter_declaration; ) ] return type_name;     -- declaration
[ kind ] function function_name [ ( parameter_declaration; ) ] return type_name is
  [ function_declarations ]
begin
  sequential_statements
end [ function ] [ function_name ];                                                  -- body 

kind = pure | impure

说明(Description)

函数用来组织可执行的顺序语句来实现新的数学或逻辑函数。函数的特点在于他有一个指定类型的返回值。这也是他和过程的主要区别。
The function is used to group together executable, sequential statements to define new mathematical or logical functions. The important feature of functions is that they are used as expressions that return values of a specified type. This is the main difference from another type of subprograms: procedures, which are used as statements.
函数的类型可以是pure(默认值)或者impure。Pure函数对于相同实际参数集总是返回相同的值。Impure函数可能返回不同值,也就是有“副作用”,比如在函数外更新对象的值,这在pure函数中是不允许的。
Functions can be of type pure (which is default) or impure. Pure functions always return the same value for the same set of actual parameters. Impure functions may return different values for the same set of parameters, and may have “side effects”, like updating objects outside of their scope, this is not allowed for pure functions.
函数声明包含,名字,参数声明,返回类型。
A function declaration consists of the name, parameter declaration and type of the value returned by the function.
函数主体包含本地网格表子程序声明,类型,常量,变量,文件,断言,属性和组,以及实现算法的顺序语句。
A function body defines local declarations of nested subprograms, types, constants, variables, files, aliases, attributes and groups, as well as sequence of statements specifying the algorithm performed by the function.
函数声明是可选的,在函数主体中也有一个该部分的副本。但是,如果函数声明存在,那么函数主体不许也有在指定部分存在函数声明。
The function declaration is optional and the function body, which contains a copy of it, is sufficient for correct specification. However, if a function declaration exists, the function body declaration must appear in the given scope.
函数名可以是标识符也可以是运算符号。VHDL允许为已存在运算符指定新功能,这被叫做运算符重载。
The function name can be either an identifier or an operator symbol (if the function specifies the operator). VHDL allows the specification of new functions for existing operators this is called operator overloading.
函数参数被定义为输入,因此不要在函数声明中特别指定他为输入模式。只有常量,信号和文件可当做函数参数。
The parameters of the function are by definition inputs and thus do not need to have the mode (direction) specified in the function declaration. Only constants, signals and files can be function parameters.
函数主体包含函数描述的功能。函数主体包含两部分:声明和顺序语句。函数主体必须执行一个return语句。
The function body contains the functional description of the function. A function body consists of two parts: declarations and sequential statements. A function body must execute a return statement.

例程(Example)

function Func1 (signal A, B: in integer; MaxValue: integer) return integer is
  variable Temp, Max: integer;
begin
  Temp:= A + B;
  if Temp > MaxValue then
    Max:= MaxValue;
  else
    Max:= Temp;
  end if;
  return Max;
end Func1;

注释(Notes)

  • 函数可以被递归调用。
  • Functions can be called recursively.
  • 函数主体不能有wait语句或信号赋值。
  • A function body may not contain a wait statement or a signal
    assignment.

  • 函数主体不允许在包中声明

  • A function body is not allowed in a package declaration.
  • 子程序可以被分配网表资源。
  • Subprograms (functions and procedures) can be nested.

过程(Procedure)

过程是一组顺序语句。
A procedure is a subprogram to group sequential statements.

语法(Syntax)

procedure 过程名 [ ( 参数声明; ) ] ;      -- 声明
procedure 过程名 [ ( 参数声明; ) ] is
  [ 过程声明 ]
begin
  顺序语句
end [ procedure ] [ 过程名 ];            -- 主体
procedure procedure_name [ ( parameter_declaration; ) ] ;      -- declaration
procedure procedure_name [ ( parameter_declaration; ) ] is
  [ procedure_declarations ]
begin
  sequential_statements
end [ procedure ] [ procedure_name ];                          -- body

说明(Description)

过程用来组织可执行的顺序语句。过程有名字和一组参数。过程可以在结构体任何位置调用。过程定义包含过程声明和一个过程主体。
The procedure is used to group together executable, sequential statements. A procedure has a name and a set of parameters. Procedure can be called in any place of the architecture. The procedure definition consists of a procedure declaration and a procedure body.
过程声明包含一个过程名和过程调用时需要的参数列表。
The procedure declaration consists of the procedure name and parameter list required when the procedure is called.
过程主体定义了组成过程算法的顺序语句。过程中的声明仅对过程有效。
The procedure body defines the procedure’s algorithm composed of sequential statements. Declarations in a procedure are local to this declaration.
过程声明时可选的。过程主体可以没有声明。但是如果过程声明被使用,主体就必须和他相同。
The procedure declaration is optional. The procedure body can exist without a declaration. If, however, a procedure declaration is used, then a procedure body must accompany it.
过程重载就是相同名字的过程但是编号不同或者组成参数类型不同。实际参数决定调用哪个过程。
The overloaded procedures are procedures with the same name but with different number or different types of formal parameters. The actual parameters decide which overloaded procedure will be called.

例程(Example)

procedure Proc1 (signal Clk: in std_logic; 
                 Values: std_logic_vector; 
                 signal S1, S2: out std_logic;
                 variable V1, V2: out std_logic;
                 WaitTime: time := 10 ns) is
begin
  if rising_edge(Clk) then 
    S1 <= Values(0); 
    S2 <= Values(1); 
    V1 <= Values(2); 
    V2 <= Values(3);
    wait for WaitTime; 
  end if;
end Proc1;

注释(Notes)

  • 子程序会被分配网表。
  • Subprograms (procedures and functions) can be nested.
  • 子程序可以递归调用。
  • Subprograms can be called recursively.
  • 过程中只要没有内部寄存器或不包含wait语句,就是同步的。
  • Procedures are synthesizable, provided that they do not infer
    registers or contain wait statements.
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值