Module 5: Behavioral Verilog for Synthesis · 5.5 Modeling Flip-Flops and Registers

Clock Enables and Load Signals

Most registers in a digital system do not update on every clock edge. A serial receiver register shifts data only when a valid bit arrives. An arithmetic accumulator adds data only when instructed. Verilog uses conditional enable patterns to model this behavior.

The Clock-Enable Pattern

To describe a register with an enable signal, omit the else branch in an edge-triggered block:

always @(posedge clk) begin
    if (en)
        q <= d;
    // When en is low, q maintains its current value
end

In sequential blocks, omitting the else branch does not infer transparent latches. Flip-flops inherently retain their stored values between active clock edges. Synthesis tools map en to the dedicated clock-enable pin of target library flip-flops, reducing logic gate count and dynamic power consumption.

Prioritizing Reset Over Enable

When a design uses both reset and enable signals, reset must have higher priority:

always @(posedge clk or negedge rst_n) begin
    if (!rst_n)
        q <= 1'b0;
    else if (en)
        q <= d;
end

The reset check occurs first. A register must clear reliably during reset even if the enable signal is asserted.

Synchronous Parallel Load

A parallel load register uses an enable condition to capture parallel bus data:

always @(posedge clk) begin
    if (load)
        data_reg <= data_in;
end

The signal name load indicates that the register captures an entire parallel word on the active clock edge.

Distinguishing Reset from Synchronous Clear

Reset and clear operations serve distinct architectural functions:

always @(posedge clk or negedge rst_n) begin
    if (!rst_n)
        q <= 1'b0;      // System-level reset at power-up or fault condition
    else if (clear)
        q <= 1'b0;      // Synchronous operational clear during execution
    else if (en)
        q <= d;         // Data update
end

Follow this strict evaluation sequence:

  1. System reset: Initializes registers during power-up or system reinitialization.
  2. Synchronous clear: Resets operational counters or registers during standard execution.
  3. Clock enable: Updates the stored data.

Hazard: Do Not Gate Clock Signals Manually

Do not gate clock signals with discrete logic gates in RTL:

assign gated_clk = clk & en;    // Non-compliant coding practice

If the enable signal changes state while the clock signal is high, the AND gate generates a glitch or runt pulse. Downstream registers treat this glitch as a false clock edge. In addition, static timing analysis tools cannot verify clock trees with discrete combinational gating.

Always implement enables using data-path multiplexing or dedicated clock-enable pins. In power-sensitive designs, automated synthesis tools insert integrated clock-gating (ICG) cells. These dedicated cells include latch-based glitch protection.

Practice Exercise

Design a 4-bit accumulator module. The accumulator clears when clear is high, adds data_in when acc_en is high, and holds its state otherwise. Create a self-checking testbench to verify prioritization.

Solution
`timescale 1ns / 1ps
`default_nettype none

module accumulator (
    input  wire       clk, rst_n,
    input  wire       clear, acc_en,
    input  wire [3:0] data_in,
    output reg  [3:0] acc
);
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n)      acc <= 4'b0000;
        else if (clear)  acc <= 4'b0000;
        else if (acc_en) acc <= acc + data_in;
    end
endmodule

module tb_accumulator;
    reg        clk = 0, rst_n, clear, acc_en;
    reg  [3:0] data_in;
    wire [3:0] acc;

    accumulator dut (.clk(clk), .rst_n(rst_n), .clear(clear), .acc_en(acc_en), .data_in(data_in), .acc(acc));

    always #5 clk = ~clk;

    initial begin
        $dumpfile("tb.vcd");
        $dumpvars(0, tb_accumulator);

        rst_n = 0; clear = 0; acc_en = 0; data_in = 0;
        @(negedge clk); rst_n = 1;

        @(negedge clk);
        if (acc !== 4'd0) $display("FAIL: expected hold at 0, got %0d", acc);

        acc_en = 1; data_in = 4'd3; @(negedge clk);
        data_in = 4'd2; @(negedge clk);
        if (acc !== 4'd5) $display("FAIL: expected 5, got %0d", acc);
        else              $display("PASS: acc=%0d after 3+2", acc);

        clear = 1; @(negedge clk);          // Clear with acc_en still high
        if (acc !== 4'd0) $display("FAIL: expected 0 after clear, got %0d", acc);
        else              $display("PASS: cleared");

        $finish;
    end
endmodule

The testbench verifies that clear overrides acc_en when both control signals assert simultaneously.

The next article presents a catalog of fundamental register structures, including binary counters, shift registers, and circular buffers.

← Reset Strategies: Synchronous and Asynchronous Register Design Patterns →