Tasks and Functions
本文最后更新于:Wednesday, September 30th 2020, 8:00 pm
Tasks and Functions
1、What are they good for
Tasks and functions provide the ability to execute common procedures from several different places in a description. They also provide a means of breaking up large procedures into smaller ones to make it easier to read and debug the source descriptions.
从std 1364-2005的这段描述中,我们可以总结出以下用处:
- 减少重复工作,代码复用。
- 把大的过程拆解成几个小的任务和函数,更加易读和找bug
2、Distinctions between tasks and functions
A function shall execute in one simulation time unit; a task can contain time-controlling statements.
函数应该瞬间返回结果,不能有延时;而任务可以带有延时控制语句。
A function cannot enable a task; a task can enable other tasks and functions.
函数不能调用任务;但是任务可以调用其他任务或者函数。
A function shall have at least one input type argument and shall not have an output or inout type argument; a task can have zero or more arguments of any type.
函数应该至少有一个
输入参数并且不能有输出或者输入输出参数。任务可以有零个或者任意个任何种类的参数。
A function shall return a single value; a task shall not return a value.
函数应该返回一个单一的值;任务不能返回值(只能通过把处理放在参数上带回)
The purpose of a function is to respond to an input value by returning a single value. A task can support multiple goals and can calculate multiple result values. However, only the output or inout type arguments pass result values back from the invocation of a task. A function is used as an operand in an expression; the value of that operand is the value returned by the function.
函数的目的:对于输入给出一个返回结果
任务支持多个结果,但是只有
输出或者输入输出参数能够传递这种结果函数可以在表达式中用作一个操作数。(其值就是函数返回的结果。是不是很像C)
例如:
1
2
3switch_bytes(old_word, new_word); //task会把运算结果放在new_word上
new_word = switch_bytes(old_word); //function会直接把结果返回。| 比较点 | tasks | functions |
| ——————— | ———————————————————————- | ———————————————————————————— |
| 输入输出 | 任意多输入输出(input,output,inout) | 至少一个输入不能有输出 |
| 触发事件控制 | 可以包含延时控制语句(#), 只能面向仿真,不能综合 | 不能出现always, # 等语句,函数应该在一个时间单元内返回值 |
| 返回值 | 通过输出端口传递 | 通过函数名返回(only one) |
| 中段 | 可以由disable中断 | 不能 |
| 语句 | | |
| 调用其他 | 可以调用task和function | 只能调用function,不能调用task |
| 其他说明 | task调用可以作为完整的语句出现 | function调用只能作为赋值语句右边的operand |
3、 Tasks and task enabling
A task shall be enabled from a statement that defines the argument values to be passed to the task and the variables that receive the results. Control shall be passed back to the enabling process after the task has completed. Thus, if a task has timing controls inside it, then the time of enabling a task can be different from the time at which the control is returned. A task can enable other tasks, which in turn can enable still other tasks—with no limit on the number of tasks enabled. Regardless of how many tasks have been enabled,control shall not return until all enabled tasks have completed.
- 调用task应该给出
传进参数的值,和接受结果的变量。 - task如果有延时控制,那么task被调用时刻和控制返回时刻可以不同。
- task内可以再调用task,直到所有的task完成才会返回控制到顶层调用处。
3.1、task declaration syntaxes
1 | |
Tasks without the optional keyword automatic are static tasks, with all declared items being statically allocated. These items shall be shared across all uses of the task executing concurrently. All items declared inside automatic tasks are allocated dynamically for each invocation Automatic task items cannot be accessed by hierarchical references. Automatic tasks can be invoked through use of their hierarchical name.(automatic的坑后面再填)
3.2、Task enabling and argument passing
1 | |
- If the argument is declared as an output or an inout, then the expression shall be restricted to an expression that is valid on the left-hand side of a procedural assignment.(输出参数:能位于
过程赋值语句左边的有效表达式。不能是wire)
- reg, integer, real, realtime, and time variables
- Memory references
- Concatenations of reg, integer, and time variables
- Concatenations of memory references
- Bit-selects and part-selects of reg, integer, and time variables
All arguments to the task shall be passed by value rather than by reference
任务参数的是
值传递不是引用(指针)If an argument in the task is declared as an input, then the corresponding expression can be any expression.The order of evaluation of the expressions in the argument list is undefined.
如果参数被定义为input,那么调用语句与之相应位置的表达式没有限制,表达式求值过程是没有被定义的。(求值顺序没有先后)
1 | |
1 | |
一种是参数列表在函数名后定义。一种是在task里面定义。
my_task (v, w, x, y, z); //调用语句
The task-enabling arguments ( v , w , x , y , and z ) correspond to the arguments ( a , b , c , d , and e ) defined by the task.
当函数调用时:
a = v;
b = w;
c = x;
When the task completes, the following assignments to return the computed values to the
calling process are performed:(当函数结束后,下面的赋值返回对应的结果)
x = c;
y = d;
z = e;
task enable statement中参数位置要和task definition 对应。不管你是哪种方法定义task
3.3、Task memory usage and concurrent 🤕(可以跳过)
A task may be enabled more than once concurrently. All variables of an automatic task shall be replicated on each concurrent task invocation to store state specific to that invocation.All variables of a static task shall be static in that there shall be a single variable corresponding to each declared local variable in a module instance, regardless of the number of concurrent activations of the task. However, static tasks in different instances of a module shall have separate storage from each other.
4、Disabling of named blocks and tasks
The disable statement provides the ability to terminate the activity associated with concurrently active procedures, while maintaining the structured nature of Verilog HDL procedural descriptions.(disable 语句可以终止正在运行的过程)
1
2
3
4
5begin : block_name
rega = regb;
disable block_name;
regc = rega; // this assignment will never execute
end1
2
3
4
5
6
7begin : block_name
...
if (a == 0)
disable block_name;
...
end // end of named block
// continue with code following named block1
2
3
4
5
6
7
8
9
10task proc_a;
begin
...
...
if (a == 0)
disable proc_a; // return if true(控制权交回调用语句)
...
...
end
endtask1
2
3
4
5
6
7
8
9
10
11
12
13
14begin : break
for (i = 0; i < n; i = i+1) begin : continue
@ clk
if (a == 0) // "continue" loop
disable continue; //(相当于c里面的break)
statements
statements
@ clk
if (a == b) // "break" from loop
disable break; // (相当于c里面的break)
statements
statements
end
end1
2
3
4
5
6
7
8fork
begin : event_expr
@ ev1;
repeat (3) @ trig;
# d action (areg, breg); //(只有当ev1出现,trig出现3次才触发)
end
@ reset disable event_expr; //(同上面并行进行)
join1
2
3
4
5
6
7always begin : monostable
# 250 q = 0;
end
always @ retrig begin //(只要retrig变化间隔小于250,那么q就一直为1)
disable monostable;
q = 1;
end5、Functions and function calling
5.1、Function declarations
1 | |
1 | |
1 | |
5.2、Returning a value from a function
The function definition shall implicitly declare a variable, internal to the function, with the same name as the function. This variable either defaults to a 1-bit reg or is the same type as the type specified in the function declaration. The function definition initializes the return value from the function by assigning the function result to the internal variable with the same name as the function.
函数定义就隐含了一个变量(即函数名)
函数通过给这个隐含变量赋值返回值。
5.3、Calling a function
1 | |
1 | |
5.4、Function rules
A function shall not have any nonblocking assignments or procedural continuous assignments.
函数不能有
非阻塞赋值或者过程连续赋值(在一个时间单位内完成)A function shall not have any event triggers.
函数不能有任何
时间触发
1 | |
5.5、 Use of constant functions🤕(可以跳过)
Constant function calls are used to support the building of complex calculations of values at elaboration time
常量函数调用用于支持在精化阶段构建复杂的值计算
🙂o_o ….待填坑。
Renference
[IEEE Std 1364™-2005下载🔗]:链接:https://pan.baidu.com/s/1ryz4IAuQzNPnGifUJ8oAGw
提取码:2qvb
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!