Jump to content

Recommended Posts

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] } ;  } 

           

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...