{"id":255,"date":"2015-01-24T17:23:58","date_gmt":"2015-01-25T01:23:58","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?page_id=255"},"modified":"2016-02-14T07:55:23","modified_gmt":"2016-02-14T15:55:23","slug":"tutorial3","status":"publish","type":"page","link":"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial3\/","title":{"rendered":"Tutorial 3: More on Simulation and an Important Topic"},"content":{"rendered":"<p>In this tutorial, we are going to cover a little bit more on simulation. In addition, we&#8217;ll discuss an important subject that causes some confusion.<\/p>\n<h1>Blocking Versus Non-Blocking Assignments<\/h1>\n<p>The <em>very<\/em> observant reader of the last tutorial might have noticed there is an ambiguity in what the simulator needs to be doing. In our FPGA, we had an assignment which set the value of the the <em>led<\/em> outputs to be the same as the <em>switch<\/em> inputs on the rising edge of <em>clk<\/em>. Here is the code:<\/p>\n<pre><code>always @(posedge clk) led = switch;<\/code><\/pre>\n<p>In our test bench module, we drove the switch values to random values on the rising edge of <em>clk<\/em> as well.<\/p>\n<pre><code>always @(posedge clk) switch = $random;<\/code><\/pre>\n<p>So the question is, which of these events happens first in the simulation? In real hardware, it is possible for them to happen simultaneously, but in the simulator this is not possible. So, which goes first? The answer is, quite simply, we don&#8217;t know. It&#8217;s unclear\u00a0ahead of time which one will execute first. And which one is executed first\u00a0has an impact on the simulation&#8217;s behavior. Even if it were possible to\u00a0know, this situation is so common that making a working simulation with a real-life design would be just about impossible. You would be spending all of your time trying to keep track of which block goes\u00a0before which other block. Furthermore, it is actually impossible because sometimes, two blocks might <em>both<\/em> need to go first. Consider the code below:<\/p>\n<pre><code>module top(input clk, output reg a, output reg b);\r\n  initial a = 0;\r\n  initial b = 1;\r\n  always @(posedge clk) a = b;\r\n  always @(posedge clk) b = a;\r\nendmodule\r\n<\/code><\/pre>\n<p>When I simulate this <em>a<\/em> is set to 1 right away, and then nothing ever changes. <em>a<\/em> and <em>b<\/em> are both 1 for the entire simulation.<\/p>\n<p>If you elaborate the RTL design and look at the schematic, you&#8217;ill see the correct implementation:<\/p>\n<div id=\"attachment_258\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Schematic1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-258\" class=\"size-medium wp-image-258\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Schematic1-300x157.png\" alt=\"Synthesized design\" width=\"300\" height=\"157\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Schematic1-300x157.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Schematic1-500x262.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Schematic1.png 829w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-258\" class=\"wp-caption-text\">Synthesized design<\/p><\/div>\n<p>The sequential assignments above are called <em>blocking<\/em> assignments. Verilog also supports a different assignment operator called a <em>nonblocking<\/em> assignment. The example below shows the same code with nonblocking assignments.<\/p>\n<pre><code>module top(input clk, output reg a, output reg b);\r\n  initial a = 0;\r\n  initial b = 1;\r\n  always @(posedge clk) a &lt;= b;\r\n  always @(posedge clk) b &lt;= a;\r\nendmodule\r\n<\/code><\/pre>\n<p>When the simulator encounters a nonblocking assignment, it evaluates the right hand side of the expression and schedules that value to be assigned to the left hand side of the expression once\u00a0all other always blocks are evaluated. <span style=\"text-decoration: underline;\">It is\u00a0critically important that you always use long blocking assignments when assigning to output values in sequential always blocks. Likewise, always use a blocking assignment when assigning the output of a combinational always block.<\/span><\/p>\n<p>You should take the time now to modify your existing <strong>top.v<\/strong> and <strong>bench.v<\/strong> files to use the nonblocking assignment operator where appropriate.<\/p>\n<p>Here is what I have for my <strong>top.v<\/strong> file<\/p>\n<pre><code>`timescale 1ns \/ 1ns\r\nmodule top\r\n (\r\n input clk,\r\n input [7:0] switch,\r\n output reg [7:0] led\r\n );\r\n always @(posedge clk) led &lt;= switch;\r\nendmodule\r\n<\/code><\/pre>\n<p>And here is my <strong>bench.v<\/strong> file<\/p>\n<pre><code>`timescale 1ns \/ 1ns\r\nmodule bench;\r\n reg clk = 1;\r\n always #5 clk = ~clk;\r\n\r\n reg [7:0] switch;\r\n wire [7:0] led;\r\n top top\r\n (\r\n .clk(clk),\r\n .switch(switch),\r\n .led(led)\r\n );\r\n\r\n always @(posedge clk)\r\n switch &lt;= $random;\r\n\r\n initial\r\n begin\r\n repeat (1000) @(posedge clk);\r\n $finish;\r\n end\r\n \r\nendmodule\r\n<\/code><\/pre>\n<h1>Improving the Test Bench<\/h1>\n<p>The waveform below shows the last few clock cycles of my simulation. The <em>switch<\/em> and <em>led<\/em> values are random. However, notice how the <em>led<\/em> values lag behind the <em>switch<\/em> values by one clock cycle. \u00a0This is to be expected, since the <em>led<\/em> values are captured in flip-flops on the rising edge of <em>clk<\/em>.<\/p>\n<div id=\"attachment_261\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Sim1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-261\" class=\"size-medium wp-image-261\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Sim1-300x53.png\" alt=\"Simulation Waveform\" width=\"300\" height=\"53\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Sim1-300x53.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Sim1-1024x182.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Sim1-500x89.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial3_Sim1.png 1672w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-261\" class=\"wp-caption-text\">Simulation Waveform<\/p><\/div>\n<p>We will now\u00a0add a check to the test bench to compare the <em>led<\/em> outputs with an expected result. The first thing to do is to generate the expected result, which will be simply the <em>switch<\/em> variable delayed by one clock cycle.<\/p>\n<pre><code>  reg [7:0] expected_led;\r\n  always @(posedge clk)\r\n    expected_led &lt;= switch;<\/code><\/pre>\n<p>Then, we will perform the comparison between the expected output and the actual output. We declare two integer variables called <em>num_checks<\/em> and <em>num_errors<\/em> and initialize them to zero. Integer variables are essentially signed 32-bit reg values. Then, on every clock edge, we perform a comparison. We&#8217;ll increment the number of checks counter for each time we perform this comparison, and increment the number of errors counter if the expected output does not match the actual output.<\/p>\n<pre><code>  integer num_checks = 0;\r\n  integer num_errors = 0;\r\n  always @(posedge clk)\r\n    begin\r\n      num_checks = num_checks+1;\r\n      if (expected_led != led)\r\n\tbegin\r\n\t  $display(\"ERROR: led value %0x does not match expected value %0x at time %0fns\",\r\n\t\t   led,expected_led,$realtime);\r\n\t  num_errors = num_errors+1;\r\n\tend\r\n    end<\/code><\/pre>\n<p>Finally, we modify the always block that\u00a0stops our simulation so that it prints out a pass\/fail message. I like to keep track of the number of comparisons so I can ensure that the fail message actually means something. It&#8217;s not a very good test if it passes with no errors and no comparisons. The message for passing and failing is distinctive and similar to allow scripts to check your simulation results. It&#8217;s a great feeling when you\u00a0have the computer run hundreds of tests overnight, and then you come in the next morning to\u00a0see all those passed messages. Automated regression testing is an important part of real-world FPGA engineering.<\/p>\n<p>You should be able to make changes to your test bench and reproduce my results.<\/p>\n<h1>Synthesizable Verilog<\/h1>\n<p>As a final note, I should point out that there are different subsets of the Verilog language for different purposes. In these tutorials, we will discuss the Synthesizable RTL and behavioral subsets. We used lots of constructs in our test bench which have no place in an FPGA design proper. After all, what is your FPGA supposed to do if it encounters a <b>$finish<\/b> or <b>$display<\/b> statement?<\/p>\n<p>In general, code which calls system tasks (those beginning with a dollar sign) are not synthesizable. Also, you can not use the #<em>number<\/em> construct to advance time in RTL code. Technically, strictly speaking you can use the #<em>number<\/em> construct in synthesizable code, but the synthesis tool will ignore it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to cover a little bit more on simulation. In addition, we&#8217;ll discuss an important subject that causes some confusion. Blocking Versus Non-Blocking Assignments The very observant reader of the last tutorial might have noticed &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial3\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":226,"menu_order":3,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-255","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":8,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/255\/revisions"}],"predecessor-version":[{"id":893,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/255\/revisions\/893"}],"up":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/226"}],"wp:attachment":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/media?parent=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}