RTL Foundations / F08

Clock-Edge Sampling: Which Value Does Each Register Read?

Follow three readings through two registers, compare pre-edge and post-NBA observations, and run a blocking-assignment control.

Lesson film

Film chapters

TranscriptCaptionsDownload film

A shift register connects the Q of one stage to the D of the next. Both stages use the same clock and sample at each rising edge. That raises a question at the second stage. When the first stage updates on that edge, does the second stage receive the value the first stage just saved, or the one it had before?

Return to F07’s sensor panel. We’ll pass each eight-bit reading through a first register and then a second. Three distinct hexadecimal readings, 12, 34 and 56, let us identify which sample is in each stage. Both stages update on every rising edge. The exercise at the end adds an enable and validity bits.

How many edges does a reading need?

The first stage takes din_i and produces q1_o. The second stage reads q1_o and produces q2_o. Both use the same active-low synchronous reset, rst_ni. While rst_ni=0, the rising edge clears both stages to 00. After rst_ni returns to one, each stage samples its own input at every rising edge.

din_i enters the first register. q1_o feeds the second register, which produces q2_o. Both registers sample at the same rising edge of clk_i.
Before E2, the first stage already holds 12. The outside input is now 34. The two stages have different values at their inputs.

A reading reaches the first stage at E1 and the second at E2. Those two storage updates are one cycle apart. If you start with the reading still on the external input, it passes through two rising edges. Specify the start and end points whenever you count latency.

always_ff @(posedge clk_i) begin
    if (!rst_ni) begin
        q1_o <= 8'h00;
        q2_o <= 8'h00;
    end else begin
        q1_o <= din_i;
        q2_o <= q1_o;
    end
end

F06 used generate to create one parity_leaf instance per channel at elaboration. With CHANNELS=3, three instances operate in parallel, each on its own channel. Changing the channel count changes the number of instances. Here, two sampling events follow from the data path. The first stage’s Q feeds the second stage’s D, so a reading travels through both registers in order.

Separate the reads from the updates at one edge

Look at E2, at 25 ns. Just before the edge, din_i=34, q1_o=12 and q2_o=00. When the simulator executes q1_o <= din_i, it evaluates the right-hand side (RHS) as 34 and schedules an update to q1_o. Then it executes q2_o <= q1_o. The first scheduled update has yet to take effect, so the RHS still reads 12. The nonblocking assignment update (NBA) region applies both scheduled values. The two stages become 34 and 12 within the same simulation time slot.

Variable at E2Value before the edgeRHS when evaluatedAfter the NBA update
q1_o12din_i is 3434
q2_o00q1_o is 1212
Before E2, din_i is 34, q1_o is 12 and q2_o is 00. The nonblocking assignments evaluate 34 and 12 respectively. After NBA, Q1 is 34 and Q2 is 12.
Read the panels from left to right. They show order within one simulation time slot. The space between them has no time scale.

Apply that reasoning to each row. E0 samples reset. E1 puts 12 in the first stage, and E2 moves it to the second stage. In the same way, 34 reaches the second stage at E3 and 56 at E4. Data is shown in hexadecimal.

EdgeTimerst_nidin_iQ1 / Q2 beforeQ1 / Q2 after
E05 ns000XX / XX00 / 00
E115 ns11200 / 0012 / 00
E225 ns13412 / 0034 / 12
E335 ns15634 / 1256 / 34
E445 ns10056 / 3400 / 56
In the Icarus waveform, Q1 stores 12, 34 and 56 at 15, 25 and 35 ns. Q2 stores those readings at 25, 35 and 45 ns. Q2 in the blocking control instead matches the Q1 update at each edge.
The figure uses the recorded VCD. Look on both sides of each sampling line to see where the same reading appears.

What changes when both assignments use blocking?

In F04, we used blocking assignment with = to calculate combinational outputs. One statement calculated an intermediate value. The next statement used the result immediately. Let’s deliberately apply that order to the two-stage register and see what happens. Keep the reset branch and change the two expressions that run when reset is deasserted.

q1_o = din_i;
q2_o = q1_o;

At E2, the first statement immediately sets the procedural value of q1_o to 34. The second then reads 34. The statement order in this block determines the outcome. In the experiment, q2_o gets 12 at E1, followed by 34, 56 and 00.

The first blocking statement immediately changes q1_o to 34. The second statement reads this new value, so q2_o becomes 34 too.
The second statement runs after the first completes. Both outputs in this control end up storing the current input.

Now imagine putting the blocking write and its reader in two different processes, both triggered by the same rising edge. If the writer runs first, the reader can see the new value. If the reader runs first, it can see the old value. The scheduler allows either order. That’s a race between processes, even if repeated runs on one simulator happen to agree. Our negative control stays in one process, where the wrong data flow follows from the specified statement order.

In this series, we use blocking assignments for ordered calculations in combinational procedures. For clocked storage we use nonblocking assignments, so each stage can read the input at the sampling edge and then update its output. Those choices keep the code aligned with the data paths we drew.

When should the testbench read Q?

To find out what a register updated to, look after the update. The experiment uses a 10 ns clock and declares timeunit 1ns and timeprecision 1ps in the testbench. It prepares input at falling edges. The monitor in tb_after_update.sv waits for the rising edge, then waits 1 ns before looking at the outputs. The testbench uses #1ns to choose an observation point. Physical clock-to-Q delay and setup and hold timing require cell models and timing analysis.

To find out what a receiver read at the rising edge, keep a snapshot from before the update. The monitor in tb_edge_snapshot.sv reads Q1 and Q2 when activated by the rising edge. It observes only the nonblocking DUT, with the inputs prepared at the falling edge. The scheduled NBA updates have yet to run. At E3, a hypothetical receiver sampling Q2 on that edge would read 12. After E3, Q2 shows 34, ready for a later sampling event. F11 will need the same distinction to identify which reading a fire event transferred.

Fix the expected latency and check it in simulation

Get the lab package, extract it on Linux or WSL, and run bash run.sh. Both testbenches use the clock and inputs in sequence.svh. First try to predict the updated table as if the current input reached Q2 on its first edge. All four rows after reset should disagree. Now use the value in Q1 before the edge as the expectation for Q2 after the update, and rerun the check.

Both testbenches passed five rows in Icarus Verilog 12.0. The blocking control differed from the two-stage specification on four rows after reset. In the generic netlist, Yosys 0.33 produced two eight-bit $sdff cells in series for the main design. In the control, it merged the identical outputs into one eight-bit $sdff. See the run summary, raw VCD and evidence and hashes.

Transfer exercise: What should data and validity do if both stages pause?

Suppose stage two holds valid reading 12 and stage one holds valid reading 34. Both stages must pause at the next edge. The source has already prepared 56. What should each stage’s data and validity be after the pause edge? Which reading enters stage two when both stages resume?

Answer

Each stage holds its data and validity. Stage two keeps 12 and stage one keeps 34, both still valid. The interface must prevent a new delivery at stage two during the pause and have the source retain the unaccepted 56. When both stages resume and 56 is still ready, stage two takes 34 and stage one takes 56. Data and validity advance together. Pausing only one stage also requires a transfer rule between the stages.

Next we’ll use the same clock to track work already done. Every cycle has a sampling edge, but the completed count advances only when a work event occurs.

Which sample reaches stage two?

Both stages start at 00 after reset. Pick an input, then step through the pre-edge values, right-hand operands and updated registers. Hold both stages to see which samples stay in place.

Download RTL, testbench and results

Check your understanding

1. Before E2, din_i=34, Q1=12 and Q2=00. What are the two values after the nonblocking updates?
2. The same process runs q1_o = din_i and then q2_o = q1_o. What is Q2 after E2?
3. At E3, 35 ns, a receiver samples Q2 on the common clock. Another testbench checks Q2 at 36 ns. What do they read, in that order?
4. 56 is sampled for the first time at E3. When can the post-update monitor first observe 56 on q2_o?
5. F06 changes CHANNELS from 3 to 4, giving the fourth channel its own parity_leaf. With the F08 pipeline wiring unchanged, when can a new sensor reading reach Q2?