Module 7: Advanced RTL Design Techniques · 7.3 Pipeline Design

A Pipelined Multiply-Accumulate Unit

A Multiply-Accumulate (MAC) unit executes the arithmetic operation $a \times b + \text{acc} \rightarrow \text{acc}$. This mathematical operation forms the computational core of finite impulse response (FIR) filters, matrix multiplications, and machine learning accelerators. This article integrates stage partitioning, passthrough alignment, and stall control into a complete hardware MAC unit.

Design Specification

The hardware unit adheres to these functional requirements:

  • Operands a and b arrive synchronously with a valid_in flag.
  • The unit calculates $a \times b$ and sums the product into a running accumulation register.
  • An assertion on clear_acc resets the accumulator and loads the product calculated from accompanying inputs.
  • An assertion on stall freezes all three pipeline stages.
  • The datapath consists of three stages: Stage 1 and Stage 2 calculate the product; Stage 3 executes accumulation.

Managing Accumulator Feedback Paths

Unlike purely feedforward datapaths, a MAC unit includes a sequential feedback loop. Stage 3 feeds its own registered output back into the final adder:

The accumulate stage feeds its own output back
Figure 1: MAC pipeline with the accumulator feedback path

Temporal misalignment corrupts subsequent calculations because errors compound across accumulation cycles. The clear signal (clear_acc) must traverse the pipeline synchronously with its corresponding data operands.

Complete Pipelined MAC Architecture

`timescale 1ns / 1ps
`default_nettype none

module mac_unit #(parameter WIDTH = 16) (
    input  wire                    clk, rst_n,
    input  wire                    stall,
    input  wire                    clear_acc,
    input  wire [WIDTH-1:0]        a, b,
    input  wire                    valid_in,
    output reg  [2*WIDTH-1:0]      acc_out,
    output reg                     valid_out
);
    localparam H = WIDTH / 2;

    // ---- Stage 1: Partial product generation ----
    reg [2*WIDTH-1:0] pp_lo, pp_hi;
    reg               valid_s1, clear_s1;

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            pp_lo <= 0; pp_hi <= 0; valid_s1 <= 1'b0; clear_s1 <= 1'b0;
        end else if (!stall) begin
            pp_lo    <= a * b[H-1:0];
            pp_hi    <= (a * b[WIDTH-1:H]) << H;
            valid_s1 <= valid_in;
            clear_s1 <= clear_acc;   // Passthrough signal registration
        end
    end

    // ---- Stage 2: Final product addition ----
    reg [2*WIDTH-1:0] product_reg;
    reg               valid_s2, clear_s2;

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            product_reg <= 0; valid_s2 <= 1'b0; clear_s2 <= 1'b0;
        end else if (!stall) begin
            product_reg <= pp_lo + pp_hi;
            valid_s2    <= valid_s1;
            clear_s2    <= clear_s1;   // Passthrough signal propagation
        end
    end

    // ---- Stage 3: Accumulation and feedback ----
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            acc_out   <= {2*WIDTH{1'b0}};
            valid_out <= 1'b0;
        end else if (!stall) begin
            if (clear_s2)
                acc_out <= product_reg;                 // Start new accumulation sequence
            else if (valid_s2)
                acc_out <= acc_out + product_reg;       // Continue running accumulation
            // Invalid bubble cycles hold acc_out implicitly
            valid_out <= valid_s2;
        end
    end
endmodule

Observe three architectural details:

  1. Control Signal Alignment: The signal clear_acc enters on cycle 0 but applies to the accumulator in cycle 2 via clear_s1 and clear_s2.
  2. Priority Resolution: In Stage 3, clear_s2 takes precedence over valid_s2 to guarantee clean sequence initialization.
  3. Implicit Value Retention: If neither clear_s2 nor valid_s2 asserts, acc_out retains its current value, preventing bubble cycles from corrupting accumulation totals.

Cycle-Accurate Self-Checking Testbench

module tb_mac_unit;
    parameter WIDTH = 8;
    reg clk = 0, rst_n, stall, clear_acc, valid_in;
    reg [WIDTH-1:0] a, b;
    wire [2*WIDTH-1:0] acc_out;
    wire valid_out;

    mac_unit #(.WIDTH(WIDTH)) dut (
        .clk(clk), .rst_n(rst_n), .stall(stall), .clear_acc(clear_acc),
        .a(a), .b(b), .valid_in(valid_in),
        .acc_out(acc_out), .valid_out(valid_out)
    );

    always #5 clk = ~clk;

    // Apply one pair of operands synchronously on negative clock edge
    task feed;
        input [WIDTH-1:0] av, bv;
        input clr;
        begin
            a = av; b = bv; clear_acc = clr; valid_in = 1;
            @(negedge clk);
        end
    endtask

    integer errors = 0;

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

        rst_n = 0; stall = 0; clear_acc = 0; valid_in = 0; a = 0; b = 0;
        @(negedge clk); rst_n = 1;

        // Sequence: (3 * 4) + (5 * 6) + (2 * 2) = 12 + 30 + 4 = 46
        feed(3, 4, 1);   // Start accumulation with first product
        feed(5, 6, 0);
        feed(2, 2, 0);
        valid_in = 0; clear_acc = 0;

        // Wait 2 clock edges for final product to reach accumulator
        repeat (2) @(negedge clk);

        if (acc_out == 46 && valid_out == 1'b1)
            $display("PASS: acc_out=%0d valid_out=%b on expected cycle", acc_out, valid_out);
        else begin
            $display("FAIL: acc_out=%0d valid_out=%b (expected 46, 1)", acc_out, valid_out);
            errors = errors + 1;
        end

        // Verify stall behavior: assert stall and confirm register freeze
        feed(10, 10, 1);   // New accumulation starts: expected 100
        valid_in = 0; clear_acc = 0;
        repeat (2) @(negedge clk);
        stall = 1;
        repeat (3) @(negedge clk); // Hold stall for 3 clock cycles
        if (acc_out !== 100 || valid_out !== 1'b1) begin
            $display("FAIL: outputs modified during stall, acc_out=%0d valid_out=%b", acc_out, valid_out);
            errors = errors + 1;
        end
        stall = 0;
        @(negedge clk);
        if (valid_out !== 1'b0) begin
            $display("FAIL: valid_out must deassert after empty pipeline drain");
            errors = errors + 1;
        end

        if (errors == 0) $display("ALL TESTS PASSED");
        else $display("%0d TEST(S) FAILED", errors);

        $finish;
    end
endmodule

The testbench confirms both mathematical results and exact cycle latency. It also verifies that asserting stall freezes all register outputs without losing state.

Systolic arrays in neural network hardware accelerators replicate thousands of these pipelined MAC units to execute high-throughput matrix computations.

Verification Exercise

Add an overflow_out flag to detect when accumulation exceeds the representable unsigned range. Ensure the overflow indicator updates synchronously with the accumulator stage.

View Solution
reg overflow_out;

always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        acc_out      <= {2*WIDTH{1'b0}};
        valid_out    <= 1'b0;
        overflow_out <= 1'b0;
    end else if (!stall) begin
        if (clear_s2) begin
            acc_out      <= product_reg;
            overflow_out <= 1'b0;   // Reset overflow status on new accumulation
        end else if (valid_s2) begin
            // Unsigned carry-out detection
            {overflow_out, acc_out} <= {1'b0, acc_out} + {1'b0, product_reg};
        end
        valid_out <= valid_s2;
    end
end

Because operands are unsigned, carry-out from the addition indicates numerical overflow directly.

This completes Section 7.3. Section 7.4 explores First-In, First-Out (FIFO) buffer memories for elastic data storage.

← Stalls and Bubbles Why Every Design Has a FIFO in It →