Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomizing structure with typedefs

I am trying to randomize a structure which has typedefs, but it is not getting randomized. How can I achieve that?

typedef enum bit[1:0]{A=0, B=1,E=2,F=3}dl_pkt_type;
typedef enum bit[2:0]{G=4,H=5,C=6, D=7}tl_pkt_type;
  
  typedef struct {
    dl_pkt_type dl_pkt;
    tl_pkt_type tl_pkt;
  } pkt_struct;
    
class packet;

 rand pkt_struct p;
 rand dl_pkt_type dl;
 rand tl_pkt_type tl;
  //pre randomization function
  function void pre_randomize();
    $display("Inside pre_randomize");
  endfunction
  
  //post randomization function
  function void post_randomize();
    $display("Inside post_randomize");
    $display("dl: %0s   tl: %0s",p.dl_pkt,p.tl_pkt);
    $display("dl: %0s   tl: %0s",dl,tl);
  endfunction
endclass

module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
    pkt.randomize;
  end
endmodule

This is the output that I am getting:

Inside pre_randomize
Inside post_randomize
p.dl_pkt: A   p.tl_pkt: 
dl: B   tl: G

I am expecting p.dl_pkt and p.tl_pkt to be randomized.

like image 804
Anjali_287 Avatar asked Oct 18 '25 11:10

Anjali_287


1 Answers

You need to add the keyword rand to your struct, too:

  typedef struct {
    rand dl_pkt_type dl_pkt;
    rand tl_pkt_type tl_pkt;
  } pkt_struct;

https://www.edaplayground.com/x/BsLJ

typedef enum bit[1:0]{A=0, B=1,E=2,F=3}dl_pkt_type;
typedef enum bit[2:0]{G=4,H=5,C=6, D=7}tl_pkt_type;
  
  typedef struct {
    rand dl_pkt_type dl_pkt;
    rand tl_pkt_type tl_pkt;
  } pkt_struct;
    
class packet;

 rand pkt_struct p;
 rand dl_pkt_type dl;
 rand tl_pkt_type tl;
  //pre randomization function
  function void pre_randomize();
    $display("Inside pre_randomize");
  endfunction
  
  //post randomization function
  function void post_randomize();
    $display("Inside post_randomize");
    $display("dl: %0s   tl: %0s",p.dl_pkt.name,p.tl_pkt.name);
    $display("dl: %0s   tl: %0s",dl.name,tl.name);
  endfunction
endclass

module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
    pkt.randomize;
  end
endmodule
like image 167
Matthew Taylor Avatar answered Oct 22 '25 06:10

Matthew Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!