D flip-flop
Line up D, Q and the sampling edges in a waveform. If D changes several times between rising edges, Q keeps the previous sample and updates at the next rising edge. Use the trace to distinguish the values before sampling and after the update.
The sensor first offers 31, then 52, then A6. The panel reads at rising clock edges. Which values end up stored?
When does D reach Q?
A D flip-flop samples D on a rising edge and holds that value on Q until the next sample. One flip-flop holds one bit. Here eight bits share clk to capture one word.
Describe sampling with a nonblocking assignment
posedge clk selects the rising edge. Simulation reads d at that edge and updates q later in the same simulation time slot. That is the nonblocking assignment.
module d_flip_flop (
input logic clk,
input logic [7:0] d,
output logic [7:0] q
);
always_ff @(posedge clk) begin
q <= d;
end
endmodule
Does the flip-flop capture 52?
D and Q are hexadecimal. The Q column shows the settled simulation result after each event. Before the first rising edge, the uninitialized Q is XX in four-state simulation.
| ns | Event | D | Q |
|---|---|---|---|
| 5 | E0 samples | 31 | 31 |
| 7 | D changes | 52 | 31 |
| 12 | D changes again | A6 | 31 |
| 15 | E1 samples | A6 | A6 |
| 17 | D changes | 00 | A6 |
| 25 | E2 samples | 00 | 00 |
Before E1, D is A6 and Q is 31. E1 reads A6, so Q becomes A6 after the update. To capture 52, keep it present through a rising edge and satisfy the physical setup and hold requirements.
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 checks run 1 ps after the labeled events to observe the RTL update. Continue with F07 on registers, F08 on clock-edge sampling.
Interactive lab
Sample 31, change D through 52 and A6, then apply the next rising edge.
- Stored Q
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 D flip-flop RTL (.zip)