Systemverilog 中的随机顺序

Randomization Order in Systemverilog

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

在这段代码中,我遇到了一个致命错误,因为 nsid 不在 num_supp 的范围内。因此,在 if 条件下,它会尝试访问未创建的对象(如 format[32'hb235_44d5])。

solve nsid before b 也不起作用。

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

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

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