0% found this document useful (0 votes)
23 views7 pages

Randsequence in SystemVerilog

Uploaded by

Anindita Dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Randsequence in SystemVerilog

Uploaded by

Anindita Dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

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

module rand_sequence4a();

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) incr_one else incr_two;
incr_one : {one_1 += 2; one_1--;};
incr_two : {two_2 += 2; 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_sequence4a

//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

The repeat production statement is used to iterate a production over a specified number
of times.

module rand_sequence6();

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 | repeat(2) two | repeat(3) three ;
one : {one_1 ++; };
two : {two_2 ++; };
three: {three_3 ++; };
endsequence
end
$display(" one %d \n two %d \n three %d",one_1,two_2,three_3);
end
endmodule : rand_sequence6

//Output:
// one 2059
// two 4072
// three 5715
The next way to generate a sequence of transactions is by using the randsequence
construct in SystemVerilog. With randsequence you describe the grammar of the
transaction, using a syntax similar to BNF (Backus-Naur Form).
Example 6-53 Command generator using randsequence
initial
begin
for (int i=0; i<15; i++) begin
randsequence (stream)
stream : cfg_read := 1 | io_read := 2 | mem_read := 5;
cfg_read : { cfg_read_task; } | { cfg_read_task; } cfg_read;
mem_read : { mem_read_task; } | { mem_read_task; } mem_read;
io_read : { io_read_task; } | { io_read_task; } io_read;
endsequence end // for end
task cfg_read_task; ... endtask

You might also like