A trap of SystemVerilog inline constraint

In Constraint Random Verification(CRV), we may write the inline constraint with below format:

 

In most cases,  this works. After be randomized, the value of item.var1 is _something_. But have you ever thought about the result of below codes:

 

You will find that the  both item.var1 is not set to 10, but a random value. Why? This is because the ‘scope’. For inline constraint, it first looks up the var1 in item scope, thus it finds var1. The inline constraint equals to:

 

That means assign the value to itself. So the result is a random value.

To achieve our goal, there are two ways.

First is rename the local variable name from var1 to _var1.

The second is add “local::” contributor before the local variable var1. Shown as below.

 

 

Then the item.var1 will be set to 10 after randomization.

A summary: The priority of the scope in inline constraint is

 

Author: aquaporcus

上过大学读过研,进过华为创过业,敢追台风敢吸霾,娶了老婆就生娃,没车没房闯京城,哥有肩膀哥能扛!

Leave a Reply

Your email address will not be published. Required fields are marked *