Having played around with Verilog for some time now, I decided to graduate to implementing designs on Alltera CycloneIV FPGA using the Quartus suite.

Starting with a simple D flip flop, I face the first road block.

Problem Statement: The output(Q) of a D-FF is required follow the input signal (as it should be), and here goes the code:

//module D flip flip

module d(q_w,q0_w,d,c); 
output q_w,q0_w;
input c,d;

//wire c;
reg q,q0;
wire q_w,q0_w;
assign q_w=q;
assign q0_w=q0;

       q=1'b0; q0=1'b1;
always @ (posedge c)
     q0<= ~d;

The input signal 'd' also transitions at the same positive edge of a common clock signal ('c' for module D-FF). This is causing the circuit to not behave properly as the transition and testing happens at same edge of clock. Also that I have read it is better to stick to one edge of clock in designs (as a good practice) and am hence abstained from using negative edge of the clock.

Having encountered the same issue when simulating I used a delay to do away with the problem

always @ (posedge c)
         #2 q=d;
         q0= ~d;

Despite searching a lot I cant figure out a way to implement the functionality as required in the hardware. I tried adding buffer elements before the clock signal enters D-FF module but in vain.

PS: I haven't yet burnt any of this on the FPGA yet. My apprehensions are marked by the results of Quartus Simulation.

I have manually tweaked the input D at point to check the follow up of the circuit

I have manually tweaked the input D at a point to check the follow up of the circuit


Your flip-flop implementation is correct.

What @duskwuff is pointing out is that when you implemented your test waveform (c and d) you have d transitioning at the exact same time as c. This is not how whatever is driving your flip-flop will really work. The input has to be set up before the clock edge. If the input is transitioning at the clock edge--remember that while the edges look square in the logic analyzer, in reality the voltage takes a certain amount of time to transition from one state to another--your flip-flop may read high, low, or some intermediate voltage that may really confuse your logic.

You can see what I'm talking about in your screen shot.

At the first positive clock edge, d transitions from low to high. But because the new value of d (high) was not set up before the clock edge, your flip-flop sees the original value (low) and outputs that on q. The same thing happens on the second positive clock edge.

However, between the second and third positive clock edges, at 140ns, you change d from low to high. So when the clock goes positive the third time at 150ns, it sees the high value, and then q goes high about 8ns later.

Your test sequence should look something like:

  1. set clock high
  2. wait 1/4 clock cycle
  3. set d to some value
  4. wait 1/4 clock cycle
  5. set clock low
  6. wait 1/2 clock cycle
  7. set clock high (it's now been one full cycle from last positive edge)
  8. wait 1/4 clock cycle
  9. check q
  • \$\begingroup\$ Thanks for the elaborate explanation. It helped. I may change the test waveforms as per what you mentioned. But still, how do I make sure that the waveform is actually shifted in the hardware so as to avoid the problem. Or will this code run just fine on hardware as there will automatically be delays? \$\endgroup\$ – Prateek Sharma Mar 11 '16 at 5:20
  • 1
    \$\begingroup\$ As long as everything is driven from the same clock you'll be fine. It's trickier when dealing with asynchronous signals, like from an external serial line. Cross that bridge when you come to it. \$\endgroup\$ – Willis Blackburn Mar 11 '16 at 12:19
  • \$\begingroup\$ How about an up vote? :-) \$\endgroup\$ – Willis Blackburn Mar 11 '16 at 12:19
  • \$\begingroup\$ Sry was away over the weekend. Anywho, just to clarify one last time. This piece of code will run perfectly on the hardware without me requiring any changes? is that right. Thanks again \$\endgroup\$ – Prateek Sharma Mar 14 '16 at 8:36
  • \$\begingroup\$ It should work. But don't take my word for it, synthesize it and see for yourself. \$\endgroup\$ – Willis Blackburn Mar 14 '16 at 11:47

First: don't use delays in implementations. Delays cannot be synthesized in hardware; they should be confined to testbench code.

Second: the situation you're testing, where d changes at a clock edge, is a timing violation. Failing under these circumstances is perfectly normal; virtually all flip-flops will fail or exhibit inconsistent behavior when subjected to this input. Adjust your testbench to move the transitions of d such that they don't coincide with the clock edges.

  • \$\begingroup\$ So do you mean to say that if I run my contentious code on actual FPGA then the result would be consistent?Secondly, aren't tetstbenches, purely simulation tool? If yes, whats the point of tweaking the testbench code. If not necessarily, then how so I 'move the transitions of d such that they don't coincide with the clock edges' if not by using delays. Thanks \$\endgroup\$ – Prateek Sharma Mar 11 '16 at 2:16
  • \$\begingroup\$ Having run on simulation tools earlier, I am now looking only to make it work on actual hardware. Beats me. \$\endgroup\$ – Prateek Sharma Mar 11 '16 at 2:32
  • \$\begingroup\$ Changing the D input to any flip-flop during a clock transition will cause unpredictable behavior. Don't do this in your tests. Edit the input waveform such that this doesn't happen. \$\endgroup\$ – duskwuff -inactive- Mar 11 '16 at 3:12
  • \$\begingroup\$ Thanks. Even though, I may edit the wave form for testing, how do I make sure that the waveform is actually 'shifted' when run on the hardware. \$\endgroup\$ – Prateek Sharma Mar 11 '16 at 5:18
  • \$\begingroup\$ Checking for these timing violations is a part of standard timing analysis, and the software will take care of that for you under most circumstances. \$\endgroup\$ – duskwuff -inactive- Mar 11 '16 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.