Ever played with SystemVerilog DPI (Direct Programming Interface)? Most of the verification engineers coming from EC/EE background, they have hard time debugging C code in general.
Things get complicated when you deal with multi-logic-valued system such as Verilog/SV (0,1,Z,X) and 2-state system such as C. See the standard data type matching slide we deliver during our popular VSV training: (http://www.cvcblr.com/trainings):
In case you can memorize the table above and use it each and every time when you touch DPI – perhaps you maynot need this post much. But for the 99.9% users who don’t or don’t want to do that, tools like Questa provide you a very handy option called -dpiheader Let’s see it in action: Consider a simple DPI import declaration as below:
Corresponding C-code looks like:
So far so good? For those who have “spotted” the bug, pat yourself and continue reading. Others – any issue so far? Let’s see what the simulation output from Questa looks like:
Now all your debug skills ON..go, chase and find that little:
If you go back to the basics – default argument data type for SV task is logic and is single bit in size. On SV side we had
output data)
On C/DPI side the corresponding mapping as per LRM is:
Whereas what we intended was to use a plain “int” for “data” – the example is kind of trivial to keep things simple and in perspective. Realize that in reality this can be fairly cumbersome mapping to memorize and/or remember.
Here is where the “debug champion” helps – your friendly –depheader option to vlog command.
When you run this, you get an output header file “sv_auto_hdrs_for_c.h”. It is best to include this to your C-code:
Now during “vsim” step, the Questa invokes C-compiler and it detects the type-mismatch in arguments on SV vs. C-side. On SV side we said “output data” – indicating it is a 4-state, 1-bit variable, while on C-side we declared it as “int”:
Now that’s by far a much better issue to debug than the simulation/runtime mismatch of values in C and SV side.
So next time you attempt to use SystemVerilog DPI make sure you read this blog and use the –dpiheader option with Questa. And if your team needs experts, hands-on training on SystemVerilog, contact us via: www.cvcblr.com/about_us
No comments:
Post a Comment