Prev Problem
Next Problem

74. Gated SR Latch

module sr_latch_enable (
    input  EN,
    input  S,
    input  R,
    output Q,
    output Qn
);
    reg Q_reg;
    reg Qn_reg;

    always @(*) begin
        if (EN) begin
            if (S && R) begin
                Q_reg  = 1'b0; // illegal -> reset
                Qn_reg = 1'b1;
            end else if (S && !R) begin
                Q_reg  = 1'b1; // set
                Qn_reg = 1'b0;
            end else if (!S && R) begin
                Q_reg  = 1'b0; // reset
                Qn_reg = 1'b1;
            end
        end
    end

    assign Q  = Q_reg;
    assign Qn = Qn_reg;
endmodule

💡Remember

  • Latch inference comes from always @(*) with incomplete assignments in hold paths.
  • EN gates write-access to the state; when low, the latch ignores S/R.
  • Deterministic handling of S=R=1 prevents X propagation in simulation.
  • No register initial values are assumed; the environment establishes the first known state.