task/functionのオーバーロードを検討してみた。(DPI-C編)

task/functionのオーバーロードを検討してみた。」を見て DPI-Cで書いてみみた
ただ、この場合だと C++のオーバーロードを使ったやり方になりますので、
シナリオは C++側に書くことになります。

  • C++側のコード
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
extern void sv_show( int );
extern void sv_show2( int , int );
#ifdef __cplusplus
}
#endif

void show(int a){
  sv_show(a);
}

void show(int a, int b){
  sv_show2(a, b);
}

#ifdef __cplusplus
extern "C" {
#endif
int c_main( void )
{
  int x = 2;
  int y = 9;
  show(x);
  show(x, y);
  return 0;
}
#ifdef __cplusplus
}
#endif
  • SystemVerilog側のコード
module test;

  import "DPI-C" context task c_main();

  initial begin
      c_main();

      $finish(2);
  end

  export "DPI-C" sv_show  = task task_show;
  export "DPI-C" sv_show2 = task task_show2;

  task task_show( input int a );
    $display("--- a = %3d", a);
  endtask : task_show

  task task_show2( input int a, input int b );
    $display("--- a = %3d, b = %3d", a,b);
  endtask : task_show2

endmodule : test
  • 実行結果
# --- a =   2
# --- a =   2, b =   9

というような感じです。