-1
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

In this code, I am getting a fatal error because nsid is not in the range of num_supp. Therefore, in the if condition, it tries to access an object which is not created (like format[32'hb235_44d5]).

solve nsid before b is also not working.

I can use that if condition outside of the randomize function and it may work, but what can be the solution of this problem in the randomize function?

2
  • Still have lots of errors. Have you looked at my answer and compared the differences?
    – dave_59
    May 8, 2015 at 4:48
  • @dave_59: Yes, I have found the errors. But more important, I have got my answer. Thanks a lot.
    – Karan Shah
    May 8, 2015 at 6:28

1 Answer 1

3

The problem SystemVerilog does not allow you to use an expression with a random variable as an index to an array. You need to put your constraint in terms of a foreach loop. Also - a solve before directive does not change the solution space, just the distribution of values selected as solutions.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.