How to DPI "export" a task embedded in a uvm_driver to c code?

Hi,
I have a working uvm_driver which has all tasks to read and write registers.
Now I want to reuse these tasks in my c code for bringup. Systemverilog DPI supports this using “export”.
But when I exports the tasks in my uvm_driver I run into compile issues !
Any help is appreciated.

Thanks in advance.

You can’t export a class method to another language; you need to make a wrapper task

class my_driver extends uvm_driver;

  task do_something(something arguments);
    ...
  endtask

endclass
task do_something_wrapper(something arguments);
  my_driver h; // Someone needs to initialize this 
  h.do_something(arguments);
endtask

export "DPI-C" task do_something_wrapper;

And remember, your C code needs to be invoked by importing a C task.

In reply to dave_59:

Hi Dave
Thanks for the reply.
Couple of questions
You have said:

  1. my_driver h; // Someone needs to initialize this
    How could this be done? We could do h = my_driver_handle(assuming my_driver_handle was created in say an agent), but that can be done only inside a class.
  2. “your C code needs to be invoked by importing a C task” - Not sure why this is needed for an “export”?

Thanks.

In reply to bramaniinfn:

h would probably be a static variable defined in a package. Your agent would be able to set after the build phase with

my_pkg::do_something_wrapper.h = my_driver handle.

You could also put it in the config database or use find_component

task do_something_wrapper(something arguments);
  static my_driver h;
  if (h==null) begin <get from config_db or use find_component> end
  h.do_something(arguments);
endtask

About your C code being imported, I think you need to get further along before you will understand the issue.

Hello All,

I am trying to import and export in the sequence/virtual sequences scope.

I am successfully able to import C functions, but having a problem with exporting virtual task defined in these sequences, since DPI exports only export task and function defined in modules/ Packages.

Kindly help,
Sumeet.

In reply to bugbuster:

Don’t just mention you have a problem, describe it.

As I stated above, you need a wrapper task that you export, and it calls the virtual task you want. Also, you need to import your C routines as a task, not a function, to call another task.