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.
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.
| Edge | Time | D | Q1 | Q2 | Q3 |
|---|---|---|---|---|---|
| E0 | 5 ns | 00 | 00 | 00 | 00 |
| E1 | 15 ns | 31 | 31 | 00 | 00 |
| E2 | 25 ns | 52 | 52 | 31 | 00 |
| E3 | 35 ns | A6 | A6 | 52 | 31 |
| E4 | 45 ns | 00 | 00 | A6 | 52 |
| E5 | 55 ns | 00 | 00 | 00 | A6 |
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
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)