目录
2.2 SystemVerilog 'break' and 'continue'
2.3 SystemVerilog 'unique' and 'priority' if-else
2.5 Verilog Blocking & Non-Blocking
2.6.1@(event_name)与.triggered之间的区别是什么?
2.7.2 ANSI-C style declaration
2.7.3 Using declarations and directions
2 SystemVerilog Control Flow
2.1SystemVerilog Loops
2.1.1什么是循环?
循环是一遍又一遍的执行代码,当条件变为true时可以停止,如果循环永远进行,那模拟将无限期挂起。下表给出了Systemverilog中不同类型的循环结构:
2.1.2 forever
2.1.2.1语法
forever
// single statement
forever begin
// Multiple ststement
end
类似于下面Verilog中显示的代码。两者都运行无限的模拟时间,并且在其中存在一个延时元素很重要。
class Monitor;
virtual task run();
forever begin
@(posedge vif.clk);
if (vif.write & vif.sel)
if (!vif.write & vif.sel)
end
endtask
endclass
module tb;
Monitor mon;
initial begin
fork
mon.run();
join_none
end
endmodule
2.1.3 repeat
一组给定的语句可以使用构造执行 N 次。
module tb;
initial begin
repeat (5) begin
$display ("Repeat this statement");
end
end
endmodule
_______________________________________
Repeat this statement
Repeat this statement
Repeat this statement
Repeat this statement
Repeat this statement
_______________________________________
2.1.4 while与do while
两者都是循环构造,只要条件给定为真,他们就会执行给定的语句集合。
while:循环首先检查条件是否为真,如果条件为真,则执行语句。如果条件被证明是假的,那么循环就在那里结束
do while:循环首先执行一次语句,然后检查条件是否为真。如果条件为真,则执行语句集合,直到条件变为假。如果条件为假,那么循环再此结束。
因此两者之间的区别在于循环至少执行一次语句集。
2.1.4.1语法
while (<condition>) begin
// Multiple statements
end
_______________________________________
do begin
// Multiple statements
end while (<condition>);
_______________________________________
2.1.4.2示例
// while循环:
module tb;
initial begin
int cnt = 0;
while (cnt < 5) begin
$display("cnt = %0d", cnt);
cnt++;
end
end
endmodule
/*sumilation log
cnt = 0
cnt = 1
cnt = 2
cnt = 3
cnt = 4
*/
_______________________________________
module tb;
initial begin
int cnt = 0;
do begin
$display("cnt = %0d", cnt);
cnt++;
end while (cnt < 5);
end
endmodule
/*sumilation log
cnt = 0
cnt = 1
cnt = 2
cnt = 3
cnt = 4
*/
_______________________________________
module tb;
initial begin
int cnt = 0;
do begin
$display("cnt = %0d", cnt);
cnt++;
end while (cnt == 0);
end
endmodule
/*sumilation log
cnt = 0
*/
_______________________________________
2.1.5 for
SystemVerilog 中的循环多次重复一组给定的语句,直到不满足 给定的表达式。与所有其他过程块一样,循环需要其中的多个语 句被 and 关键字括起来。
2.1.5.1语法
For 循环使用三步法控制其语句的执行:
1. 初始化影响循环运行次数的变量
2. 在执行循环之前,请检查条件是否为 true
3. 修饰符在每次迭代结束时执行,并跳转到步骤 2
for ( [initialization]; <condition>; [modifier])
// Single statement
for ( [initialization]; <condition>; [modifier])
// Multiple statements
end
2.1.5.2 数组迭代
在此示例中,我们将遍历字符串数组并打印出其内容。数组 数组使用 5 个不同的水果名称进行初始化。循环初始化声明一个名为 i 的局部变量,该变量表示数组中任何元素的索引。条件表达式检查 i 是否小于数组的大小。修饰符递增 i 的值,以便循环的每次迭代都对不同的索引进行操作。
module tb;
string array [5] = '{'apple', 'orange', 'pear','blueberry','lemon'};
initial begin
for (int i = 0; i < $size(array); i++)
$display ("array[%0d] = %s", i, array[i]);
end
endmodule
//
array[0] = apple
array[1] = orange
array[2] = pear
array[3] = blueberry
array[4] = lemon
2.1.5.3多次初始化
module tb;
string array[5] = '{'apple', 'orange', 'pear','blueberry','lemon'};
initial begin
for(int i = 0,j = 2;i<$size(array);i++)begin
array[i][j] = "0";
$display("array[%0d] = %s,%0dth index repaced by 0",i,array[i],j);
end
end
endmodule
//
array[0] = ap0le, 2th index replaced by 0
array[1] = or0nge, 2th index replaced by 0
array[2] = pe0r, 2th index replaced by 0
array[3] = bl0eberry, 2th index replaced by 0
array[4] = le0on, 2th index replaced by 0
2.1.5.4添加多个修饰符
module tb;
string array[5] = '{'apple', 'orange', 'pear','blueberry','lemon'};
initial begin
for(int i = 0,j = array[i].len()-1;i<$size(array);i++,j--)begin
array[i][j] = "0";
$display("array[%0d] = %s,%0dth index repaced by 0",i,array[i],j);
end
end
endmodule
array[0] = apll0, 4th index replaced by 0
array[1] = ora0ge, 3th index replaced by 0
array[2] = pe0r, 2th index replaced by 0
array[3] = b0ueberry, 1th index replaced by 0
array[4] = 0emon, 0th index replaced by 0
2.1.6 foreach
SystemVerilog 数组是允许在单个变量中存储多个值的数据结 构。循环仅用于迭代此类数组,是最简单的方法。
2.1.6.1语法
循环从 0 开始遍历每个索引。如果循环中有多个语句,则必须 像所有其他过程块一样用 and 关键字将它们括起来
foreach(<variable>[<iterator>])
// Single statement
foreach(<variable>[<iterator>]) begin
// Multiple statements
end
2.1.6.2 一维数组
module