Register

Connect a mux to an eight-bit register. Watch D change while Q holds its old value, then try enable and synchronous reset.

The mux output changes from 31 to 52 while the downstream logic is still using 31. Connect the mux to a register’s D input so the clock determines when Q updates. D carries the value currently on offer. Q holds the value already saved. The two can differ between rising edges.

When does D become Q?

A basic eight-bit D register samples D at each rising clock edge. Q holds the sampled value until the next rising edge. All eight bits use the same sampling instant.

module d_register (
    input  logic       clk,
    input  logic [7:0] d,
    output logic [7:0] q
);
    always_ff @(posedge clk) begin
        q <= d;
    end
endmodule

posedge clk defines the sampling event. The <= operator is a nonblocking assignment. Simulation reads D at the rising edge and applies the update to Q later in that same simulation time slot. To read the waveform, find the stable D value just before the edge and then check the updated Q.

The mux uses sel to choose between 31 and 52 and drives the register’s D input. At 20 ns the mux changes D to 52, while Q still holds the previously sampled 31.
Changing sel supplies a new D. The new value reaches Q when the register can write at a rising edge.

The mux inputs are A=8'h31 and B=8'h52. With sel=0 it chooses A. With sel=1 it chooses B. Its output y drives the register’s input d. See the multiplexer entry for the selection rule.

Which rising edges can write?

If the downstream logic still needs the old value, use en to pause writes. Reset also gives the register an agreed starting value. In this example rst_n=0 asserts reset, and the rising edge clears Q to 00. That makes the reset active-low and synchronous.

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

At each rising edge, the register checks reset first. If rst_n=0, Q clears to 00 even when en=1. With reset released, en=1 stores D and en=0 holds Q. The clocked process leaves Q unassigned in the hold case, so it keeps its previous value.

At a rising edge, rst_n low clears Q to 00 even if en is high. With rst_n high, en low holds Q and en high stores D.
The register samples reset and enable on the same edge. Reset takes priority.

Follow 31 and 52 through the recorded trace

The clock period is 10 ns. The table shows hexadecimal data and the value of Q after each event.

TimeEventrst_nenDQ
5 nsE0 rising edge, reset wins013100
15 nsE1 rising edge, store A113131
20 nsBetween rising edges, set sel high115231
25 nsE2 rising edge, hold105231
35 nsE3 rising edge, store B115252
40 nsBetween rising edges, pull rst_n low015252
45 nsE4 rising edge, clear Q015200
Recorded simulation waveform. Q clears at 5 ns and stores 31 at 15 ns. D changes to 52 at 20 ns. Q holds 31 at 25 ns and stores 52 at 35 ns. Reset goes low at 40 ns and clears Q at 45 ns.
D changes to 52 at 20 ns. The edge at 25 ns sees enable low, so Q holds 31 until the enabled edge at 35 ns.

Compare E0 and E4. Before E0, four-state simulation shows the uninitialized Q as XX. Before E4, Q already holds 52. Pulling reset low at 40 ns leaves Q at 52 until the rising edge at 45 ns clears it to 00.

Replay these samples

Extract the executable lab package on Linux or WSL and run bash run.sh. The lab includes both complete RTL modules, the testbench, a run log from Icarus Verilog 12.0, and the VCD waveform. The testbench records D and the old Q on each rising edge, then checks Q after the update. It also checks between-edge values at 21 ns and 41 ns. The waveform shows RTL behavior. Physical sampling also has setup and hold requirements, which the F07 register lesson introduces.

Interactive lab

Apply a rising edge to clear Q with synchronous reset. Release reset and apply another edge to store A. Now select B and compare D with Q. What happens at the next edge if you turn enable off?

D 0x31Q X

Last eight rising edges, with Q after each update
Edgerst_nenDQ

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 Register RTL (.zip)

Check your understanding

1. Before the register samples anything, simulated Q reads X. May you assume 00?
2. Between rising edges D moves from 31 to 52 with en high. Q was 31. What is Q now?
3. At a rising edge rst_n is 1, en is 0 and D is 52. Q was 31. What follows?
4. Q is 52 when rst_n goes low between edges. When does Q clear?
5. An edge samples rst_n = 0, en = 1 and D = 52. Which condition takes priority?