{"id":29412,"date":"2022-12-16T09:05:40","date_gmt":"2022-12-16T17:05:40","guid":{"rendered":"https:\/\/digilent.com\/blog\/?p=29412"},"modified":"2023-01-03T15:39:52","modified_gmt":"2023-01-03T23:39:52","slug":"using-vivados-built-in-simulator-to-debug-designs","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/using-vivados-built-in-simulator-to-debug-designs\/","title":{"rendered":"Using Vivado&#8217;s Built-in Simulator to Debug Designs"},"content":{"rendered":"<p>A classic interaction between a new FPGA user and an embattled older engineer (whether on our <a href=\"https:\/\/forum.digilent.com\/\">Forums<\/a> or elsewhere) goes as follows:<\/p>\n<blockquote><p><strong>New engineer:<\/strong> My design doesn&#8217;t work, could you help me figure out why?<\/p>\n<p><strong>Experienced engineer:<\/strong> Sure&#8230;<\/p>\n<p><strong>New engineer:<\/strong> Here&#8217;s a 200 line module I wrote, when I run it in hardware, it doesn&#8217;t do what I expect.<\/p>\n<p><strong>Experienced engineer:<\/strong> *Sighs*, Hey you should really simulate this, it&#8217;s not that hard.<\/p>\n<p><strong>New engineer:<\/strong> &lt;Some kind of brain exploding emoji&gt;<\/p><\/blockquote>\n<p>Simulation of hardware designs (which is performed using a simulator) is a way of looking at what a design does and how it responds when it receives certain signals, without running anything on an actual board. This makes it a really quick way to check your work quickly without extensive testing in hardware. It also lets you test only specific smaller parts of larger designs, letting you check that smaller pieces work, without involving the complexity of the rest of the system, and without generating an entire bitstream for a larger system &#8211; you can test a small piece in minutes instead of spending hours with debuggers and logic analyzers.<\/p>\n<p>Check out the guide below to see how a design behaves as its input changes using Vivado&#8217;s build-in Simulator. The design consists of a three-input AND gate and a D-Flip-Flop to register the output.<\/p>\n<h2 id=\"inventory\" class=\"sectionedit2\">Inventory<\/h2>\n<div class=\"level2\">\n<div class=\"li\">All you need to follow this guide is a Vivado installation.<\/div>\n<ul>\n<li class=\"li\">This guide was written using Vivado 2022.1, however other versions will work largely the same, with some minor visual differences.<\/li>\n<li class=\"li\">If you haven&#8217;t already, check out\u00a0<a class=\"wikilink1\" title=\"programmable-logic:guides:installing-vivado-and-vitis\" href=\"https:\/\/digilent.com\/reference\/programmable-logic\/guides\/installing-vivado-and-vitis\" data-wiki-id=\"programmable-logic:guides:installing-vivado-and-vitis\">Installing Vivado, Vitis, and Digilent Board Files<\/a>\u00a0for instructions on how to install Vivado<\/li>\n<\/ul>\n<h2 id=\"guide\" class=\"sectionedit3\">Guide<\/h2>\n<h3 id=\"simple_d-flip-flop_example\" class=\"sectionedit4\"><a id=\"simple_d-flip-flop_example_anchor\" class=\"anchor\" href=\"https:\/\/digilent.com\/reference\/programmable-logic\/guides\/simulation#simple_d-flip-flop_example\" aria-hidden=\"true\"><\/a>Simple D-Flip-Flop Example<\/h3>\n<div class=\"level3\">\n<div class=\"group plugin_wrap\">\n<div class=\"wrap_column wrap_half plugin_wrap\">\n<p>To get started, we will add a simulation source file to our project. See <a class=\"wikilink1\" title=\"programmable-logic:guides:getting-started-with-vivado\" href=\"https:\/\/digilent.com\/reference\/programmable-logic\/guides\/getting-started-with-vivado\" data-wiki-id=\"programmable-logic:guides:getting-started-with-vivado\">Getting Started with Vivado for Hardware-Only Designs<\/a> if you require a review of creating a simple design project. For the purposes of this guide, the following module is the design under test:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-29439\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/12\/1_design-600x300.png\" alt=\"\" width=\"600\" height=\"300\" data-wp-pid=\"29439\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/12\/1_design-600x300.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/12\/1_design.png 623w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Under the Project Manager section, choose Add Sources and create a simulation source file.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/3_addsimulationsource.png?w=800&amp;tok=e000d7\" \/><\/p>\n<p>Instead of a Verilog file, we want to create a SystemVerilog file, which can be very helpful with our simulations because it exposes additional constructs. Be sure to name the file. RegisterAndGate is the name of our design module, so in order to indicate that we are running a simulation module, we will use Test_RegisterAndGate. Next, click OK and then Finish.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/4_addsimulationsource2.png?w=800&amp;tok=880a97\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/5_addsimulationsource3.png?w=800&amp;tok=b9c495\" \/><\/p>\n<p>The wizard will ask if the simulation model uses inputs and outputs before you complete the process.\u00a0In general, top-level test-bench modules do not use inputs or output, so press OK to continue followed by YES for confirmation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/6_addsimulationsource4.png?w=800&amp;tok=9bc24b\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/7_addsimulationsource5.png?w=800&amp;tok=3b288c\" \/><\/p>\n<p>Once you double-click the new file shown under Simulation Sources\u2192sim_1, Vivado will generate and display the file.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/8_openyoursimulationfile.png?w=800&amp;tok=cf15b9\" \/><\/p>\n<p>For test purposes, inputs to the module to be tested are registers and its outputs are wires. For the three inputs add one three-bit register. For the one output add a wire. Instantiate the design module as U1 (or any name of your choosing) as shown below and save the file.<\/p>\n<p>Notice that under Simulation Sources\u2192sim_1, the test module now appears as a child of the simulation module.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/9_addvarsandmoduleandsave.png?w=800&amp;tok=d80e71\" \/><\/p>\n<p>The design uses a flip-flop to register the AND gate output, so we need to create a clock next. Add a local parameter named CLK_PERIOD with a value of 10 to designate the clock period. 10 indicates 10 nS denoted by the `timescale 1ns \/ 1ps line at the top.<\/p>\n<p>The following chunk of code initializes the clock to logic zero and toggles (inverts) it every 5 ns for as long as the simulation is running:<\/p>\n<pre class=\"code\">initial clk = 1\u2019b0;\r\nalways #(CLK_PERIOD \/ 2.0)\r\n  clk = ~clk;<\/pre>\n<p>Edit Test_RegisterAndGate.sv as shown to the right. This code will generate a continuous clock that has a 10 nS period. Save the file before continuing.<\/p>\n<\/div>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/10_simclkandtimescale.png?cache=&amp;w=567&amp;h=700&amp;tok=c278bf\" alt=\"10_simclkandtimescale.png\" \/><\/p>\n<p>Now need the individual test steps. For this, we will use the brute-force method of individually setting each condition and time duration. See the initial-begin block below. The #10 is a 10 nS delay. Each line is a variation of the three AND gate signals along with a time delay. #10 just happens to be the period of the clock, but it is an arbitrary number. You could use #2, #3, #5, and so on, however, if the delay is shorter than the clock period, the result may not be registered to the output. Save the file and continue.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/11_addtestsignals.png?w=800&amp;tok=6999ea\" \/><\/p>\n<p>To run the simulation, choose Run Behavioral Simulation by selecting Run Simulation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/12_runbehavioralsimulation.png?w=800&amp;tok=c972b6\" \/><\/p>\n<p>The Wave-Window should open displaying the results graphically after the simulation is done. The default period for a simulation is 1000 nS. You can adjust the time via the Simulation Settings. Select Full View and zoom in to get a better picture of the signals. To select Full View, right-mouse click the screen and choose Full View.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/13_selectfullview.png?w=800&amp;tok=b1b66b\" width=\"679\" height=\"410\" \/><\/p>\n<p>Hit the (+) magnifying glass icon twice then move the x-axis slider to the beginning (left) to zoom in on the main region of interest.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/14_magnifyandadjustposition.png?w=800&amp;tok=43ebf1\" \/><\/p>\n<p>We already know what the CLK_PERIOD is, so we don&#8217;t want it cluttering up the screen. If we were taking a look at a more complex design with many more signals it would be taking up prime real estate. Go ahead and delete its trace:<\/p>\n<p>To delete a signal from the Wave-Window: select it, followed by a right-click-Delete. Or select it and press the Delete key.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/15_deletewave.png?w=800&amp;tok=e3c779\" \/><\/p>\n<p>Internal signals can also be added from within the instantiated module. For instance, choose U1 (the instantiated design module) in the Scope tab. This will list the signals inside U1, right-mouse click and choose Add to Wave-Window.<\/p>\n<p>These signals typically start out without recorded waves when first added to the window, so you may need to either relaunch the simulation or reset it and run it forward again in order to get these signals to appear.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/digilent.com\/reference\/_media\/programmable-logic\/guides\/simulation\/16_addotherwavestochart.png?w=800&amp;tok=dd4227\" \/><\/p>\n<p>From the previous Wave-window results, the gate_inputs register counts from zero to seven testing the eight combinations of the inputs. When the gate_inputs register equals seven, or all three inputs are high, the output of the AND gate is high and, on the next clock edge after, the flip-flop output Q is set high, indicating correct behavior.<\/p>\n<p>In this <a href=\"https:\/\/digilent.com\/reference\/programmable-logic\/guides\/simulation\">guide on reference<\/a>, you can find several additional tips and tricks, including how you can use the simulator to measure how long it takes after an event occurs for an output to appear &#8211; a useful step in verifying that your design meets the requirements you&#8217;ve been given.\u00a0To jump straight to the Debouncer example, <a href=\"https:\/\/digilent.com\/reference\/programmable-logic\/guides\/simulation#debouncer_example\">click here.<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-29412 jlk' data-task='like' data-post_id='29412' data-nonce='a7290e5e40' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-29412 lc'>+1<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-29412 jlk' data-task='unlike' data-post_id='29412' data-nonce='a7290e5e40' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-29412 unlc'>-1<\/span><\/a><\/div><\/div> <div class='status-29412 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>A classic interaction between a new FPGA user and an embattled older engineer (whether on our Forums or elsewhere) goes as follows: New engineer: My design doesn&#8217;t work, could you &hellip; <\/p>\n","protected":false},"author":52,"featured_media":29428,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4325,4327,4323,4267,1563,4326],"tags":[4355,4371,3899,453],"ppma_author":[4462],"class_list":["post-29412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debug-validation-test","category-projects","category-software","category-featured","category-guide","category-teaching-training","tag-getting-started","tag-simulator","tag-test-and-measurement","tag-vivado"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/12\/learn-to-simulate.png","jetpack_sharing_enabled":true,"authors":[{"term_id":4462,"user_id":52,"is_guest":0,"slug":"abrown","display_name":"Arthur Brown","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/60e7f8e1b3a55e2e20ee541df1f393c2acbcee9fd05fd3e38d07e25a2e6fd237?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=29412"}],"version-history":[{"count":13,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29412\/revisions"}],"predecessor-version":[{"id":29441,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29412\/revisions\/29441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/29428"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=29412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=29412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=29412"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=29412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}