Randsequence in SystemVerilog

The random sequence generator is useful for randomly generating structured sequences of stimulus such as instructions or network traffic patterns.
By randomizing a packet, it will generate most unlikely scenarios which are not interested. These type of sequence of scenarios can be generated using randsequence.
 

Uses of Randsequence in SV:
 
  • Generation of random values: The randsequence can be used to generate sequences of random numbers, events, or values within a defined range or according to specific constraints.
  • Flexible and reusable: You can generate and manipulate the random sequences to model specific scenarios in your verification environment.
  • Combinatorial and Sequential constraints: You can apply constraints to randsequence to restrict the sequence behavior (e.g., the sequence might need to be in a certain order, have certain properties, etc.).


randsequence is composed of one or more productions.
Each production contains a name and one or more production_list.
Production_list contains one or more production_item.
Production_items of production_list are further classified into terminals and non-terminals.
Non-terminals are defined in terms of terminals and other non-terminals.
A terminal is an indivisible item that needs no further definition than its associated code block.
Ultimately, every non-terminal is decomposed into its terminals.

module rand_sequence1();
initial begin
repeat(5) begin
randsequence( main )
main : one two three ;
one : {$write(“one”);};
two : {$write(” two”);};
three: {$display(” three”);};
endsequence
end
end
endmodule : rand_sequence1

// Here production is “main”.
// production “main” contain only one production_list with 3 production_items named “one”, “two”, “three”.
// production_items “one”, “two”, “three” are terminals.
// When the “main” is chosen, it will select the sequence “one”, “two” and “three” in order.

//Output:
// one two three
// one two three
// one two three
// one two three
// one two three


A single production can contain multiple production lists.
multiple production lists are separated by the | symbol.
Production lists separated by a | imply a set of choices, which the generator will make at random.

module rand_sequence2();
initial begin
repeat(8) begin
randsequence( main )
main : one | two | three ;
one : {$display(“one”);};
two : {$display(“two”);};
three: {$display(“three”);};
endsequence
end
end
endmodule : rand_sequence2

// Here “main” is production,
// “main” has 3 production_list, each production_list consist 1 production_item,
// 1st production_list has a production_item called “one”,
// 2nd production_list has a production_item called “two”,
// 3rd production_list has a production_item called “three”,
// production_items “one”, “two”, “three” all are terminals.

//Output:
// three
// three
// one
// two
// three
// two
// two
// one

//Results show that one, two and three are selected randomly.

By default procution_list is generated randomly, you can give probability for a production_list generation.
The probability that a production list is generated can be changed by assigning weights to production lists.
The probability that a particular production list is generated is proportional to its specified weight.


The := operator assigns the weight specified by the weight_specification to its production list.
A weight_specification must evaluate to an integral non-negative value.
Weight expressions are evaluated when their enclosing production is selected, thus allowing weights to change dynamically.

module rand_sequence3();
integer one_1,two_2,three_3;
initial begin
one_1 = 0;
two_2 = 0;
three_3 = 0;
repeat(6000) begin
randsequence( main )
main : one := 1 | two := 2 | three := 3;
one : {one_1++;};
two : {two_2++;};
three: {three_3++;};
endsequence
end
$display(” one %0d \n two %0d \n three %0d”,one_1,two_2,three_3);
end
endmodule : rand_sequence3

//Output:
// one 1044
// two 1970
// three 2986

A production can be made conditional by means of an if..else production statement.
The expression can be any expression that evaluates to a boolean value. If the expression evaluates to true, the production following the expression is generated, otherwise the production following the optional else statement is generated.

module rand_sequence4();
integer one_1,two_2,three_3;
reg on;
initial begin
on = 0;
one_1 = 0;
two_2 = 0;
three_3 = 0;
repeat(2500) begin
randsequence( main )
main : one three;
one : {if(on) one_1++; else two_2 ++; };
three: {three_3++;};
endsequence
end
$display(” one %0d \n two %0d \n three %0d”,one_1,two_2,three_3);
end
endmodule : rand_sequence4

//Output:
// one 0
// two 2500
// three 2500

A production can be selected from a set of alternatives using a case production statement.
The case expression is evaluated, and its value is compared against the value of each case-item expression, which are evaluated and compared in the order in which they are given.


The production associated with the first case-item expression that matches the case expression is generated.
If no matching case-item expression is found then the production associated with the optional default item is generated, or nothing if there no default item.
Case-item expressions separated by commas allow multiple expressions to share the production.

module rand_sequence5();
integer one_1,two_2,three_3;
initial begin
for(int i = 0 ;i < 10 ;i++)
begin
randsequence( main )
main : case(i%3)
0 : zero;
1, 2 : non_zero;
default : def;
endcase;
zero : {$display(“zero”);};
non_zero : {$display(“non_zero”);};
def : {$display(“default”);};
endsequence
end
end
endmodule : rand_sequence5

//Output:
// zero
// non_zero
// non_zero
// zero
// non_zero
// non_zero
// zero
// non_zero
// non_zero
// zero

 

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top