{"id":303,"date":"2015-01-29T08:58:41","date_gmt":"2015-01-29T16:58:41","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?page_id=303"},"modified":"2016-07-05T14:31:25","modified_gmt":"2016-07-05T22:31:25","slug":"tutorial6","status":"publish","type":"page","link":"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial6\/","title":{"rendered":"Tutorial 6: Counting Seconds"},"content":{"rendered":"<p>In this tutorial, we&#8217;ll have the PmodSSD count seconds.<\/p>\n<p>We need a counter which increments every second. We&#8217;re going to want to display the counter value on the PmodSSD display. We&#8217;ll do this in three parts: first, we&#8217;ll need to know when to increment the counter; second, we need to maintain the counter; and finally we need to feed the count value to the display.<\/p>\n<h1>A Pulse Every Second<\/h1>\n<p>In our last tutorial, we generated a pulse every millisecond. We can use that same pulse to enable a counter that will count to 1000, incrementing every millisecond. That makes a pulse every second for us. Here&#8217;s the code to do that.<\/p>\n<pre><code>integer ms_count = 0;\r\nreg sec_pulse;\r\nalways @(posedge clk)\r\n  begin\r\n    sec_pulse &lt;= 0;\r\n    if (ms_pulse)\r\n      if (ms_count == 999)\r\n        begin\r\n          ms_count &lt;= 0;\r\n          sec_pulse &lt;= 1;\r\n        end\r\n      else\r\n        ms_count &lt;= ms_count+1;\r\n  end\r\n<\/code><\/pre>\n<p>We&#8217;ll also need to use the <em>sec_pulse<\/em> signal to increment a counter each second. Here is the code for that.<\/p>\n<pre><code>    reg [7:0] sec_count = 0;\r\n    always @ (posedge clk)\r\n        if (sec_pulse)\r\n            sec_count &lt;= sec_count +1;\r\n<\/code><\/pre>\n<p>We can simulate this for a few milliseconds. You&#8217;ll be able to see the <em>ms_pulse<\/em> signal pulse every millisecond. What about the <em>sec_pulse<\/em> signal? We&#8217;d need to simulate for a full second to see that pulse, which might take a while. It would also use up a lot of disk space when we go to save all the data. A full second is 100,000,000 clock cycles. We really need a way to somehow short circuit the millisecond counter while we&#8217;re simulating. If you&#8217;re simulating long counts, like we are here, this is a pretty common requirement. We&#8217;ll introduce a parameter to our <em>top<\/em> module.<\/p>\n<h2>Speeding Up the Simulation<\/h2>\n<p>You can think of a parameter as a constant used in the design. The parameter&#8217;s value will never change during the simulation of a module. However, different instances of a module can\u00a0have different parameter values. First, we&#8217;ll\u00a0introduce a parameter that will\u00a0hold the count value for <em>counter<\/em>. The default value of this parameter is 100,000. With this default value, the <em>ms_pulse<\/em> signal will pulse once every millisecond. The parameter is declared along with the module, but before the module ports are declared.<\/p>\n<pre><code>module top\r\n#(\r\n  parameter ms_limit = 100000\r\n) (\r\n   input clk,\r\n   input [7:0] switch,\r\n   output reg [7:0] led,\r\n   output reg [6:0] ssd,\r\n   output reg ssdcat\r\n   );\r\n<\/code><\/pre>\n<p>Next\u00a0we&#8217;ll change the counter definition to count up to <em>ms_limit-1<\/em>. This won&#8217;t change our design&#8217;s behavior at all.<\/p>\n<pre><code>  always @(posedge clk)\r\n    begin\r\n      ms_pulse &lt;= 0;\r\n      if (count == ms_limit-1)\r\n\tbegin\r\n\t  count &lt;= 0;\r\n\t  ms_pulse &lt;= 1;\r\n\tend\r\n      else\r\n\tcount &lt;= count+1;\r\n    end\r\n<\/code><\/pre>\n<p>When we synthesize our design, nothing will change. However, now we modify the instance of <em>top<\/em> in our test bench to override the default value of <em>ms_limit<\/em> to just 100. This will make <em>ms_pulse<\/em> pulse every 100 clocks in the simulation instead of every 100,000.<\/p>\n<pre><code>  top #(.ms_limit(100)) top\r\n    (\r\n     .clk(clk),\r\n     .switch(switch),\r\n     .led(led)\r\n     );\r\n<\/code><\/pre>\n<p>Rerun your simulation and watch\u00a0the second counter, which now changes every millisecond.<\/p>\n<p>Reuse the code to drive the <em>ssd<\/em> output from the previous tutorial.<\/p>\n<pre><code>  wire [3:0]  digit;\r\n  always @(posedge clk)\r\n    case (digit)\r\n      0: ssd &lt;= 7'b1111110;\r\n      1: ssd &lt;= 7'b0110000;\r\n      2: ssd &lt;= 7'b1101101;\r\n      3: ssd &lt;= 7'b1111001;\r\n      4: ssd &lt;= 7'b0110011;\r\n      5: ssd &lt;= 7'b1011011;\r\n      6: ssd &lt;= 7'b1011111;\r\n      7: ssd &lt;= 7'b1110000;\r\n      8: ssd &lt;= 7'b1111111;\r\n      9: ssd &lt;= 7'b1110011;\r\n      10: ssd &lt;= 7'b1110111;\r\n      11: ssd &lt;= 7'b0011111;\r\n      12: ssd &lt;= 7'b1001110;\r\n      13: ssd &lt;= 7'b0111101;\r\n      14: ssd &lt;= 7'b1001111;\r\n      15: ssd &lt;= 7'b1000111;\r\n    endcase\r\n\r\n  assign digit = ssdcat ? sec_count[7:4] : sec_count[3:0];\r\n<\/code><\/pre>\n<p>Make sure to place the code in your <em>top<\/em> module such that signals are declared before they are used. You should now build a new FPGA and check that the digits on the display increment every second.<\/p>\n<h1>But is the Output Correct?<\/h1>\n<p>All right, so we&#8217;ve simulated our design. But is the output correct? It&#8217;s not exactly easy to check binary codes for segment values in the display that switch between digits every millisecond. Let&#8217;s write some Verilog code to do that checking for us.<\/p>\n<h2>A Model of a Seven-Segment Digit<\/h2>\n<p>We need a model of the seven segment display. We can then put our model into the test bench, and it&#8217;ll take the segment inputs and replace that with the numeric value which the segments represent. The model will be another Verilog module. The model&#8217;s input will be the segment value and the enable value. The digit numeric value will be the output. Let&#8217;s start with the module declaration:<\/p>\n<pre><code>module ssd_digit\r\n   input enable,\r\n   input [6:0] ssd,\r\n   output reg [3:0] value\r\n   );\r\n<\/code><\/pre>\n<p>Now,\u00a0we need to turn the segment values back into a numeric value. We&#8217;ll use a case statement to do this.<\/p>\n<pre><code>  always @(*)\r\n    if (enable)\r\n      case (ssd)\r\n        7'b1111110: value = 0;\r\n        7'b0110000: value = 1;\r\n        7'b1101101: value = 2;\r\n        7'b1111001: value = 3;\r\n        7'b0110011: value = 4;\r\n        7'b1011011: value = 5;\r\n        7'b1011111: value = 6;\r\n        7'b1110000: value = 7;\r\n        7'b1111111: value = 8;\r\n        7'b1110011: value = 9;\r\n        7'b1110111: value = 10;\r\n        7'b0011111: value = 11;\r\n        7'b1001110: value = 12;\r\n        7'b0111101: value = 13;\r\n        7'b1001111: value = 14;\r\n        7'b1000111: value = 15;\r\n      endcase\r\n<\/code><\/pre>\n<p>This is just the inverse of the table in our <b>top.v<\/b> file. It takes the segment values and turns them\u00a0back into a numeric value. However, we&#8217;ve got\u00a0a couple issues. First, there are really two different values for the digit &#8220;C&#8221;. To solve this, we&#8217;ll use two different parameters:\u00a0an uppercase &#8220;C&#8221; and\u00a0a lowercase &#8220;c&#8221;. To handle both\u00a0cases, we&#8217;ll need to add another entry in our table.<\/p>\n<pre><code>7'b0011001: value = 12;<\/code><\/pre>\n<p>There&#8217;s another issue:\u00a0what if none of the segment values match a table entry? Let&#8217;s use a default value in this case.<\/p>\n<pre><code>default: value = 'bx;<\/code><\/pre>\n<p>This entry in the case statement will cause the <em>value<\/em> signal to be set to X (unknown) if none of the other entries match <em>segments<\/em>. The default statement should come just before the <b>endcase<\/b> statement.<\/p>\n<h2>Instantiating the Model in the Test Bench<\/h2>\n<p>We need two digits in our test bench, as well as eight wires to hook up to the output of our digit models. Also, we&#8217;re going to need an enable signal for each digit. The enable will be our <em>ssdcat<\/em> signal. We know from before that the low digit is active when <em>ssdcat<\/em> is low and when <em>ssdcat<\/em> is high, the high digit is active. Here is the code.<\/p>\n<pre><code>  wire [7:0] digits;\r\n  ssd_digit PmodSSD0\r\n    (\r\n     .enable(~ssdcat),\r\n     .ssd(ssd),\r\n     .value(digits[3:0])\r\n     );\r\n  \r\n  ssd_digit PmodSSD1\r\n    (\r\n     .enable(ssdcat),\r\n     .ssd(ssd),\r\n     .value(digits[7:4])\r\n     );\r\n<\/code><\/pre>\n<h1>A Self-Checking Test<\/h1>\n<p>Everything looks good from here. We can build an FPGA and watch the seconds tick by (in hex, of course). Running a simulation and looking at the output value, we see the simulation counts correctly. What else do we need to do? In the Real World\u00ae, this might not be enough. What if our test could check itself? What would the\u00a0benefit of that be? Well, if you have a self-checking test, you then have an automated test you can run whenever your code changes. If it passes the test, you&#8217;ll know your code works. You don&#8217;t have to build an FPGA and watch the results, and you don&#8217;t have to examine a test waveform to know whether the code works. Implementing a self-checking test saves a tremendous amount of work and unpredictability: something that&#8217;s well worth doing.<\/p>\n<p>In our case here, we need to count the seconds outside of our FPGA. Ideally, you&#8217;ll write something called a &#8220;behavioral model&#8221;. A behavioral model if a Verilog model of your design, which behaves how you want your FPGA to, but with less complexity. Your test bench just runs your FPGA and the model and checks to see\u00a0that the FPGA outputs match those of the model. Let&#8217;s create a model of our second counter.<\/p>\n<h2>Second Counter Model<\/h2>\n<p>The model needs as input the clock signal, and it should output an eight-bit value which counts the seconds. Also, remember that we short-circuited the millisecond counter to make the simulations run faster. I write the model with the same parameter as the actual design and override it from the test bench. That keeps the knowledge of how the parameter is overridden to the test bench only. A good rule of thumb is to keep knowledge as local as possible.<\/p>\n<pre><code>`timescale 1ns\/1ns\r\nmodule model\r\n  #(\r\n    parameter ms_limit = 100000\r\n    )\r\n  (\r\n   input clk,\r\n   output [7:0] seconds\r\n   );\r\n  \r\n  integer counter = 0;\r\n  always @(posedge clk)\r\n    counter &lt;= counter+1;\r\n\r\n  assign seconds = counter \/ (ms_limit * 1000);\r\n\r\nendmodule\r\n<\/code><\/pre>\n<p>That&#8217;s the entire model. It&#8217;s pretty easy to tell it&#8217;ll count the seconds properly, and it&#8217;s much simpler than our RTL design. Now, we&#8217;re just instantiating the model in our test bench, like this:<\/p>\n<pre><code>  wire [7:0] model_seconds;\r\n  model\r\n    #(.ms_limit(100))\r\n  model\r\n    (\r\n     .clk(clk),\r\n     .seconds(model_seconds)\r\n     );\r\n<\/code><\/pre>\n<p>Remember, we need to override the <em>ms_limit<\/em> parameter in the model instance.<\/p>\n<h2>Comparing Results<\/h2>\n<p>Now, we just have to compare the results coming out of the <em>ssd_digit<\/em> modules with what comes out\u00a0of our behavioral model. Something like this:<\/p>\n<pre><code>  always @(posedge clk)\r\n    begin\r\n      num_checks = num_checks+1;\r\n      if (digits != model_seconds)\r\n\tbegin\r\n\t  $display(\"ERROR: digits value %0x does not match expected value %0x at time %0fns\",\r\n\t\t   digits,model_seconds,$realtime);\r\n\t  num_errors = num_errors+1;\r\n\tend\r\n    end\t\r\n<\/code><\/pre>\n<p>Note that the incrementing of <em>num_checks<\/em> and <em>num_errors<\/em> is not being done with a nonblocking assignment. Remember, this is a test bench and not RTL. There is an additional check every clock edge being done on the LED outputs, and we want <em>both<\/em> checks to count. If we used a nonblocking assign,x we&#8217;d only count one check each clock cycle and could mask errors depending on which always block runs first.<\/p>\n<p>Also, I have modified the test bench to wait for 20 seconds (or milliseconds since we short circuit the simulation) and then stop. So I have replaced the repeat statement used to wait to finish the simulation with a Verilog <b>wait<\/b> statement. Like this.<\/p>\n<pre><code>  initial\r\n    begin\r\n      wait (model_seconds == 20);\r\n      $display(\"Simulation complete at time %0fns.\",$realtime);\r\n      if (num_errors &gt; 0)\r\n\t$display(\"*** Simulation FAILED %0d\/%0d\",num_errors,num_checks);\r\n      else\r\n\t$display(\"*** Simulation PASSED %0d\/%0d\",num_errors,num_checks);\r\n      $finish;\r\n    end\r\n<\/code><\/pre>\n<p>Now we just run the simulation and see how it works.<\/p>\n<pre><code>run all\r\nERROR: digits value 0 does not match expected value 1 at time 1000000.000000ns\r\nERROR: digits value 0 does not match expected value 1 at time 1000010.000000ns\r\nERROR: digits value 0 does not match expected value 1 at time 1000020.000000ns\r\nERROR: digits value 11 does not match expected value 1 at time 1001010.000000ns\r\nERROR: digits value 0 does not match expected value 1 at time 1002010.000000ns\r\nERROR: digits value 11 does not match expected value 1 at time 1003010.000000ns\r\nERROR: digits value 0 does not match expected value 1 at time 1004010.000000ns\r\nERROR: digits value 11 does not match expected value 1 at time 1005010.000000ns\r\n<\/code><\/pre>\n<p>This is followed by <em>lots<\/em> of error messages and finally wraps up with<\/p>\n<pre><code>ERROR: digits value 33 does not match expected value 13 at time 19995010.000000ns\r\nERROR: digits value 11 does not match expected value 13 at time 19996010.000000ns\r\nERROR: digits value 33 does not match expected value 13 at time 19997010.000000ns\r\nERROR: digits value 11 does not match expected value 13 at time 19998010.000000ns\r\nERROR: digits value 33 does not match expected value 13 at time 19999010.000000ns\r\nSimulation complete at time 19999990.000000ns.\r\n*** Simulation FAILED 18136\/4000000\r\n$finish called at time : 19999990 ns : File \"\/home\/pete\/tutorial6\/tutorial6.srcs\/sim_1\/new\/bench.v\" Line 85\r\n<\/code><\/pre>\n<p>What has gone wrong? Obviously with 18136 failures out of 4000000 comparisons things are <em>mostly<\/em> correct. Let&#8217;s look at the waveform from the simulation at time 1,000,000ns. Really zoom in to that time so you only see 10 or so clock cycles. Here is what my simulation is showing me.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-323\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave1-300x114.png\" alt=\"Tutorial6_Wave1\" width=\"300\" height=\"114\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave1-300x114.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave1-1024x389.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave1-500x190.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave1.png 1130w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Well, I can see why <em>sec_count<\/em> does not match <em>model_seconds<\/em>. The timing is off by a few clock cycles. This is due to some <em>latency<\/em> in the design. We can correct for this in a number of ways. We can reduce the latency in the design, or we can add latency to the model. Or both. One thing I notice is that the <em>ms_pulse<\/em> and <em>sec_pulse<\/em> signals are not coincident. This may be OK, but we could reduce the latency some by bringing in sec_pulse by one clock cycle. This also makes the design a little bit cleaner and in a complex design might help keep things simple.<\/p>\n<p>Let&#8217;s look again at the code which generates <em>ms_pulse<\/em>.<\/p>\n<pre><code>\r\n  integer     count = 0;\r\n  reg \t      ms_pulse = 0;\r\n  always @(posedge clk)\r\n    begin\r\n      ms_pulse &lt;= 0;\r\n      if (count == ms_limit-1)\r\n\tbegin\r\n\t  count &lt;= 0;\r\n\t  ms_pulse &lt;= 1;\r\n\tend\r\n      else\r\n\tcount &lt;= count+1;\r\n    end\r\n<\/code><\/pre>\n<p>Notice how <em>ms_pulse<\/em> is placed in a flip-flop? Also, notice how it is high when the <em>count<\/em> value is zero. Let&#8217;s change the definition of <em>ms_pulse<\/em> to be combinational. This will eliminate the delay of one clock.<\/p>\n<p>The result looks like this.<\/p>\n<pre><code>  integer count = 0;\r\n  wire ms_pulse = count == ms_limit-1;\r\n  always @(posedge clk)\r\n    if (ms_pulse)\r\n      count &lt;= 0;\r\n    else\r\n      count &lt;= count+1;\r\n<\/code><\/pre>\n<p>Hey, that is even simpler to code. Let&#8217;s run our simulation and see if we have changed things.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-325\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave2-300x97.png\" alt=\"Tutorial6_Wave2\" width=\"300\" height=\"97\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave2-300x97.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave2-1024x332.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave2-500x162.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave2.png 1105w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Yep, we pulled that <em>ms_pulse<\/em> signal in one cycle. And we got rid of one of the error messages. Still our output only updates after the <em>sec_pulse<\/em> signal. Can we pull that one in too? It looks like we can pull the same trick with that. Here is the modified code.<\/p>\n<pre><code>  integer ms_count = 0;\r\n  wire sec_pulse = ms_count == 999;\r\n  always @(posedge clk)\r\n    if (ms_pulse)\r\n      if (sec_pulse)\r\n\tms_count &lt;= 0;\r\n      else\r\n\tms_count &lt;= ms_count+1;\r\n<\/code><\/pre>\n<p>Let&#8217;s run that. Here are the first error messages and the waveform. It looks like we have introduced a bug. Notice how sec_count increments every clock cycle before the ms_pulse signal triggers. You can see it in the waveform.<\/p>\n<pre><code>run 1 ms\r\nERROR: digits value 10 does not match expected value 0 at time 999170.000000ns\r\nERROR: digits value 10 does not match expected value 0 at time 999180.000000ns\r\nERROR: digits value 10 does not match expected value 0 at time 999190.000000ns\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-327\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3-300x87.png\" alt=\"Tutorial6_Wave3\" width=\"300\" height=\"87\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3-300x87.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3-1024x296.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3-1000x288.png 1000w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3-500x145.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave3.png 1379w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If you look at sec_pulse you see it is no longer a pulse. This causes sec_count to increment rapidly. We need to condition sec_pulse to now only happen when ms_pulse is also happening. Something like this.<\/p>\n<pre><code>  wire sec_pulse = ms_count == 999 &amp;&amp; ms_pulse;<\/code><\/pre>\n<p>Let&#8217;s make the change and run again. Here is the waveform.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-328\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave4-300x98.png\" alt=\"Tutorial6_Wave4\" width=\"300\" height=\"98\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave4-300x98.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave4-1024x336.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave4-500x164.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave4.png 1106w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Notice that the pulses are each for a single clock and they all line up? We are still getting an error message since things still don&#8217;t quite line up. This is because there is still a delay of a clock when encoding the seven segment outputs. We should leave that clock in there if possible. It lets the outputs of the FPGA leave the chip on the rising edge of a clock. This will hide any internal delays and make interfacing to the outside world easier. It doesn&#8217;t much matter for an LED, but for faster devices it can really help.<\/p>\n<p>So, we need to add a cycle of delay to the test bench. Let&#8217;s do that now. Just change the model to register the seconds output on the rising edge of clock. Like this.<\/p>\n<pre><code>  always @(posedge clk)\r\n    seconds &lt;= counter \/ (ms_limit * 1000);\r\n<\/code><\/pre>\n<p>Be sure to also change the output declaration for <em>seconds<\/em> to be a <strong>reg<\/strong>.<\/p>\n<p>Rerunning the simulation causes the errors we were attacking to go away. Here is the waveform right at 1ms.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-330\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave5-300x102.png\" alt=\"Tutorial6_Wave5\" width=\"300\" height=\"102\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave5-300x102.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave5-1024x349.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave5-500x170.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave5.png 1101w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Tracking more bugs<\/h2>\n<p>It looks like we still have some errors though. Here is the next error message.<\/p>\n<pre><code>ERROR: digits value 11 does not match expected value 1 at time 1001000.000000ns<\/code><\/pre>\n<p>Let&#8217;s zoom in on that time and see what&#8217;s going on.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-331\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave6-300x92.png\" alt=\"Tutorial6_Wave6\" width=\"300\" height=\"92\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave6-300x92.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave6-1024x315.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave6-500x154.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave6.png 1102w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Look at what happens to the <em>digits<\/em> value. Right as <em>ssdcat<\/em> rises it has a value of &#8216;h11, when the correct value is &#8216;h01. It then goes back to &#8216;h01 the following clock cycle. It looks like the <em>ssdcat<\/em> signal is leading the <em>ssd<\/em> output by a clock cycle. This is causing the wrong data to go to the display for a clock cycle. You could never see this on the display and might not matter in this instance. But usually you are sending data to another chip and those can see what is happening on every cycle and every cycle needs to be correct.<\/p>\n<p>The bug is caused by our <em>ssdcat<\/em> output not having the same pipeline delay as the <em>ssd<\/em> output. We can fix that by delaying it one clock. Here is how I chose to do it.<\/p>\n<pre><code>  reg  ms_pulse_delay = 0;\r\n  always @(posedge clk)\r\n    ms_pulse_delay &lt;= ms_pulse;\r\n  \r\n  initial ssdcat = 0;\r\n  always @(posedge clk)\r\n    if (ms_pulse_delay) ssdcat &lt;= ~ssdcat;\r\n<\/code><\/pre>\n<p>Delaying the <em>ssdcat<\/em> signal seems to do the trick. But if you simulate even further you will find another group of error messages. Here they are<\/p>\n<pre><code>ERROR: digits value 0 does not match expected value 10 at time 16000010.000000ns\r\nERROR: digits value 0 does not match expected value 10 at time 16000020.000000ns\r\nERROR: digits value 0 does not match expected value 10 at time 16000030.000000ns\r\n<\/code><\/pre>\n<p>You know the drill by now. Let&#8217;s look at the waveform around 16ms into the simulation. Here is what I have.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-334\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave7-300x83.png\" alt=\"Tutorial6_Wave7\" width=\"300\" height=\"83\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave7-300x83.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave7-1024x285.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave7-500x139.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2015\/01\/Tutorial6_Wave7.png 1378w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Sure enough the digits output goes back to &#8216;h00 rather than properly wrapping to &#8216;h10. Why does it do this? The error persists for a millisecond and then goes away. If you dig into it a little you will realize that only one of the hex digits in the <em>digits<\/em> signal can change at a time. The one that changes is indicated by <em>ssdcat<\/em>. Once <em>ssdcat<\/em> changes state again the digit then gets updated and the messages go away &#8211; until of course we go from &#8216;h1f to &#8216;h20. The fix for this is to be a little more selective on when we check the digits. We can update this in our test bench.<\/p>\n<p>Here is the modified check that I have now. I use two four bit values to hold the digits I want to check, one from the model and one from the FPGA.<\/p>\n<pre><code>  reg [3:0] check_digits;\r\n  reg [3:0] check_model_digits;\r\n  always @(posedge clk)\r\n    begin\r\n      check_digits = ssdcat ? digits[7:4] : digits[3:0];\r\n      check_model_digits = ssdcat ? model_seconds[7:4] : model_seconds[3:0];\r\n      num_checks = num_checks+1;\r\n      if (check_digits != check_model_digits)\r\n\tbegin\r\n\t  if (num_errors &lt; 100)\r\n\t    $display(\"ERROR: check_digits value %0x does not match expected check_model_digits value %0x at time %0.0fns\",\r\n\t\t     check_digits,check_model_digits,$realtime);\r\n\t  num_errors = num_errors+1;\r\n\tend\r\n    end\t\r\n<\/code><\/pre>\n<p>I also took the liberty of limiting the number of errors that are printed out to keep from having tens of thousands of error lines in my log file. When I run the test with these changes I get the beautiful message below.<\/p>\n<pre><code>run all\r\nSimulation complete at time 20000000.000000ns.\r\n*** Simulation PASSED 0\/4000002\r\n$finish called at time : 20 ms : File \"\/home\/pete\/tutorial6\/tutorial6.srcs\/sim_1\/new\/bench.v\" Line 89\r\nrun: Time (s): cpu = 00:00:15 ; elapsed = 00:00:14 . Memory (MB): peak = 6060.445 ; gain = 0.000 ; free physical = 1068 ; free virtual = 4552\r\n<\/code><\/pre>\n<p>Four million checks and every one passed. Here are my files for this tutorial.<\/p>\n<ul>\n<li><a title=\"top.v\" href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/tutorials\/tutorial6\/top.v\">top.v<\/a><\/li>\n<li><a title=\"bench.v\" href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/tutorials\/tutorial6\/bench.v\">bench.v<\/a><\/li>\n<li><a title=\"ssd_digit.v\" href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/tutorials\/tutorial6\/ssd_digit.v\">ssd_digit.v<\/a><\/li>\n<li><a title=\"model.v\" href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/tutorials\/tutorial6\/model.v\">model.v<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll have the PmodSSD count seconds. We need a counter which increments every second. We&#8217;re going to want to display the counter value on the PmodSSD display. We&#8217;ll do this in three parts: first, we&#8217;ll need to &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial6\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":226,"menu_order":6,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-303","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/303","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=303"}],"version-history":[{"count":26,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/303\/revisions"}],"predecessor-version":[{"id":988,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/303\/revisions\/988"}],"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=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}