Tutorials
Learn More
randcase in SystemVerilog
Randcase is a case statement that randomly selects one of its branch statements based on the probability of each statement.
Syntax:
randcase
item 1: <statement 1>;
item 2: <statement 2>;
item 3: <statement 3>;
...
…
item n: <statement n>;
endcase
How to calculate the probability of each branch statement?
The probability of any item is calculated by the value of an item divided by the sum of all item values.
Ex: P(item1) = item1/sum(item 1..n)
randcase can also be written inside the module.
randcase Example
class seq_item;
int cnt_arr[int];
real i_sum;
function void randcase_testing();
repeat(10) begin
randcase
2: begin $display("Selected 2"); cnt_arr[2]++; end
3: begin $display("Selected 3"); cnt_arr[3]++; end
5: begin $display("Selected 5"); cnt_arr[5]++; end
7: begin $display("Selected 7"); cnt_arr[7]++; end
endcase
end
foreach(cnt_arr[i]) begin
i_sum += i;
end
foreach(cnt_arr[i]) begin
$display("Probability for %0d = %0f in 1 iteration", i, i/i_sum);
$display("cnt_arr[%0d] = %0d", i, cnt_arr[i]);
$display("Probability for cnt_arr[%0d] = %0f", i, cnt_arr[i]/10.0);
$display("---------------------------------");
end
endfunction
endclass
module constraint_example;
seq_item item;
initial begin
item = new();
item.randcase_testing();
end
endmodule
Output:
Selected 7
Selected 5
Selected 2
Selected 2
Selected 3
Selected 7
Selected 7
Selected 7
Selected 7
Selected 5
Probability for 2 = 0.117647 in 1 iteration
cnt_arr[2] = 2
Probability for cnt_arr[2] = 0.200000
---------------------------------
Probability for 3 = 0.176471 in 1 iteration
cnt_arr[3] = 1
Probability for cnt_arr[3] = 0.100000
---------------------------------
Probability for 5 = 0.294118 in 1 iteration
cnt_arr[5] = 2
Probability for cnt_arr[5] = 0.200000
---------------------------------
Probability for 7 = 0.411765 in 1 iteration
cnt_arr[7] = 5
Probability for cnt_arr[7] = 0.500000
---------------------------------
The probability of item increases with 2, 3, 5,7.
Probability of 2: 0.117647
Probability of 3: 0.176471
Probability of 5: 0.294118
Probability of 7: 0.411765
System Verilog Tutorials