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

Pipeline Registers and Valid Signals

Assume a combinational logic path requires 8 ns to settle, but the target clock period is 5 ns. The destination flip-flop samples before data stabilizes, causing setup time violations. Designers resolve this timing failure with two approaches:

  1. Increase the clock period to 8 ns, which reduces the system operating frequency.
  2. Partition the combinational path into two stages separated by an intermediate register.

The second method is pipelining. Pipelining enables systems to achieve high clock frequencies without violating setup constraints.

Throughput Versus Latency Trade-Off

Consider an assembly line analogy:

  • A single workstation that performs all steps takes 8 hours to complete one product.
  • An assembly line with four 2-hour stations produces a finished product every 2 hours once filled.
  • Each individual product still spends 8 hours in the manufacturing line.

In digital hardware:

  • Throughput (data items processed per unit time) increases. Once filled, the pipeline completes one result per clock cycle. The maximum propagation delay of the slowest individual stage determines the clock period.
  • Latency (clock cycles required to produce a specific result) increases. An $N$-stage pipeline requires $N$ clock cycles of latency.

Pipelining optimizes high-throughput data processing streams where overall bandwidth is more critical than single-sample latency.

Two-Stage Pipelined Multiply-Accumulate Datapath

Multipliers and adders in sequence form long combinational paths. Placing a register between arithmetic operations divides the delay:

// Stage 1: Multiplication stage
always @(posedge clk) begin
    product_s1 <= a * b;
end

// Stage 2: Addition stage
always @(posedge clk) begin
    result_s2 <= product_s1 + accum;
end

Data applied at cycle 0 produces an intermediate product at cycle 1 and a final sum at cycle 2. This structure introduces two cycles of latency. However, a new input set can enter the pipeline at cycle 1 while the first calculation progresses to stage 2. After the pipeline fills, a valid output appears on every clock edge.

Tracking Data Validity Through Pipeline Stages

At power-up and following system reset, pipeline registers contain uninitialized data. A control signal must track whether a specific stage contains valid data:

always @(posedge clk) begin
    valid_s1 <= valid_in;
    valid_s2 <= valid_s1;
end

The valid signal shifts forward in synchronization with the datapath registers (Figure 1). When the consumer reads result_s2, control bit valid_s2 indicates whether the result represents valid computation.

Data and valid through a pipeline
Figure 1: The valid bit shadows the data stage for stage

Flow Control and Backpressure

This basic pipeline architecture assumes the downstream consumer accepts data on every clock cycle. If the receiver cannot process data continuously, earlier stages must stall. Handling stalls requires backpressure control signals, such as ready/valid handshakes. Module 7 discusses complete flow-control architectures.

Timing closure on high-speed ASICs and FPGAs relies extensively on pipelining. When a critical path fails timing closure, designers insert pipeline registers to break up combinational delay.

Practice Exercise

Restructure a three-input combinational adder into a two-stage pipelined circuit. Track data validity and verify output latency with a self-checking testbench.

Solution
`timescale 1ns / 1ps
`default_nettype none

module adder_tree_pipelined (
    input  wire       clk,
    input  wire [7:0] a, b, c,
    input  wire       valid_in,
    output reg  [8:0] sum_out,
    output reg        valid_out
);
    reg [8:0] ab_s1;
    reg       valid_s1;

    always @(posedge clk) begin          // Stage 1: Compute a + b
        ab_s1    <= a + b;
        valid_s1 <= valid_in;
    end

    always @(posedge clk) begin          // Stage 2: Add c to intermediate sum
        sum_out   <= ab_s1 + c;
        valid_out <= valid_s1;
    end
endmodule

module tb_adder_tree_pipelined;
    reg        clk = 0, valid_in;
    reg  [7:0] a, b, c;
    wire [8:0] sum_out;
    wire       valid_out;

    adder_tree_pipelined dut (
        .clk(clk),
        .a(a),
        .b(b),
        .c(c),
        .valid_in(valid_in),
        .sum_out(sum_out),
        .valid_out(valid_out)
    );

    always #5 clk = ~clk;

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

        valid_in = 0; a = 0; b = 0; c = 0;
        @(negedge clk);

        a = 8'd10; b = 8'd20; c = 8'd30; valid_in = 1;
        @(negedge clk);        // Cycle 1: ab_s1 = 30, valid_s1 = 1
        valid_in = 0;
        @(negedge clk);        // Cycle 2: sum_out = 60, valid_out = 1

        if (sum_out !== 9'd60 || valid_out !== 1'b1)
            $display("FAIL: sum_out=%0d valid_out=%b (expected 60, 1)", sum_out, valid_out);
        else
            $display("PASS: sum_out=%0d on cycle 2", sum_out);

        $finish;
    end
endmodule

The testbench confirms that the calculation completes on the second clock edge and confirms that valid_out asserts on the matching cycle.

Note that operand c must remain stable during stage 1. In production designs, register operand c into an intermediate stage register (c_s1) to maintain signal synchronization.

This concludes Section 5.5. Section 5.6 discusses finite state machine (FSM) modeling architectures in synthesizable RTL.

← Register Design Patterns The State Machine in RTL →