ch3-If else

2019-10-24

if-else

unique-if, unique0-if

unique-if evaluates conditions in any order and does the following :

  • report an error when none of the if conditions match unless there is an explicit else.
  • report an erorr when there is more than 1 match found in the if else conditions

Unlike unique-if, unique0-if does not report a violation if none of the conditions match

unique-if:有且只有一个分支匹配。

unique0-if:有一个或者零个分支被匹配。

No else block for unique-if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module tb;
int x = 4;

initial begin
// This if else if construct is declared to be "unique"
// Error is not reported here because there is a "else"
// clause in the end which will be triggered when none of
// the conditions match
unique if (x == 3)
$display ("x is %0d", x);
else if (x == 5)
$display ("x is %0d", x);
else
$display ("x is neither 3 nor 5");

// When none of the conditions become true and there
// is no "else" clause, then an error is reported
unique if (x == 3)
$display ("x is %0d", x);
else if (x == 5)
$display ("x is %0d", x);
end
endmodule

Simulation Log

1
2
3
4
5
6
7
8
ncsim> run
x is neither 3 nor 5
ncsim: *W,NOCOND: Unique if violation: Every if clause was false.
File: ./testbench.sv, line = 18, pos = 13
Scope: tb
Time: 0 FS + 1

ncsim: *W,RNQUIE: Simulation is complete.

priority-if

priority-if evaluates all conditions in sequential order and a violation is reported when:

  • None of the conditions are true or if there’s no else clause to the final if construct

第一次匹配成功立即退出if-else。

No else clause in priority-if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module tb;
int x = 4;

initial begin
// This if else if construct is declared to be "unique"
// Error is not reported here because there is a "else"
// clause in the end which will be triggered when none of
// the conditions match
priority if (x == 3)
$display ("x is %0d", x);
else if (x == 5)
$display ("x is %0d", x);
else
$display ("x is neither 3 nor 5");

// When none of the conditions become true and there
// is no "else" clause, then an error is reported
priority if (x == 3)
$display ("x is %0d", x);
else if (x == 5)
$display ("x is %0d", x);
end
endmodule

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!