A TV is tuned to channel 5. Press channel up, and it switches to channel 6. Press the same button again, and it switches to channel 7. Each press sends the same command. The TV works out the next channel from the channel it already remembers. Something in the circuit has to save the result of the previous calculation.
A sensor panel has a similar problem. A source keeps putting new readings on the data lines while the panel is still working on an earlier reading. If the panel reads the data lines directly, it can lose access to that earlier value as soon as the source moves on. We can put a register in between. The register saves the reading at an agreed time and gives the panel a stable value to work with.
Why does the same command produce two results?
Split the TV tuner into two parts. Combinational logic calculates the next channel from the current one. A storage element retains the result when an update is requested. The following press uses that saved channel for the next calculation. The stored channel is the tuner’s state. In the sensor panel, the state is the saved eight-bit reading.
In F04, the omitted branch in a combinational selector inferred a latch and retained a previous reading. A deliberately designed D latch also has a hold mode. While its control level is active, changes on D can pass to Q. When the control becomes inactive, it holds the value. This lesson uses D flip-flops. Each flip-flop samples D on a specified clock edge and keeps Q between those events. We can store eight bits by sampling them on the same edge.
D can change at any time. When does Q change?
Connect the source to D, the pending data input d_i. Connect the panel to Q, the saved output q_o. The register uses clk_i and samples on its rising edge. Two consecutive rising edges bound a cycle. A sampling edge is an event at one instant.
Start with a register that samples every rising edge. The event control posedge clk_i identifies the sampling event. The operator <= is a nonblocking assignment. At the event, the simulator evaluates D on the right and schedules an update to Q on the left later in the same simulation time slot. F08 will examine those two steps in detail.
always_ff @(posedge clk_i) begin
q_o <= d_i;
end
Suppose D is stable at 3C before the rising edge at 25 ns. The register saves 3C. The source then changes D to 44 at 27 ns. Q still holds 3C, and D may change again before the next sampling edge. If the panel must receive every reading, the interface also needs a rule for when the source can advance. This lesson defines which value the register saves. F11 adds a handshake so both sides can agree on when a transfer occurs.
Which takes priority when clearing and writing overlap?
The controller can set en_i=0 while the panel is busy. The register then keeps its reading even at a rising edge. When the panel is ready for a new reading, the controller sets en_i=1, letting the next rising edge write the data. Changes on D between those edges leave Q alone.
The TV also needs an initial channel. For the panel exercise, we choose a reset value of 8'h00. If the panel needs to distinguish “no reading yet” from a real reading of zero, the interface needs a separate validity field. Here we verify what the register stores in its data field.
This reset is active-low and synchronous. Driving rst_ni low asserts reset. Returning it to one deasserts reset. The suffix _ni marks an active-low input. The event control @(posedge clk_i) makes the reset synchronous. Q clears only if reset is asserted at a rising edge. Reset wins even with en_i=1.
always_ff @(posedge clk_i) begin
if (!rst_ni) q_o <= 8'h00;
else if (en_i) q_o <= d_i;
end
The final case leaves Q unassigned at that edge, so it holds the previous reading. That hold is part of our clocked register’s specification. In the F04 selector, the specification called for a current output and the missing combinational branch retained an earlier one.
A synchronous reset needs a rising edge to clear Q. If a system must clear state while the clock is stopped, consider an asynchronous reset. For an active-low asynchronous reset, adding or negedge rst_ni to the event control lets reset assertion trigger a clear. Real flip-flops impose recovery and removal timing requirements when reset is released. F14 covers reset deassertion and clock-domain handling.
Follow the reading across nine rising edges
The simulation reaches its first rising edge at 5 ns. Reset has yet to run, and enable is low. Four-state simulation reports the uninitialized Q as XX. At 15 ns, both reset and enable are active. The register clears to 00. At the next rising edge, reset has been released and the register writes 3C. The table shows data in hexadecimal.
| Edge | Time | rst_ni | en_i | D | Q before | Q after | Action |
|---|---|---|---|---|---|---|---|
| E0 | 5 ns | 1 | 0 | 2A | XX | XX | Hold uninitialized Q |
| E1 | 15 ns | 0 | 1 | A5 | XX | 00 | Reset wins |
| E2 | 25 ns | 1 | 1 | 3C | 00 | 3C | Write |
| E3 | 35 ns | 1 | 0 | 57 | 3C | 3C | Hold |
| E4 | 45 ns | 1 | 0 | 69 | 3C | 3C | Hold |
| E5 | 55 ns | 1 | 1 | 7D | 3C | 7D | Write |
| E6 | 65 ns | 0 | 1 | B4 | 7D | 00 | Clear |
| E7 | 75 ns | 1 | 0 | C6 | 00 | 00 | Hold reset value |
| E8 | 85 ns | 1 | 1 | D8 | 00 | D8 | Write new data |
Compare E6 with E7. When reset goes low at 60 ns, Q still holds 7D. It clears at 65 ns. Reset returns high at 70 ns. Enable is low at the next sampling edge, so Q keeps 00. The reset operation already replaced 7D. Releasing reset returns the register to its normal write rule.
Use the same inputs to catch a priority bug
Get the lab package, extract it on Linux or WSL, and run bash run.sh. The device under test, or DUT, is in result_register.sv. The testbench tb_result_register.sv prepares the next row on the falling edge of a 10 ns clock, then checks the result 1 ns after the rising edge. Input changes and sampling occur at different times. The testbench also checks Q three times between rising edges.
- At 10 ns, it drives
rst_nilow and immediately sees Q still atXX. - At 27 ns, it makes an extra input change to
44. At 28 ns, Q still holds3C. The falling edge at 30 ns replaces44with57, so no rising edge samples44and it never appears in the table. - At 60 ns, it drives
rst_nilow again. At 61 ns, Q still holds7D. The rising edge at 65 ns clears it.
Reverse the conditions to make the negative control in result_register_enable_first.sv. It writes A5 at E1 and B4 at E6. It then holds the incorrect B4 at E7 because enable is low. Testing reset and enable one at a time can show that each works in isolation. To find out which wins, drive both active at the same edge.
In Icarus Verilog 12.0 the main register passed nine edge cases and three checks between edges. The negative control differed on E1, E6 and E7. Yosys 0.33 represented the main register as one eight-bit $sdffe, a synchronous-reset flip-flop cell with an enable. The control used an eight-bit $dffe with extra control logic. Check the run summary, raw VCD and evidence and hashes. The simulator checks behavior and the generic netlist describes the storage structure. Physical setup and hold timing require cell models and timing analysis.
Transfer exercise: What if the same data arrives after the sampling edge?
At E2, d_i is stable at 3C ahead of the edge, enable is one, and reset is deasserted. Suppose the new value 44 arrives after E2, with both changes safely away from the sampling instant. What does E2 store? What if 44 remains stable and enable is still one at the next rising edge?
Answer
E2 stores 3C. The arrival of 44 changes D while Q continues to hold 3C. At the next rising edge, if D is 44, reset is deasserted and enable is one, Q updates to 44. This answer assumes the signals meet timing. A transition near a real sampling edge needs separate analysis for setup or hold violations.
In the next lesson, we connect two of these registers in series. They sample on the same rising edge. Following a reading through the pair reveals which value each stage sees at that edge.