Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

Flip Floppy

December 03, 2011

Of course, inputs from the outside world might not be synchronized, but don't worry about that for now. Inputs from other parts of our circuit can easily be synchronized to a single clock and FPGAs have special provisions for handling clock signals.

Typically, a flip flop has two outputs: Q and Q' (often written as a Q with a bar over it). Q' is simply the inverse of the normal output. So if Q=1 then Q'=0 and vice versa. When we build flip flops with Verilog, we can use either output (or both).

There are many common types of flip flops. Perhaps the most common is the D (data) flip flop. In addition to a clock input and the Q outputs, a D flip flop has a single input (named, unsurprisingly, D). When the clock makes the enabled transition, whatever value D has becomes the new Q value. In Verilog:

module d_ff(input clk, input d, output reg q, output q_not);
assign q_not=~q;
always @(posedge clk)

This shows a few new Verilog constructs. The always statement is similar to initial. It causes the associated code to execute when its "sensitivity list" is true. In this case, the code (q=d) executes with the clk signal has a positive edge (that is, goes from a 0 to a 1).

You can specify combinatorial logic using always too:

always @(a or b or c)

That causes the line of code to execute when a, b, or c changes. You can also just use always with no list to cause something to — no surprise — always execute.

Try writing a test bench for the D flip flop (you can find mine here). Hint: You can generate a clock (using a reg type) like this:

Initial clk=0;   // start clock low
always #1 clk=~clk;  // flip clock every cycle

If you need more than one line of code in the always block you can use begin end. Here's another version of the D flip flop: module d_ff(input clk, input d, output reg q, output reg q_not); always @(posedge clk) begin q=d; q_not=~q; // uh oh end endmodule

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.