我是靠谱客的博主 大方海燕,最近开发中收集的这篇文章主要介绍[SV]SystemVerilog與C語言的接口(SystemVerilog DPI C++)SystemVerilog DPI C++,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

SystemVerilog DPI C++

       SystemVerilog DPI (Direct Programming Interface) is an interface which can be used to interface SystemVerilog with foreign languages. These Foreign languages can be C, C++, SystemC as well as others.

       DPI allows the user to easily call functions of other language from SystemVerilog and to export SystemVerilog functions, so that they can be called in other languages.

       Advantage of DPI is,  it allows to make use of the code exists in other languages.

 

一、DPI Import and export methods

1、Import Method

       The methods (functions/tasks) implemented in Foreign language can be called from SystemVerilog and such methods are called Import methods.

2、Export methods

       The methods implemented in SystemVerilog can be called from Foreign language such methods are called Export methods.

       It is allowed to transfer the data between two languages through arguments passing and return.

 

二、DPI Declaration

1、Import Declaration

import “DPI-C” function int calc_parity (input int a);

 

2、Export Declaration

export “DPI-C” my_cfunction = function myfunction;

 

3、SystemVerilog DPI Example

(1)Calling C++ method from SystemVerilog file

//----------------------------------------------
// SystemVerilog File
//----------------------------------------------
module dpi_tb;
 
  import "DPI-C" function void c_method();
   
  initial begin
    $display("Before calling C Method");
    c_method();
    $display("After calling C Method");
  end
   
endmodule
 
//----------------------------------------------
// C++ file
//----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <svdpi.h>
 
extern "C" void c_method() {
 
  printf("     Hello World...!n");
 
}

  Simulator output: 

  Before calling C Method
  [C-Prog]   Hello World…!
  After calling C Method

 

(2)Calling SystemVerilog method from C++ file

//----------------------------------------------
// SystemVerilog File
//----------------------------------------------

module dpi_tb;

  export "DPI-C" function sv_method;

  import "DPI-C" context function void c_method();   

  initial begin

    $display("Before calling C Method");

    c_method();

    $display("After calling C Method");

  end 

  function void sv_method();

    $display("  [SV-Prog]  Hello World...!");

  endfunction

endmodule

 

//----------------------------------------------
// C++ file
//----------------------------------------------

#include <stdio.h>
#include <iostream>
#include <svdpi.h>

using namespace std;

extern "C" void sv_method();

extern "C" void c_method() {

  printf("  [C-Prog]   Hello World...!n");

  sv_method();

}

  Simulator output: 


  Before calling C Method 

  [C-Prog]   Hello World…!
  [SV-Prog] Hello World…!
  After calling C Method

最后

以上就是大方海燕为你收集整理的[SV]SystemVerilog與C語言的接口(SystemVerilog DPI C++)SystemVerilog DPI C++的全部内容,希望文章能够帮你解决[SV]SystemVerilog與C語言的接口(SystemVerilog DPI C++)SystemVerilog DPI C++所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部