Hi,
I was trying to write a constraint for the following scenario:
Assume that class pkt has a random property ‘id’.
On calling randomize() for the 1st object of ‘pkt’ type it randomized to value of X
Then post this irrespective of the no. of objects created for ‘pkt’ type followed by call to randomize(),
id should retain it’s value of X
I tried two approaches ::
(1) Using static random property ( which I wasn’t sure was legal )
class pkt;
static rand int id;
function void post_randomize();
id.rand_mode(0);
endfunction
endclass
module top;
pkt pkt1 , pkt2 , pkt3;
initial begin
pkt1 = new();
if(pkt1.randomize() ) $display("Success with %0p",pkt1);
pkt2 = new();
if(pkt2.randomize() ) $display("Success with %0p",pkt2);
pkt3 = new();
if(pkt3.randomize() ) $display("Success with %0p",pkt3);
end
endmodule
However to my surprise it works !!
IEEE Std 1800-2023 Syntax 18-1 mentions
class_property ::= // from A.1.9
{ property_qualifier } data_declaration
| ...
property_qualifier10 ::=
random_qualifier
| class_item_qualifier
random_qualifier10 ::= rand | randc
10) In any one declaration, only one of protected or local is allowed, only one of rand or randc is allowed,
and static and/or virtual can appear only once.
(Q1) So this means one could club local , randc & static as well, correct ?
(Q2) Is there a pre-defined order of these qualifiers ? i.e protected/local is to be written first followed by rand/randc and then static OR they can be written in any order ?
(2) My 2nd approach ::
class pkt;
static bit set;
rand int id;
constraint ID { if( set == 1 ) id == const'(id) ; }
function void post_randomize();
set = 1;
endfunction
endclass
// Same module top declaration as above
I observe o/p such as ::
# Success with -1022841503 1
# Success with 0 1
# Success with 0 1
(Q3) Why does ‘id’ change during 2nd call to randomize but remains same during 3rd call ?
I expected the o/p to be
# Success with -1022841503 1
# Success with -1022841503 1
# Success with -1022841503 1
Thanks in Advance