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.
d goes into both pipelines. Each cycle ends at a rising clock edge.- Cycle 0
Before the first edge both outputs are
xx, unknown. The value 11 waits at the input. - Cycle 1
After one edge the
=version already shows 11. The<=version still showsxx: the value is in the first stage. - Cycle 2
After two edges the
<=version shows 11. That is the delay a two-stage pipeline must have. - 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.
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
endThe 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).
rst, en and d of that cycle. The new q shows in the next cycle.- Test
- Storage
- I/O
A token on d means the next edge loads that value.
- Cycle 0
No edge has come yet, so
qisxx.rstis 1, and the edge at the end of this cycle will clear the register. - Cycle 1
qis 00 after the reset edge. Nowenis 1 anddis 3c, so the next edge loads 3c. - Cycle 2
qis 3c.enis 0 in this cycle.dchanges to a5, but the register does not take it. - Cycle 3
qstill holds 3c.enis 1 again, so the edge at the end of this cycle loads a5. - Cycle 5
rstandenare both 1, anddis ff. Reset has priority. - Cycle 6
qis 00: reset won.enis 1 withrstat 0, so the next edge loads ff. - Cycle 7
qis ff.enis 0, but the bench raisesrstin 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.
Read
At the edge, the simulator reads every right-hand side. It uses the values from before the edge.
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.
logic clk = 0;
always #5 clk = ~clk; // flip every 5 ns: rising edges at 5, 15, 25, ... nsThe 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
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.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.
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.
Synthesize the design to measure its cells and inspect the mapped gates.
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=00Original 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
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.
always_ff @(posedge clk) begin
q1 = d;
q2 = q1; // reads the new q1
endalways_ff @(posedge clk) begin
q1 <= d;
q2 <= q1; // reads the old q1
endWrong 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).
always_ff @(posedge clk)
if (rst) q <= 8'h00;
else q <= d;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
ifsets 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 rstmakes the reset asynchronous. - A register with no reset holds
xuntil its first load.
Practice
Quick reference: always_ff, Blocking = and Nonblocking <=