欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何在Verilog中通过VCS使用VPI和PLI/DPI调用C程序来实现Cover Property驱动的仿真控制?

最编程 2024-08-07 07:42:13
...

本文介绍一种在verilog中,运用PLI+VPI机制调用C程序获得cover property的覆盖情况从而控制仿真的方法,其中的C程序,可以接受传参,且可以有返回值,仿真器选择的VCS。

  1. 编写C程序
    #include <stdio.h>
    #include "vpi_user.h"
    //使用PLI有两种机制:TF/ACC机制,或者VPI机制
    
    //void hello_calltf(int user_data, int reason)
    //{
    //    int var,result;
    //    var = tf_getp(1);//取$hello调用时的第一个参数传递值
    //    result = var;
    //    tf_putp(0, result);//实际返回值
    //    0;
    //}
    
    void get_cover_cnt_calltf(int user_data, int reason)
    {
        vpiHandle hd;
        hd=vpi_handle_by_name(tf_getcstringp(1),Null);//根据string获取句柄
        //从第一个形参获取传入的property层级路径名,
        //注意使用bind时,property的层级路径是在被bind的instance下面,如:top.xxx.assert_module.cover_name
        tf_putp(0,vpi_get(vpiAssertSuccessCovered,hd);//获取此property的success次数
        0;
    }

     

  2. 建立链接,下面两种方式任选其一即可
    1. tab模式,编写xxx.tab文件,内容参考:
      $hello size=32 call=get_cover_cnt_calltf    //如果期望调用的C程序有返回值,则必须声明size=xxx,PLI视其为function,否则视其为task(无返回值)

       

    2. 注册模式,麻烦,略。
  3. 仿真选项
    vcs hello.c -P xxx.tab +vpi ...
  4. 在平台中调用即可获得所传property的覆盖情况:

    var=$hello("top.xxx.assert_module.property_inst_name");

     

大功告成!

参考资料:

hello world

Verilog PLI Tutorial


上面的方法不好用,现在都是用DPI机制了,可以参考SV绿皮书C语言部分,只需两步:

1.C程序部分修改:

int get_cover_cnt_calltf(char *cp_name)
{
    vpiHandle hd;
    hd=vpi_handle_by_name(cp_name,Null);//根据string获取句柄
    //从第一个形参获取传入的property层级路径名,
    //注意使用bind时,property的层级路径是在被bind的instance下面,如:top.xxx.assert_module.cover_name
     return vpi_get(vpiAssertSuccessCovered,hd);//获取此property的success次数
}

2.SV部分修改:

import "DPI-C" function int get_cover_cnt_call_tf(input string cp_name);//使用sv语法

...
get_cover_cnt_call_tf("top.xxx.cp_name");//注意没有$符号