Latch

Show how an intentional latch passes its input while enabled and holds the value when disabled. Then test combinational logic with a missing assignment, explain why its output holds, and check what the tool infers.

The panel should follow the sensor while en=1. When the operator closes enable, it should keep the last value. An active-high latch uses the enable level to decide when data passes and when it holds.

D can pass throughout the enabled interval

While en=1, Q follows changes in D. With en=0, Q holds the value from before the path closed. More than one change can pass through during a single enabled interval.

With enable high at T2, both D and Q show 52. At T4 enable is low, D is A6, and Q still holds 52.
The enable level selects between passing and holding the data.

An intentional hold and a missing branch both require storage

The first module intentionally uses always_latch. The second was meant to be combinational but assigns Q only with en=1. The old value must survive when enable is low, so synthesis also infers storage. If the required disabled value is 00, the complete third version assigns both enable cases.

module latch8 (
    input  logic       en,
    input  logic [7:0] d,
    output logic [7:0] q
);
    always_latch begin
        if (en)
            q <= d;
    end
endmodule


module missing_branch8 (
    input  logic       en,
    input  logic [7:0] d,
    output logic [7:0] q
);
    always @* begin
        if (en)
            q = d;
    end
endmodule

module complete_branch8 (
    input  logic       en,
    input  logic [7:0] d,
    output logic [7:0] q
);
    assign q = en ? d : 8'h00;
endmodule

Change D after closing enable

All three versions use the same enable and hexadecimal data. The test observes 1 ns after each stimulus. At T0 the latches have yet to capture any value, so four-state simulation shows XX.

SampleenDIntentional latchMissing branchComplete branch
T0, 1 ns031XXXX00
T1, 11 ns131313131
T2, 21 ns152525252
T3, 31 ns052525200
T4, 41 ns0A6525200
T5, 51 ns1A6A6A6A6
At T4 all have D=A6 and en low. The intentional latch and missing branch retain 52. The complete branch drives 00.
From T1 to T2, enable stays high and Q follows the change on D.

Yosys 0.33 infers a $dlatch for each of the first two modules and a $mux for the complete branch. The specification determines whether storage belongs there. If the output must depend only on current inputs, assign every branch and test what happens when enable turns off.

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. One testbench compares all three versions. The archive also records their generic structures. Continue with F04 on combinational logic, F07 on clocked storage.

Interactive lab

Open en and set D to 52. Close en, change D to A6 and compare the three branches.

Stored Q
Missing branch
Complete combinational branch
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 Latch RTL (.zip)

    Check your understanding

    1. Enable stays high while D changes from 31 to 52. When does Q follow?
    2. At T4 en is low and D=A6. What does the intentional latch output?
    3. A combinational description omits the assignment when en is low. Why does the old value remain?
    4. With assign q = en ? d : 8'h00, what does the complete combinational branch output at T4?
    5. What does Yosys infer for the intentional latch, missing branch and complete branch, respectively?