4

I am not trying to use a DPI call, but a simple Verilog program which internally uses its PLI to call a function written in C language. I don't know about static linking. I am using edaplayground.

Can anyone tell me which simulator should I use and switch should I pass to link both Verilog and C? Should I include the C file in the Verilog?

The sample code is as follows

// C function implementation
#include <stdio.h>  
void hello() {
   printf ("\nHello world\n");
 }

// SV function call
module sv_pli ();

  initial begin
    $hello;
     #10  $finish;
  end

 endmodule

I would like to know if there was any need to register the pli since currently the pli call hello is not detected.

Greg
  • 18,111
  • 5
  • 46
  • 68
user1978273
  • 484
  • 10
  • 24
  • 1
    Verilog is no programming language. – too honest for this site Aug 16 '15 at 03:05
  • 3
    @Olaf: Verilog is a simulation language, and functions can be called during simulation just like in a programming language. It's not a weird question if you remember that Verilog isn't just a hardware description language. – user541686 Aug 16 '15 at 06:08
  • @Mehrdad: It is a hardware description language, like VHDL. The functions still describe the hardware. OP might refer to System-Verilog (as the tag indicated, but that is not Verilog as his headline and text states. Anyway, the question is clearly OT. – too honest for this site Aug 16 '15 at 13:33
  • 4
    @Olaf: it is both a hardware description language and a language to aid simulating the described hardware. Only a portion of the language is used to describe hardware that can actually be synthesized. The rest is used for simulation and can contain calls to PLI, which can be written in C. – mcleod_ideafix Aug 16 '15 at 14:18
  • Could you maybe add your code showing how you use the PLI. – Morgan Aug 17 '15 at 08:15
  • Icarus got a decent PLI support: http://iverilog.wikia.com/wiki/Using_VPI – SK-logic Aug 17 '15 at 08:59
  • I'm not sure if user defined PLI/VPI tasks/functions are supported on edaplayground. I could only get the Riviera-POR to compile it with this warning. "_warning: deprecated conversion from string constant to 'PLI_BYTE8* {aka char*}' [-Wwrite-strings]_", but it doesn't seem to link to the simulator. – Greg Aug 17 '15 at 18:20
  • PLI/VPI need to be registered on the C side with the `vpi_register_systf()` and bootstrapped with ` vlog_startup_routines[]`; some simulator specific requirements might exist too. Refer to [IEEE Std 1800-2012](http://standards.ieee.org/getieee/1800/download/1800-2012.pdf) and read Clauses 36, 37, & 38. With your provided example, I would recommend converting it to DPI. It is a far simpler change then making it PLI/VPI with less overhead. – Greg Aug 17 '15 at 19:19
  • Thank you Greg, but wouldnt calling DPI be slower than registering and using a PLI equivalent call? – user1978273 Aug 17 '15 at 21:41
  • 2
    Just the opposite - DPI runs much faster than the equivalent VPI. And if you stick with passing C compatible types (int byte and arrays of these) it could be orders of magnitude faster. – dave_59 Aug 19 '15 at 21:00
  • I will never understand why C++ was used in the first place?! – 71GA May 04 '21 at 18:36

1 Answers1

0

I put your C code in a new file named test.c, and added SV-DPI to your Verilog code, removed dollar sign from $hello task to become a DPI call.

See the simulator output and it should be the expected result from your sample code.

jclin
  • 2,449
  • 1
  • 21
  • 27