gpt4 book ai didi

system-verilog - 使用 typedef 随机化结构

转载 作者:行者123 更新时间:2023-12-04 15:07:31 25 4
gpt4 key购买 nike

我试图随机化一个具有 typedef 的结构,但它没有随机化。我怎样才能做到这一点?

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
这是我得到的输出:
Inside pre_randomize
Inside post_randomize
p.dl_pkt: A p.tl_pkt:
dl: B tl: G
我在期待 p.dl_pktp.tl_pkt被随机化。

最佳答案

您需要添加关键字rand也到你的结构:

  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

关于system-verilog - 使用 typedef 随机化结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65823427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com