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


SV DPI Scope

SV的DPI(Directed Program Interface)提供了SV与其他语言(C/C++)通信的桥梁。本文不会讲解DPI的基础知识,只针对Scope这一概念进行解释,因为这是在DPI编程中常遇到的,也是比较难debug的问题。

对于SV呼叫C,不会存在Scope不对找不到对应的C function的问题。但是C呼叫SV,则会碰到这种问题。VCS仿真器会给出如下Log

Error-[DPI-DXFNF] DPI export function not found

The DPI export function/task ‘sv_task’ called from a user/external  C/C++/DPI-C code originated from import DPI function ‘c_main’ at file ‘test.sv'(line 3) is not defined or visible.

Please check the called DPI export function/task is defined in the mentioned which invokes that DPI export function/task is made with ‘context’. Another work-around is using svGetScopeFromName/svSetScope to explicitly set the scope to the module which contains the definition of the DPI export function/task.

Continue reading “SV DPI Scope”