{"id":17440,"date":"2022-08-21T07:58:52","date_gmt":"2022-08-21T11:58:52","guid":{"rendered":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/?p=17440"},"modified":"2023-09-19T16:52:28","modified_gmt":"2023-09-19T20:52:28","slug":"systemverilog-what-is-a-virtual-interface","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/2022\/08\/21\/systemverilog-what-is-a-virtual-interface\/","title":{"rendered":"SystemVerilog: What is a Virtual Interface?"},"content":{"rendered":"\n<p>When I learned the SystemVerilog verification features, one concept had me baffled \u2013 virtual interfaces. What are these and why are they needed?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an interface?<\/h2>\n\n\n\n<p>Start with an interface. Quick summary: Back in Verilog days, a module had ports, which were individual signals. To connect two modules, each with 9 ports, you had to list all 9 signals. This is very error prone.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/sigs.png\" alt=\"Connect module with individual ports\" class=\"wp-image-17468\" width=\"444\" height=\"166\" srcset=\"https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/sigs.png 831w, https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/sigs-600x225.png 600w, https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/sigs-768x287.png 768w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><figcaption class=\"wp-element-caption\">Test and design modules connected by separate port signals<\/figcaption><\/figure>\n\n\n\n<p>When you connect your TV to a DVD player, do you connect individual wires? No, you plug an HDMI cable, click, and are done. You don\u2019t even have to know how many wires are in an HDMI cable (20). In SystemVerilog, a bundle of wires is called an interface. In this diagram, the SystemVerilog test module has a single interface port, while the old Verilog design still has individual port signals.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/ifc-1.png\" alt=\"Connect test module with interface to design with individual ports\" class=\"wp-image-17469\" width=\"500\" height=\"162\" srcset=\"https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/ifc-1.png 941w, https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/ifc-1-600x194.png 600w, https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/ifc-1-768x249.png 768w, https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/ifc-1-900x292.png 900w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption class=\"wp-element-caption\">The test module has an interface port while the design module still has individual port signals<\/figcaption><\/figure>\n\n\n\n<p>An interface contains wires and synthesizable methods to perform operations such as sending and receiving transactions. An interface is RTL, just like your design. Here is an interface with 7 signals and a task to receive a transaction.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>interface color_ifc;<br>&nbsp; logic r,o,y,g,b,i,v;<br>&nbsp; task receive(output logic &#91;31:0] data);<br>&nbsp;&nbsp;&nbsp; \u2026<br>&nbsp; endtask<br>endinterface<\/code><\/code><\/pre>\n\n\n\n<p>Real hardware is static. If your chip is fabricated with 3M gates, it can&#8217;t suddenly have 4M gates just because you need a little boost. In simulation, the RTL design is specified at compile time and can&#8217;t grow or shrink.&nbsp; &nbsp;Likewise, your simulation has a fixed number of interfaces, specified at compile time, and can&#8217;t change at run time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic Testbenches<\/h2>\n\n\n\n<p>SystemVerilog classes and objects are dynamic. If you need to send 1000 transactions, just construct 1000 objects. Need 2000? Just construct more. If your UVM testbench decides it needs 2 drivers instead of the default of 1, it just creates another driver (or agent) at runtime.<\/p>\n\n\n\n<p>Since objects are dynamic, they can&#8217;t contain interfaces. Otherwise, when you construct that second driver at runtime, it would create a second interface, which is not allowed.<\/p>\n\n\n\n<p>But an object can contain pointers. For example, an agent has a pointer (handle \/ class variable) to the objects for the driver, monitor, etc. How can you make a pointer to the RTL interface?<\/p>\n\n\n\n<p>Tip: In SystemVerilog, the keyword \u201cvirtual\u201d usually means pointer. A virtual interface is a just pointer to an interface.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here is the test module with a colors interface and a driver. When it constructs the driver, it passes a pointer to the (physical) interface.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>module test(color_ifc c_ifc);\n\u00a0 Driver d;\n\u00a0 initial begin\n\u00a0\u00a0\u00a0 d = new(c_ifc);\u00a0 \/\/ Pass interface\n\u00a0\u00a0\u00a0 d.send(42);\n\u00a0 end\nendmodule<\/code><\/code><\/pre>\n\n\n\n<p>The driver class saves the pointer in a virtual interface, so it can use it later to drive signals.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Driver;\n\u00a0 <strong>virtual<\/strong> color_ifc v_ifc; \/\/ Pointer to RTL instance\n\n\u00a0 function new(input <strong>virtual<\/strong> color_ifc c_ifc);\n\u00a0\u00a0\u00a0 v_ifc = c_ifc;\n\u00a0 endfunction\n\n\u00a0 task send(input logic &#91;31:0] data);\n\u00a0\u00a0\u00a0 v_ifc.r = data&#91;0];\n\u00a0\u00a0\u00a0 \u2026\n\u00a0 endtask\nendclass<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Benefits<\/h2>\n\n\n\n<p>The Driver class is reusable as it can point to any color_ifc. If your testbench need to connect to two interfaces, it just constructs two Driver objects, passing in each interface instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<p>You can learn more about these topics including Oriented Programming with the Siemens SystemVerilog for Verification course. It is offered in <a href=\"https:\/\/training.plm.automation.siemens.com\/ilt\/iltdescription.cfm?pID=268489-US_____EDA__2021.4_1199\" target=\"_blank\" rel=\"noopener\">instructor led<\/a> format by our industry expert instructors, or in a self-paced <a href=\"https:\/\/training.plm.automation.siemens.com\/mytraining\/viewlibrary.cfm?memTypeID=287545&amp;memID=287545\" target=\"_blank\" rel=\"noopener\">on-demand<\/a> format. It can also be tailored to address your specific design goals and show you how to set up an environment for reuse for additional designs.&nbsp; Also, you can now earn a digital badge\/level 1 certificate by taking our <a href=\"https:\/\/training.plm.automation.siemens.com\/mytraining\/viewlibrary.cfm?memTypeID=288054&amp;memID=288054\" target=\"_blank\" rel=\"noopener\">Advanced Topics Badging Exam<\/a>. This will enable you to showcase your knowledge of this topic by displaying the badge in your social media and email signature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I learned the SystemVerilog verification features, one concept had me baffled \u2013 virtual interfaces. What are these and why&#8230;<\/p>\n","protected":false},"author":71586,"featured_media":17469,"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,982,10],"tags":[382,515,1464,1466,614,1465,690,751,838],"industry":[],"product":[],"coauthors":[980],"class_list":["post-17440","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-systemverilog","category-tips-tricks","tag-class","tag-handle","tag-interface","tag-object","tag-oop","tag-port","tag-rtl","tag-systemverilog","tag-virtual-interface"],"featured_image_url":"https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2022\/07\/ifc-1.png","_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/17440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/users\/71586"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/comments?post=17440"}],"version-history":[{"count":4,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/17440\/revisions"}],"predecessor-version":[{"id":18449,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/17440\/revisions\/18449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media\/17469"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media?parent=17440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/categories?post=17440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/tags?post=17440"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/industry?post=17440"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/product?post=17440"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/coauthors?post=17440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}