Shift register

Wire each stage’s Q to the next stage’s D and track distinct input values one clock edge at a time. Predict how many sampling edges the first value needs to reach the final stage, then use the waveform to diagnose an off-by-one count.

The sensor offers 31, then 52 and A6 on successive captures. They must pass through three register stages in order. At which edge does 31 reach the last stage?

Wire each Q to the next stage’s D

External D feeds stage one, Q1 feeds stage two, and Q2 feeds stage three. The three eight-bit registers share clk. Each rising edge reads the values already present at all three inputs and updates all three registers. A word advances one stage per edge.

After E2, Q1 holds 52, Q2 holds the 31 from the previous stage, and Q3 contains 00.
The wiring stays fixed. Follow a word through the successive samples.

Describe three simultaneous samples in one block

The active-low synchronous reset clears all stages. After release, q2 <= q1 reads the pre-edge Q1 and q3 <= q2 reads the pre-edge Q2.

module shift_register3 (
    input  logic       clk,
    input  logic       rst_n,
    input  logic [7:0] d,
    output logic [7:0] q1,
    output logic [7:0] q2,
    output logic [7:0] q3
);
    always_ff @(posedge clk) begin
        if (!rst_n) begin
            q1 <= 8'h00;
            q2 <= 8'h00;
            q3 <= 8'h00;
        end else begin
            q1 <= d;
            q2 <= q1;
            q3 <= q2;
        end
    end
endmodule

At which sample does 31 reach Q3?

Values are hexadecimal. The three Q columns show the stage contents after each update.

EdgeTimeDQ1Q2Q3
E05 ns00000000
E115 ns31310000
E225 ns52523100
E335 nsA6A65231
E445 ns0000A652
E555 ns000000A6
31 enters Q1 at 15 ns, Q2 at 25 ns and Q3 at 35 ns.
If you stop counting at E2, 31 is still in stage two.

Including its first capture, 31 uses three samples at E1, E2 and E3. The elapsed time from E1 to E3 is two full periods. Name the endpoints when you count so the number of samples and the elapsed cycles stay clear.

Replay the lab

Extract the executable lab on Linux or WSL and run bash run.sh. Icarus Verilog 12.0 checked 6 timed observations. The archive contains the RTL, testbench, log, VCD and generic Yosys structure. The test compares all three stages against the same sequence. Continue with F08 on clock-edge sampling, F13 on pipeline and timing.

Interactive lab

Apply one edge with reset asserted. Release reset, then use an edge each for 31, 52 and A6.

Stage 1 Q1
Stage 2 Q2
Stage 3 Q3
Show the last eight observations

    Teaching film

    Read the transcript

    Run the RTL yourself

    Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

    Download Shift register RTL (.zip)

    Check your understanding

    1. E1 captures 31. With D=52 at E2, what are Q1, Q2 and Q3 after E2?
    2. At which rising edge does 31 first reach Q3?
    3. How many full clock periods elapse from capture at E1 to arrival at Q3 at E3?
    4. Which q2 value does q3 <= q2 read?
    5. If q3 <= q2 is changed to q3 <= d, when does 31 reach Q3?