ljepson74

randomize() with inside syntax

4 posts in this topic

How can I use "randomize() with" along with "inside", on the same line?

 

Below is some code that solves the problem using >= and <=, but I'd like to use "inside".

module top;

class aclass;
   int index;

   function void get_latency;
      //assert (randomize(index) with {index inside {[1:5]}}) else begin  //WHAT IS THE PROPER SYNTAX?
      //assert (randomize(index) inside {[1:5]}) else begin               //WHAT IS THE PROPER SYNTAX?
      assert (randomize(index) with {index<=5;  index>=1;}) else begin    //WORKS
         $display("ERROR:  We failed!");
         $finish;
      end

      $display("RESULT: index=%0d",index);
   endfunction
endclass

   initial begin
      aclass a_class=new();
      a_class.get_latency();
      $finish;
   end
endmodule

I often grapple with the "randomize with" syntax, getting confused with squiggly brackets and semicolons, and refer to the LRM.  (Any tips that will stick in my head are welcome.)

 

Share this post


Link to post
Share on other sites

The syntax is

assert (
         randomize(index) with 
                               { index inside { [1:5] } ;  }  
       ) else begin

It's the same {} as if you wrote named constraint block. Each constraint within the {} needs to be terminated with a semi-colon

constraint range_constraint
                               { index inside { [1:5] } ;  } 

           

ljepson74 likes this

Share this post


Link to post
Share on other sites
Thanks a lot, dave_59.   Seeing those two (the inline and the constraint block) lined up like you did will help this stick in my head that they are the same syntax.

Share this post


Link to post
Share on other sites

Also, you might want to keep your randomize() call outside the assert().  Otherwise, simulators may not call the randomize at all if you disable assertions (e.g. to temporarily work around a problem).  You can assign the return from randomize() to a variable and then assert that variable.

tudor.timi and ljepson74 like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now