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 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.
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.
| Time | Event | rst_n | en | D | Q |
|---|---|---|---|---|---|
| 5 ns | E0 rising edge, reset wins | 0 | 1 | 31 | 00 |
| 15 ns | E1 rising edge, store A | 1 | 1 | 31 | 31 |
| 20 ns | Between rising edges, set sel high | 1 | 1 | 52 | 31 |
| 25 ns | E2 rising edge, hold | 1 | 0 | 52 | 31 |
| 35 ns | E3 rising edge, store B | 1 | 1 | 52 | 52 |
| 40 ns | Between rising edges, pull rst_n low | 0 | 1 | 52 | 52 |
| 45 ns | E4 rising edge, clear Q | 0 | 1 | 52 | 00 |
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?
| Edge | rst_n | en | D | Q |
|---|
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 Register RTL (.zip)