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.
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 E2 | Value before the edge | RHS when evaluated | After the NBA update |
|---|---|---|---|
q1_o | 12 | din_i is 34 | 34 |
q2_o | 00 | q1_o is 12 | 12 |
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.
| Edge | Time | rst_ni | din_i | Q1 / Q2 before | Q1 / Q2 after |
|---|---|---|---|---|---|
| E0 | 5 ns | 0 | 00 | XX / XX | 00 / 00 |
| E1 | 15 ns | 1 | 12 | 00 / 00 | 12 / 00 |
| E2 | 25 ns | 1 | 34 | 12 / 00 | 34 / 12 |
| E3 | 35 ns | 1 | 56 | 34 / 12 | 56 / 34 |
| E4 | 45 ns | 1 | 00 | 56 / 34 | 00 / 56 |
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.
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.