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.

产生这个错误的例子如下:

C code: c_side.c

SV Code:

从上面的例子我们可以发现,sv_task的scope是pkg, 而我们呼叫obj.run是在module test中,进而c_main的scope也是module test。也就是说,sv_task与c_main不在同一个scope下,那么仿真器在module test的scope下找不到sv_task,所以就报了上面的Error。

解决方法在Error的最后一句有提到:在c_main里显式切换scope.

修改c code,增加如下代码。

svSetScope:从当前scope切换到指定scope。

svGetScopeByName:根据给定的string获取scope。

在IEEE 1800-2012标准中给出的prototype为:

/* Set context for subsequent export function execution.
* This function must be called before calling an export function, unless
* the export function is called while executing an import function. In that
* case the export function shall inherit the scope of the surrounding import
* function. This is known as the “default scope”.
* The return is the previous active scope (per svGetScope)
*/
svScope svSetScope(const svScope scope);
/* Gets the fully qualified name of a scope handle */
const char* svGetNameFromScope(const svScope);
/* Retrieve svScope to instance scope of an arbitrary function declaration.
* (can be either module, program, interface, or generate scope)
* The return value shall be NULL for unrecognized scope names.
*/
svScope svGetScopeFromName(const char* scopeName);

重新编译、链接后,simulation结果为:

Successfully called sv_task

Author: aquaporcus

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

Leave a Reply

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