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.

The 15 ns rising edge samples D=A6 and updates Q from 31 to A6.
D settles before the rising edge samples it.

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.

nsEventDQ
5E0 samples3131
7D changes5231
12D changes againA631
15E1 samplesA6A6
17D changes00A6
25E2 samples0000
D changes to 52 at 7 ns and to A6 at 12 ns. Q stays at 31 until the 15 ns edge loads A6.
There is no rising edge while 52 is on D.

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

    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 D flip-flop RTL (.zip)

    Check your understanding

    1. The 5 ns edge samples 31. What is Q when D changes to 52 at 7 ns?
    2. D changes again to A6 at 12 ns. What is Q after the 15 ns update?
    3. Immediately before the 15 ns rising edge, what are D and Q?
    4. If D stayed at 52 through 15 ns, what would the update store?
    5. What can eight D flip-flops sharing one clock store?