创建c++测试台架来驱动Verilog DUT

Creating C++ testbench to drive a Verilog DUT

本文关键字:Verilog DUT c++ 测试台 创建      更新时间:2023-10-16

我试图了解如何创建一个c++测试台架来驱动Verilog中的DUT的刺激。假设我有一个简单的场景:

// Testbench Top.
module tb_top();
import "DPI-C" function void wait_for_input_ready();
initial
    wait_for_input_ready();
import "DPI-C" function void notify_input_ready();
always @(posedge clk or negedge rst)
begin
   // based on some condition here I want to send input-ready notify.
   notify_input_ready();
end
endmodule
下面是我的c++代码:

test_case.h

    extern "C" void wait_for_input_ready();
    extern "C" void notify_input_ready();

test_case.cpp

    #include "test_case.h"
    std::conditional_variable cond_var;
    std::mutex input_mutex;
    bool input_ready = false;
    void wait_for_input_ready()
    {
        std::unique_lock<std::mutex> lock(input_mutex);
        while(input_ready != true)
            cond_var.wait(lock, [&]{return input_ready == true;});  // This is where the problem happens.
    }
    void notify_input_ready()
    {
        std::unique_lock<std::mutex> lock(input_mutex);
        is_ready = true;
        cond_var.notify_one(); // Unblock to wait statement.
    }

在本例中,条件变量上的等待语句永远阻塞,并且不让模拟器执行Verilog代码的任何其他部分。那么什么才是正确的方法呢?我应该在wait_for_input_ready函数中创建一个c++线程并完全分离它吗?

不能将SystemVerilog线程的概念与c++线程混合使用。从DPI的角度来看,一切都在同一个线程中执行。如果你想让c++代码看起来像一个SystemVerilog线程,你需要导入你的c++代码作为一个任务,并让它调用导出的SystemVerilog任务。

你可能想要阅读的几个链接:https://s3.amazonaws.com/verificationacademy-news/DVCon2013/Papers/MGC_DVCon_13_Easy_Steps_Towards_Virtual_Prototyping_Using_the_SystemVerilog_DPI.pdf

https://verificationacademy.com/forums/ovm/how-maintain-database-c-function-if-firmware-hardware-co-simulation-used回复- 37204