Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Sequential Logic

Flip-Flops and always @(posedge clk) in Verilog

Learn sequential logic in Verilog: how clocked always blocks describe D flip-flops, registers with enable, edge detection, always_ff, setup and hold timing, with a runnable C clocked-state model.

Combinational logic has no memory. Sequential logic does: its outputs depend on both current inputs and stored state. The storage element of almost every digital design is the flip-flop, which copies its input d to its output q at one precise moment, the active edge of a clock. This lesson shows how Verilog describes flip-flops and how a clocked always block differs from the combinational one.

The D flip-flop

On each rising clock edge, q takes the value d; between edges q holds still.

VERILOG
module dff (
    input  wire clk,
    input  wire d,
    output reg  q
);
    always @(posedge clk)
        q <= d;
endmodule
  • @(posedge clk) waits for the clock to go from 0 to 1 (also counting 0 to x and x to 1). negedge is the falling edge.
  • <= is the non-blocking assignment, the correct choice for flip-flops (see the next lesson for why).
  • q is a reg because it is assigned in a procedural block; here it becomes a real flip-flop.

SystemVerilog states the intent more clearly:

VERILOG
always_ff @(posedge clk)
    q <= d;

always_ff tells tools "this must be sequential logic" and flags code that cannot be.

flowchart LR
  d --> FF["D flip-flop"]
  clk --> FF
  FF --> q

The flip-flop samples d on the rising clock edge and holds it on q.

Registers: many flip-flops, one clock

A register is a vector of flip-flops that share a clock:

VERILOG
reg [7:0] data_q;
always @(posedge clk)
    data_q <= data_in;

Eight flip-flops are created, one per bit.

Enable

A register that only loads when en is true:

VERILOG
always @(posedge clk)
    if (en)
        q <= d;

No else is needed here. In a clocked block, a missing else means "keep the old value", which is exactly what a flip-flop with an enable does. (The same missing else in a combinational block would create a latch, so the context matters.) FPGA and ASIC flip-flops often have a dedicated enable pin, and the tool picks it up automatically.

Next-state style

A popular style separates "compute the next value" from "register it":

VERILOG
reg [3:0] count_q, count_d;

always @* begin                 // combinational: next value
    count_d = count_q;
    if (en) count_d = count_q + 4'd1;
end

always @(posedge clk)           // sequential: store it
    count_q <= count_d;

This is the foundation of the two-process FSM style in chapter 6. The combinational block must assign count_d on every path (here, by the default on the first line).

Detecting edges of a signal

A synchronous design handles slow external signals by sampling them with a clock. To detect a rising edge of btn, register it and compare with the previous value:

VERILOG
reg btn_q;
always @(posedge clk) btn_q <= btn;
wire rise = btn & ~btn_q;
wire fall = ~btn & btn_q;

rise is high for exactly one clock cycle after the input goes from 0 to 1. For a signal from another clock domain you also need a synchronizer (two flip-flops in a row) to avoid metastability.

Setup, hold and the clock period

A flip-flop needs its input stable for a short time before the clock edge (setup time) and after it (hold time). Every path between flip-flops must satisfy

TEXT
clk_to_q + logic_delay + setup <= clock_period

which is what static timing analysis verifies. The more logic between two registers, the longer the period must be, which is why designers pipeline long paths into stages. Simulation of RTL ignores these delays; timing is checked after synthesis.

Simulation tip: the clock

A testbench usually generates the clock with a toggling loop:

VERILOG
reg clk = 1'b0;
always #5 clk = ~clk;      // period 10 time units

Because this uses a delay, it is simulation only and never part of the design.

Stand-in: a clocked register in C

C has no concurrency, but we can model a synchronous circuit with two steps per clock cycle: compute every next value from the current state, then update all registers at once. This is not Verilog; it shows the register, enable, and edge detector of this lesson.

C Stand-in: register, enable and edge detector (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice q holds 6 while en is 0 (cycles 2 and 3), and rise pulses for exactly one cycle (cycle 2).

Stand-in: pipelining shortens the longest path (C)

C Stand-in: a two-stage pipeline delays results by a cycle (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The first input (1) becomes 2 in stage1 and, one cycle later, 3 (= 1*2 + 1) in stage2. Latency grows by one cycle per stage; throughput stays one result per cycle.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does @(posedge clk) wait for?

    posedge is the rising edge; negedge is the falling edge.
  2. 2. In a clocked always block, what does a missing else on an if (en) assignment mean?

    A flip-flop naturally holds its value between loads; this describes a register with enable.
  3. 3. How do you detect a rising edge of a synchronous input btn?

    A rising edge means now 1 and previously 0.
  4. 4. What must hold for every flip-flop to flip-flop path?

    The data must arrive before the next edge minus the setup time.

Summary

  • A flip-flop copies d to q on the active clock edge; Verilog writes it as always @(posedge clk) q <= d;.
  • A register is a vector of flip-flops; an if (en) without else gives a register with enable.
  • The next-state style splits a combinational block (compute) from a clocked block (store).
  • Detect edges by registering the previous value; check timing with clk_to_q + logic + setup <= period.
  • Pipelining adds registers to shorten paths at the cost of latency.

Next: Blocking vs Non-Blocking Assignments