Passing an sformat-formatted string to a DPI function
Hi all:
Verilator does not seem to have a $system() task, and I wish to run a shell command at the beginning of the simulation, but only after my DPI module has created a listening socket. This is what I've written so far:
import "DPI-C" function int uart_dpi_run_shell_command ( input string shell_cmd_to_run,
output int command_exit_code );
That works fine, but now I wish to build the shell_cmd_to_run string with Verilog parameters. I've tried this:
$sformat( shell_cmd,
"gnome-terminal --command \"sh -c 'socat -,raw TCP4:localhost:%d'\" &",
`UART_DPI_TELNET_PORT );
If I define shell_cmd like this:
reg [127*8:0] shell_cmd;
Then $sformat() fills the string with leading zeroes, and the DPI module things the string is empty.
On the other hand, if I define shell_cmd like this:
string shell_cmd;
Then I get this compilation error:
error: cannot convert ‘std::string’ to ‘void*’ for argument ‘2’ to ‘void VL_SFORMAT_X(int, void*, const char*, ...)’
I guess "input string" in the DPI function declaration translates to "const char*" in the generated .h file, but the "string shell_cmd" declaration creates an std::string. Somewhere is a call to std::string::c_str() probably missing.
Can someone help me here?
Thanks in advance, R. Diez
Replies (6)
RE: Passing an sformat-formatted string to a DPI function - Added by Wilson Snyder 6 months ago
Alas you need you function to take a verilog array and strip the leading zeros.
Alternatively, perhaps you could make a patch to support system? You can copy fopen code more or less.
RE: Passing an sformat-formatted string to a DPI function - Added by Wilson Snyder 6 months ago
Btw, either way feel free to file an unsupported bug to support $system.
RE: Passing an sformat-formatted string to a DPI function - Added by Wilson Snyder 6 months ago
Great work making your example code. I've also added support for the next release.
RE: Passing an sformat-formatted string to a DPI function - Added by vsevolod predtechensky 5 months ago
as a temporary solution, I found the following to work when passing systemverilog strings to dpi functions:
in a verilog code do the following gasket: ... s1 = "abcdefgh"; //whatever text assigned to your string directly or with $sformat ... $sformat(conv_array,"%s",s1); //wrapper that makes types compatible
your_DPI_call(conv_array);
notice that in DPI import an argument should be defined as input string
this is a temporary solution until Wilson releases a version that casts the strings correctly into C.
On a side, support for systemverilog string functions would be very beneficial and should not be too difficult to do - making wrappers for each function is a pain
RE: Passing an sformat-formatted string to a DPI function - Added by Wilson Snyder 5 months ago
BTW if you need $system it was put in 3.830.
RE: Passing an sformat-formatted string to a DPI function - Added by vsevolod predtechensky 5 months ago
however I still cannot make passing the verilog-defined strings to DPI functions correctly. dpi "input string" requires to be defined as reg[ ] and string should be passed through $sformat(reg, "%s", string) to work. I cannot make output string work. According to the above - regress_test(input string i, output string o) may not work correctly, it mistakenly returns "ok". I'm still playing with the code to make sure that its a bug and not a mistake in my setup.
(1-6/6)
![[logo]](/img/veripool_small.png)