{"id":15855,"date":"2021-02-18T19:39:26","date_gmt":"2021-02-19T00:39:26","guid":{"rendered":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/?p=15855"},"modified":"2026-03-27T08:46:39","modified_gmt":"2026-03-27T12:46:39","slug":"finding-fun-dpi-c-recording-c-variables-in-a-wave-database","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/2021\/02\/18\/finding-fun-dpi-c-recording-c-variables-in-a-wave-database\/","title":{"rendered":"Finding FUN &#8211; DPI-C Recording C Variables in a Wave Database"},"content":{"rendered":"\n<p>Pandemic?<\/p>\n\n\n\n<p>Cold freeze?<\/p>\n\n\n\n<p>No Power?<\/p>\n\n\n\n<p>Ugh. What a 12 months. And all that&#8217;s unfolded.<\/p>\n\n\n\n<p>I&#8217;m looking for some fun. I don&#8217;t know about you.<\/p>\n\n\n\n<p>Hmmm.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DPI-C! That&#8217;s the ticket.<\/h2>\n\n\n\n<p>Imaginary Day Dream Helmet engaged.<\/p>\n\n\n\n<p><strong>Problem Scenario<\/strong>: I have some C code which I&#8217;m using as a GOLDEN model in my testbench. There are bugs and behavior differences between the C and SystemVerilog RTL.<\/p>\n\n\n\n<p><strong>Proposed Solution<\/strong>: Use DPI-C to record C variables in the wave database alongside the RTL signals. Compare. Find the bug.<\/p>\n\n\n\n<p>Wait. I can turn my Imaginary Day Dream Helmet off. DPI-C can record C variables by adding just a little bit of code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ok. Go.<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">The C Code<\/h3>\n\n\n\n<p>My GOLDEN C code model has an array with 3 dimensions. (See line 7). It is the critical data structure. That&#8217;s what we want to record. The C code is already &#8220;time-based&#8221; simply by calling back into SystemVerilog using the &#8216;tictoc(1)&#8217; call on line 54, below. Each call to tictoc(N) waits for N clocks, where the clock is defined back in the SV module.<\/p>\n\n\n\n<p>Simply add a single line &#8211; a call to a function named &#8216;record_data(data)&#8217; (line 53). We&#8217;ll implement record_data() as an &#8220;export DPI-C&#8221; function back in the SV module.<\/p>\n\n\n\n<p>Cost: Change our C code : &#8220;<strong>Add One Line<\/strong>&#8221; (Line 53 &#8211; The gold line below).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">C Code:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>...\n  <span class=\"has-inline-color has-light-green-cyan-color\">7 <strong>typedef int <\/strong>data_t&#91;3]&#91;3]&#91;3];<\/span>\n...\n 25 \n 26 <strong>int<\/strong>\n 27 calculate_algorithm(data_t v) {\n 28   data_t data;\n 29 \n 30   <strong>int <\/strong>value, xvalue;\n 31   <strong>int <\/strong>i, j, k;\n 32   <strong>int <\/strong>sort&#91;100];\n...\n 46   <strong>for <\/strong>(i = 0; i &lt; 3; i++) {\n 47     <strong>for <\/strong>(j = 0; j &lt; 3; j++) {\n 48       <strong>for <\/strong>(k = 0; k &lt; 3; k++) {\n 49         value = v&#91;i]&#91;j]&#91;k];\n 50         data&#91;i]&#91;j]&#91;k] = value;\n 51         sort&#91;value] = 1;\n...\n 53         <span class=\"has-inline-color has-luminous-vivid-amber-color\">record_data(data);<\/span>\n 54         <span class=\"has-inline-color has-vivid-green-cyan-color\">tictoc<\/span>(1);\n 55       }\n 56     }\n 57   }\n...<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The SystemVerilog Code<\/h3>\n\n\n\n<p>The SystemVerilog variable &#8216;data&#8217; is declared in our RTL module as a 3x3x3 array of 7 bits. That is the place we will &#8220;record the C variable&#8221;.  (Line 56 below)<\/p>\n\n\n\n<p>&#8220;Recording&#8221; happens on line 62, when record_data() gets called. The argument &#8216;i&#8217; was pushed onto the stack in C (line 53 above), and is then passed to the SV record_data() implementation and &#8220;recorded&#8221; by being assigned to the variable named &#8216;data&#8217; (line 62 below).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>SystemVerilog Code:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> 49   <strong>export <\/strong>\"DPI-C\" <strong>task <\/strong>tictoc;\n 50   <strong>task <\/strong>tictoc(<strong>int<\/strong> times);\n 51     <strong>repeat <\/strong>(times)\n 52       @(<strong>posedge<\/strong> clk);\n 53   <strong>endtask\n<\/strong>\n 54 \n 55   <strong>typedef bit <\/strong>&#91;6:0] data_t &#91;3]&#91;3]&#91;3];\n<span class=\"has-inline-color has-luminous-vivid-amber-color\"> 56   data_t data;<\/span>\n 57   \n 58   <strong>import <\/strong>\"DPI-C\" <strong>context task <\/strong>calculate_algorithm(\n 59                                  <strong>inout<\/strong> data_t data);\n      \n<span class=\"has-inline-color has-luminous-vivid-amber-color\"> 60   <strong>export <\/strong>\"DPI-C\" <strong>function <\/strong>record_data;\n 61   <strong>function void <\/strong>record_data(<strong>input<\/strong> data_t i);\n 62     data = i;\n 63   <strong>endfunction<\/strong><\/span>\n...  \n 67   R_array r_array;\n 68   \n 69   <strong>initial begin<\/strong>\n...\n 76       <strong>if <\/strong>(<span class=\"has-inline-color has-light-green-cyan-color\">r_array.randomize()<\/span> == 0) <strong>begin<\/strong>\n 77         $display(\"Error: Randomize failed\");\n 78       <strong>end<\/strong>\n 79       local_data = r_array.data;\n 80       $display(\"INFO: Starting %m.data=%p\", local_data);\n 81       <span class=\"has-inline-color has-light-green-cyan-color\">calculate_algorithm(local_data);<\/span>\n 82       $display(\"INFO: Finished %m.data=%p\", local_data);\n...\n 86   <strong>end<\/strong><\/code><\/pre>\n\n\n\n<p>That&#8217;s it.<\/p>\n\n\n\n<p>Cost: Change our SystemVerilog Module. &#8220;<strong>Add a DPI-C export function and variable<\/strong>&#8220;. 5 lines total. (Lines 56 and 60-63 &#8211; The gold lines above).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Story<\/h3>\n\n\n\n<p>The SystemVerilog module is calculating a fancy algorithm based on a randomized 3x3x3 array of integers. There is a C model which is the golden source. Our SystemVerilog has 10,000 instances of these calculators. Each instance starts its own C model thread with a call to &#8216;calculate_algorithm(data)&#8217;.<\/p>\n\n\n\n<p>The C model makes calculations about the input data, and those calculations get recorded in the wave database. The RTL does the same. The results should match.<\/p>\n\n\n\n<p>In the image below, we&#8217;ve zoomed in and are inspecting the 9995th calculator.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"577\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform-9995-1024x577.jpg\" alt=\"\" class=\"wp-image-15862\" srcset=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform-9995-1024x577.jpg 1024w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform-9995-600x338.jpg 600w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform-9995-768x433.jpg 768w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform-9995-900x507.jpg 900w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform-9995.jpg 1443w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Waveform of C Calculator Number 9995<\/figcaption><\/figure>\n\n\n\n<p>Finally, using our regular debug tools, we can compare the C variable memory with the RTL variable memory and find the mismatch (highlighted in RED below).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"365\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Compare-C-and-RTL-Memory-Element-1024x365.jpg\" alt=\"\" class=\"wp-image-15912\" srcset=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Compare-C-and-RTL-Memory-Element-1024x365.jpg 1024w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Compare-C-and-RTL-Memory-Element-600x214.jpg 600w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Compare-C-and-RTL-Memory-Element-768x273.jpg 768w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Compare-C-and-RTL-Memory-Element-900x321.jpg 900w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Compare-C-and-RTL-Memory-Element.jpg 1112w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Waveform Compare &#8211; C Memory Element and RTL Memory Element<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Total cost: Add 6 lines of code. (The gold lines above)<\/p>\n\n\n\n<p>Total fun. Complete source code is available demonstrating this bit of FUN. <\/p>\n\n\n\n<p>If you want to hear more about DPI-C, please consider attending this <a href=\"https:\/\/2021.dvcon.org\/\" target=\"_blank\" rel=\"noopener\">Virtual DVCON US 2021<\/a> Session &#8211; <strong>Tuesday, March 2, 3:00-5:00<\/strong> &#8211; &#8220;<a href=\"https:\/\/epapers.org\/dvcon2021\/ESR\/session_view.php?PHPSESSID=37rii8no8u3d8a2nius0ectmf0&amp;session_id=5\" target=\"_blank\" rel=\"noopener\">Advanced Methodologies 1<\/a>&#8221; for the paper &#8220;<a href=\"https:\/\/epapers.org\/dvcon2021\/ESR\/paper_details.php?PHPSESSID=37rii8no8u3d8a2nius0ectmf0&amp;paper_id=7090\" target=\"_blank\" rel=\"noopener\">Making Your DPI-C Interface a Fast River of Data<\/a>&#8220;<\/p>\n\n\n\n<p>Happy Verifying!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pandemic? Cold freeze? No Power? Ugh. What a 12 months. And all that&#8217;s unfolded. I&#8217;m looking for some fun. I&#8230;<\/p>\n","protected":false},"author":75444,"featured_media":15858,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[421,908,751,831,909],"industry":[],"product":[],"coauthors":[],"class_list":["post-15855","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","tag-debugging","tag-dpi-c","tag-systemverilog","tag-verilog","tag-waveform"],"featured_image_url":"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/02\/DPI-C-Recording-C-Variables-In-The-Waveform.jpg","_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/15855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/users\/75444"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/comments?post=15855"}],"version-history":[{"count":5,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/15855\/revisions"}],"predecessor-version":[{"id":15927,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/15855\/revisions\/15927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media\/15858"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media?parent=15855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/categories?post=15855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/tags?post=15855"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/industry?post=15855"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/product?post=15855"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/coauthors?post=15855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}