RTL Foundations / F07

Registers: How Do We Keep a Result?

Start with the state in a TV tuner, then use a recorded waveform to test when a sensor register samples, holds, and resets.

Lesson film

Film chapters

TranscriptCaptionsDownload film

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.

Applying the same channel-up command twice moves the stored channel from 5 to 6 and then from 6 to 7.
Each press uses the channel already saved. Here we follow the state changes. The interface handles button debouncing and sampling.

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.

The sensor connects its d_i bus to eight D flip-flops. Their q_o bus feeds the panel. The rising edge of clk_i determines when the register samples.
D is where the reading waits. A rising edge is an opportunity to update Q.

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.

At a rising edge, rst_ni=0 clears Q to 00. Otherwise en_i=1 stores D, and en_i=0 holds Q.
The reset condition comes first. Once reset is deasserted, enable determines whether to write.
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.

EdgeTimerst_nien_iDQ beforeQ afterAction
E05 ns102AXXXXHold uninitialized Q
E115 ns01A5XX00Reset wins
E225 ns113C003CWrite
E335 ns10573C3CHold
E445 ns10693C3CHold
E555 ns117D3C7DWrite
E665 ns01B47D00Clear
E775 ns10C60000Hold reset value
E885 ns11D800D8Write new data
Icarus waveform with nine rising clock edges. Q takes 3C at 25 ns and holds when D turns to 44 at 27 ns. Reset goes low at 60 ns; Q clears at 65 ns.
The figure uses the recorded VCD. The reset transition at 60 ns and the Q update at 65 ns are separate events.

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_ni low and immediately sees Q still at XX.
  • At 27 ns, it makes an extra input change to 44. At 28 ns, Q still holds 3C. The falling edge at 30 ns replaces 44 with 57, so no rising edge samples 44 and it never appears in the table.
  • At 60 ns, it drives rst_ni low again. At 61 ns, Q still holds 7D. 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.

Keep a reading until the next edge

Change D and watch Q. Press Rising edge to sample it. Try asserting enable and reset together. XX means the simulation has not yet established a known value.

Download RTL, testbench and results

Check your understanding

1. Q is 3C. With en_i=0, rst_ni=1 and D at 69, what is Q after the next rising edge?
2. Q is 7D and D is B4. rst_ni falls between rising edges while en_i stays high. What is Q before and after the next update?
3. At E2, enable is one, reset is deasserted and D is 3C. D changes to 44 at 27 ns. Enable is zero at the next edge. What is Q after the two updates?
4. E6 clears Q to 00 and reset is released at 70 ns. At E7, D=C6 and en_i=0. What is Q after E7?
5. This synchronous-reset register holds 7D. The clock stops low, and rst_ni is pulled low and held there. When will Q clear?