unique if
unique if 语句是并行的检查所有的条件,当超过一个条件满足、没有一个条件满足和没有与if像配对的else时,就会报错。
如下面的例子,有超过1个条件满足:
其输出结果为
priority if
与verilog中理解差不多,在没有条件满足和没有与if相配对的else语句会报错。
module priority_if;
//variables declaration
int a,b,c;
initial begin
//initialization
a=50;
b=20;
c=40;
priority if ( a < b ) $display("\t a is less than b");
else if ( a < c ) $display("\t a is less than c");
end
endmodule
其输出结果为:
RT Warning: No condition matches in 'priority if' statement.
do while
do begin
// statement -1
...
// statement -n
end
while(condition);
while
while(condition) begin
// statement -1
...
// statement -n
end
foreach
systemverilog使用它来遍历数组。
foreach(<variable>[<iterator>]]) begin
//statement - 1
...
//statement - n
end
module for_loop;
int a[4];
initial begin
$display("-----------------------------------------------------------------");
foreach(a[i]) a[i] = i;
foreach(a[i]) $display("\tValue of a[%0d]=%0d",i,a[i]);
$display("-----------------------------------------------------------------");
end
endmodule
输出结果为:
-----------------------------------------------------------------
Value of a[0]=0
Value of a[1]=1
Value of a[2]=2
Value of a[3]=3
for
与verilog相比,systemverilog允许在for循环内部进行循环变量的声明,而且可以多个初始化声明或者赋值。结构如下:
initialization:首先执行,并且只执行一次。可以用来声明和初始化循环控制变量。
Condition:用来评价。如果是真,整体的循环将会被执行。
modifier:在每次循环的最后将会被执行。
module for_loop;
initial begin
$display("---------------------------------------------------------");
for(int j=0, i=7; j<8; j++,i--) begin
$display("\t Value j = %0d Value of i = %0d", j, i);
end
$display("---------------------------------------------------------");
end
endmodule
输出结果为:
repeat
repeat语句会执行一些语句若干次。结构如下所示:
repeat(<variable>) begin
//statement - 1
...
//statement - n
end
module repeat_loop;
int a;
initial begin
$display("-----------------------------------------------------------------");
repeat(4) begin
$display("\tValue of a=%0d",a);
a++;
end
$display("-----------------------------------------------------------------");
end
endmodule
其输出结果为:
-----------------------------------------------------------------
Value of a=0
Value of a=1
Value of a=2
Value of a=3
-----------------------------------------------------------------
forever
会执行一些语句无限次。结构如下:
forever begin
//statement - 1
...
//statement - n
end
下面是一个例子:
module forever_loop;
int a;
initial begin
$display("-----------------------------------------------------------------");
forever begin
$display("\tValue of a=%0d",a);
a++;
#5;
end
$display("-----------------------------------------------------------------");
end
initial begin
#20 $finish;
end
endmodule
输出结果为:
-----------------------------------------------------------------
Value of a=0
Value of a=1
Value of a=2
Value of a=3
$finish called from file "testbench.sv", line 27.
$finish at simulation time 20