Systemverilog中的随机化顺序 类数据; int c=5; 末级 类配置c; 数据格式[]; rand int num_supp=5; 函数new(); 格式=新[num_supp]; foreach(格式[i]) 格式[i]=new(); 端功能 末级 类包; 兰德国际有限公司; 兰德国际公司; 兰德国际酒店; 末级 程序p; 分组p=新的; config_c conf=new; 初始开始 p、 用{nsid0; if(a

Systemverilog中的随机化顺序 类数据; int c=5; 末级 类配置c; 数据格式[]; rand int num_supp=5; 函数new(); 格式=新[num_supp]; foreach(格式[i]) 格式[i]=new(); 端功能 末级 类包; 兰德国际有限公司; 兰德国际公司; 兰德国际酒店; 末级 程序p; 分组p=新的; config_c conf=new; 初始开始 p、 用{nsid0; if(a,verilog,system-verilog,Verilog,System Verilog,在这段代码中,我得到了一个致命错误,因为nsid不在num\u supp的范围内。因此,在if条件下,它尝试访问未创建的对象(如格式[32'hb23544d5]) 在b之前解决nsid也不起作用 我可以使用随机化函数之外的if条件,它可能会工作,但是随机化函数中这个问题的解决方案是什么?问题SystemVerilog不允许使用带有随机变量的表达式作为数组的索引。您需要根据foreach循环设置约束。另外,-asolve before指令不会更改解空间,只会更改作为解选择的值的分布 class d

在这段代码中,我得到了一个致命错误,因为
nsid
不在
num\u supp
的范围内。因此,在if条件下,它尝试访问未创建的对象(如
格式[32'hb23544d5]

在b之前解决nsid
也不起作用


我可以使用
随机化
函数之外的if条件,它可能会工作,但是
随机化
函数中这个问题的解决方案是什么?

问题SystemVerilog不允许使用带有随机变量的表达式作为数组的索引。您需要根据foreach循环设置约束。另外,-asolve before指令不会更改解空间,只会更改作为解选择的值的分布

class data_s;
  int c=5;
endclass

class config_c;
  data_s format[];
  rand int num_supp = 5;

  function new();
    format = new[num_supp];
    foreach(format[i])
      format[i] = new();
  endfunction
endclass

class packet;
  rand int nsid;
  rand int a;
  rand int b;  
endclass

  program p;
     packet p = new;
     config_c conf = new;

     initial begin
       p.randomize() with {nsid < (conf.num.supp + 1);
                      nsid > 0;
                      if(a < conf.format[nsid - 1].c)
                        b=0;
                      else
                        b=1;
                     } 
     end
 endprogram
类数据;
位[31:0]c=5;
末级
类配置;
数据格式[];
rand int num_supp=5;
函数new();
格式=新[num_supp];
foreach(格式[i])
格式[i]=new();
端功能
末级
类包;
随机位[31:0]nsid;
随机位[31:0]a,b;
末级
模块顶部;
分组p=新的;
//一些其他的东西
Config conf=new();
初始开始
p、 用{nsid<(conf.num_supp+1)随机化();
foreach(conf.format[i])
i==(nsid-1)->
if(a
仍然有很多错误。你看过我的答案并比较了差异吗?@dave_59:是的,我发现了错误。但更重要的是,我已经得到了答案。谢谢。
class data_s;
  bit [31:0] c=5;
endclass

class Config;
  data_s format[];
  rand int num_supp = 5;

  function new();
    format = new[num_supp];
    foreach(format[i])
      format[i] = new();
  endfunction
endclass

class packet;
  rand bit [31:0] nsid;
  rand bit [31:0] a,b;
endclass

module top;

   packet p = new;

   // Some other stuff
Config conf=new();
initial begin
  p.randomize() with {nsid < (conf.num_supp + 1);
                      foreach (conf.format[i])
              i == (nsid -1) ->
                      if(a < conf.format[i].c)
                        b==0;
                      else
                        b==1;
                      };
   $display("%p",p);
   end

endmodule