SystemVerilog for Clocked Logic: Registers

Write a register with reset and enable in always_ff, clock it from a bench, and see why clocked blocks use <=.

You Will Be Able To

  • Write a register with a synchronous reset and an enable.
  • Explain why a clocked block uses the nonblocking assignment <=.
  • Tell a synchronous reset from an asynchronous reset in code.
  • Read a register on a waveform, one clock edge at a time.
Lesson
G6.1
Time
25 min
Runs on
Your browser
Check
5 questions

A designer copied a two-stage pipeline and typed = where <= was meant. In simulation the data arrived one clock early. Synthesis built 8 flip-flops where the design needed 16. One character removed a whole register.

Two Stages Written with <= and with =The same input d goes into both pipelines. Each cycle ends at a rising clock edge.
0123clkd112233q2 with <=xx1122q2 with =xx112233one edge: = already shows 11
Scroll horizontally ↔
  1. Cycle 0

    Before the first edge both outputs are xx, unknown. The value 11 waits at the input.

  2. Cycle 1

    After one edge the = version already shows 11. The <= version still shows xx: the value is in the first stage.

  3. Cycle 2

    After two edges the <= version shows 11. That is the delay a two-stage pipeline must have.

  4. Cycle 3

    The = version stays one edge ahead in every cycle. It has only one stage.

Measured on the simulator. The pipeline written with = lost its first stage.

A Block That Runs at the Clock Edge

A flip-flop stores one bit. At the rising edge of the clock it copies its input to its output. Then it holds that value until the next rising edge. A register is a row of flip-flops with one shared clock.

In SystemVerilog, always_ff @(posedge clk) describes a register. The block runs once at each rising edge of clk. A signal assigned inside it becomes flip-flops.

The register of this lesson: reset first, then enable, otherwise hold.
always_ff @(posedge clk) begin
  if (rst)     q <= 8'h00;   // reset wins over enable
  else if (en) q <= d;       // load
                             // otherwise q keeps its value
end

The order of the if sets the priority. Reset wins. Next comes the enable, an input that allows a load. With neither, no line runs and q keeps its value.

A missing else is correct here. In a clocked block, keeping the old value is what a flip-flop does. In a combinational block the same gap makes a latch (G4.1).

The Register, Edge by EdgeEach cycle ends at a rising edge. The edge samples rst, en and d of that cycle. The new q shows in the next cycle.
  • Test
  • Storage
  • I/O
denrstqBenchdrives the inputsreg88 flip-flopsReadersees q
d→ reg8en→ reg8rst→ reg8q→ ReaderBenchdrives the inputsreg88 flip-flopsReadersees q

A token on d means the next edge loads that value.

01234567clkrstend3ca57effqxx003ca57e00ff
Scroll horizontally ↔
  1. Cycle 0

    No edge has come yet, so q is xx. rst is 1, and the edge at the end of this cycle will clear the register.

  2. Cycle 1

    q is 00 after the reset edge. Now en is 1 and d is 3c, so the next edge loads 3c.

  3. Cycle 2

    q is 3c. en is 0 in this cycle. d changes to a5, but the register does not take it.

  4. Cycle 3

    q still holds 3c. en is 1 again, so the edge at the end of this cycle loads a5.

  5. Cycle 5

    rst and en are both 1, and d is ff. Reset has priority.

  6. Cycle 6

    q is 00: reset won. en is 1 with rst at 0, so the next edge loads ff.

  7. Cycle 7

    q is ff. en is 0, but the bench raises rst in this cycle, so the next edge clears the register.

Measured: the values are the lines the example prints for edges 0 to 6.

Why Clocked Blocks Use the Nonblocking Assignment

The nonblocking assignment <= works in two steps at each clock edge.

  1. Read

    At the edge, the simulator reads every right-hand side. It uses the values from before the edge.

  2. Update

    After all blocks have read, every left-hand side changes together.

So the order of the lines does not matter, and each stage sees the old value of the stage before it. That is how real flip-flops on one clock behave.

Nonblocking: two stages

always_ff @(posedge clk) begin
  q1 <= d;
  q2 <= q1;   // the old q1
end

Synthesized on this site: 16 flip-flops. q2 shows d two edges later.

Blocking: one stage

always_ff @(posedge clk) begin
  q1 = d;
  q2 = q1;    // the new q1, which is d
end

Synthesized: 8 flip-flops. q2 shows d one edge later. Lint warns: Blocking assignment '=' in sequential logic process.

The blocking assignment = changes q1 at once. The next line then reads the new q1, which is d. Both stages load the same value, so one of them is not needed.

Writing the lines in reverse order makes a simple chain work even with =. It breaks as soon as a stage reads a later stage, and Lint still warns. Use <=.

Synchronous and Asynchronous Reset

A register that was never written holds x. The example prints q=xx before the first edge. A reset gives the register a known value to start from.

A synchronous reset acts only at a clock edge. An asynchronous reset acts at once, with no clock. In code, the difference is the sensitivity list.

Synchronous reset

always_ff @(posedge clk) begin
  if (rst)     q <= 8'h00;
  else if (en) q <= d;
end

Reset rises between two edges: q stays ff until the next edge. 8 flip-flop cells.

Asynchronous reset

always_ff @(posedge clk or posedge rst) begin
  if (rst)     q <= 8'h00;
  else if (en) q <= d;
end

Reset rises between two edges: q becomes 00 at once. 8 flip-flop cells.

Both were measured on the bench of this lesson. The body of the block is the same. Only or posedge rst makes the block run when the reset rises.

A Clock in the Bench

A register needs a clock, so the bench must make one. Two lines are enough.

A clock with a period of 10 ns.
logic clk = 0;
always #5 clk = ~clk;   // flip every 5 ns: rising edges at 5, 15, 25, ... ns

The delay #5 waits 5 ns. The clock flips after each wait, so one full period is 10 ns.

The bench changes the inputs 1 ns after an edge. It prints 1 ns after the next edge. So each printed line shows the inputs that the edge sampled and the q that it produced.

Keep all assignments to one register in one clocked block. Two blocks that assign the same signal are two drivers. Lint reports it, and Synthesize warns of multiple conflicting drivers.

Asked in interviews
  1. Why do we use nonblocking assignments in clocked blocks?

    With <=, every right-hand side is read before any left-hand side changes. Each flip-flop then sees the old value of the others, as in hardware. With =, the result depends on the order of the lines and stages can collapse.

  2. What is the difference between a synchronous and an asynchronous reset?

    A synchronous reset is sampled at the clock edge, like any other input. An asynchronous reset clears the register at once, with no clock. In code, the asynchronous reset is in the sensitivity list.

  3. What does a register with an enable do when the enable is 0?

    It keeps its value. In the code no branch assigns q, and in a clocked block that means hold. In hardware the flip-flop reloads its own output or uses an enable pin.

In Plain Verilog

Older code is written in Verilog-2001. There, the register is a reg and the block is always @(posedge clk).

SystemVerilog

logic [7:0] q;
always_ff @(posedge clk) begin
  if (rst)     q <= 8'h00;
  else if (en) q <= d;
end

The name always_ff says the block must be flip-flops, and tools can check that.

Verilog-2001

reg [7:0] q;
always @(posedge clk) begin
  if (rst)     q <= 8'h00;
  else if (en) q <= d;
end

The same register. The rules for <= and for the reset are the same too.

Lesson R2.1 in the RTL Design track continues from here with old state and next state.

Try It

The first line shows q=xx: no edge has come yet. Edge 0 resets the register. Edges 1, 3, 4 and 6 load d. At edge 2 the enable is 0, so q keeps 3c. At edge 5 reset and enable are both 1, and reset wins.

Then rst rises between edges 6 and 7. The line between edges still shows q=ff. The register clears only at edge 7, because the reset is synchronous.

Change the first line of the block to always_ff @(posedge clk or posedge rst) and run again. The between edges line now shows q=00. Press Synthesize both times: 8 cells, all of them flip-flops.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: before any edge: q=xx e0 rst=1 en=0 d=3c q=00 e1 rst=0 en=1 d=3c q=3c e2 rst=0 en=0 d=a5 q=3c e3 rst=0 en=1 d=a5 q=a5 e4 rst=0 en=1 d=7e q=7e e5 rst=1 en=1 d=ff q=00 e6 rst=0 en=1 d=ff q=ff between edges: rst=1 q=ff e7 rst=1 en=0 d=ff q=00

Original design · 8 cells · 8 flip-flops · 0 latches · 0 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
// tb.sv: the bench (read-only). It makes the clock, drives the inputs and prints one line per edge.
`timescale 1ns/1ps
module tb;
  logic       clk = 0;
  logic       rst = 0, en = 0;
  logic [7:0] d = 8'h00;
  logic [7:0] q;
  int         edge_n = -1;

  always #5 clk = ~clk;                       // a 10 ns clock: rising edges at 5, 15, 25, ... ns
  always @(posedge clk) edge_n = edge_n + 1;   // counts the rising edges from 0

  reg8 dut (.clk(clk), .rst(rst), .en(en), .d(d), .q(q));

  // Wait for the next rising edge, let the register update, then print one line.
  task automatic tick();
    @(posedge clk); #1;
    $display("e%0d rst=%b en=%b d=%h q=%h", edge_n, rst, en, d, q);
  endtask

  initial begin
    #1 $display("before any edge: q=%h", q);
    rst = 1; en = 0; d = 8'h3c; tick();       // edge 0: reset
    rst = 0; en = 1;            tick();       // edge 1: load 3c
    en = 0;  d = 8'ha5;         tick();       // edge 2: enable low, so hold
    en = 1;                     tick();       // edge 3: load a5
    d = 8'h7e;                  tick();       // edge 4: load 7e
    rst = 1; d = 8'hff;         tick();       // edge 5: reset and enable together
    rst = 0;                    tick();       // edge 6: load ff
    en = 0;
    #2 rst = 1;                                // reset rises between two edges
    #1 $display("between edges: rst=%b q=%h", rst, q);
    tick();                                   // edge 7: the reset is seen here
    $finish(0);
  end
endmodule

Check Yourself

Predict the outputThe register has an enable and no reset. Predict the exact line the program prints after three edges.
module tb;
  logic       clk = 0, en = 1;
  logic [7:0] d = 8'h10, q;
  always_ff @(posedge clk)
    if (en) q <= d;
  initial begin
    #1 clk = 1; #1 clk = 0;     // edge 1
    d = 8'h20; en = 0;
    #1 clk = 1; #1 clk = 0;     // edge 2
    d = 8'h30; en = 1;
    #1 clk = 1; #1 clk = 0;     // edge 3
    $display("q=%h", q);
  end
endmodule
Spot the bugEach option is the clocked block of a register. The reset must be synchronous: q may change only at a rising clock edge. The bench raises rst with no clock edge and looks at q. Which block fails?
ConceptA clocked block holds q1 = d; and then q2 = q1;, both with the blocking assignment. What does it build?
Read the waveformThe register has an enable. Each cycle ends at a rising edge, and the reset stays at 0. In which cycle does q first show a5?
A register with an enable
0123clkend3ca57eq003ca5
Scroll horizontally ↔

Cycles 1 to 4 of the measured trace above, numbered here from 0.

ConceptA register has an enable but no reset. What does it hold before its first load?

Common Mistakes

Wrong Use = in a clocked block.

The next line reads the value just written, so two stages become one: 8 flip-flops here in place of 16.

Fix Use <= for every assignment in an always_ff block.

Before
always_ff @(posedge clk) begin
  q1 = d;
  q2 = q1;   // reads the new q1
end
After
always_ff @(posedge clk) begin
  q1 <= d;
  q2 <= q1;  // reads the old q1
end

Wrong Want an asynchronous reset, but leave the reset out of the sensitivity list.

The block then runs only at clock edges, so the reset waits for the next edge.

Fix For an asynchronous reset, write always_ff @(posedge clk or posedge rst).

Before
always_ff @(posedge clk)
  if (rst) q <= 8'h00;
  else     q <= d;
After
always_ff @(posedge clk or posedge rst)
  if (rst) q <= 8'h00;
  else     q <= d;

Wrong Assign one register from two always_ff blocks.

Two blocks are two drivers, and Lint reports Variable written to in always_ff also written by other process.

Fix Keep all assignments to a register in one clocked block.

Wrong Leave a register with no reset and read it at once.

A register that was never written holds x, and the example prints q=xx before the first edge.

Fix Reset every register that is read before it is loaded.

Remember

  • always_ff @(posedge clk) runs at each rising edge; signals assigned in it become flip-flops.
  • The order of the if sets the priority: reset, then enable, otherwise hold.
  • Use <= in clocked blocks: every stage reads the old values, then all update together.
  • A synchronous reset waits for the clock edge; or posedge rst makes the reset asynchronous.
  • A register with no reset holds x until its first load.

Practice

Quick reference: always_ff, Blocking = and Nonblocking <=

Report a problem with this page