RTL Design / Module 4: Combinational Design from Scratch
Lesson 4.3

Combinational ALU building blocks

An ALU is the datapath workhorse: it computes on two operands and exposes status flags the rest of the chip consumes. Here we build a clean, parameterized combinational ALU from the primitives you already know — opcode mux, shared adder, bitwise ops, shifts, and the classic Z / N / C flags — using one well-structured always @(*) block.

Core idea

Compute all candidate results in parallel, then mux the opcode-selected result to the output. Flags are derived from the selected result (and carry for arithmetic ops). Parameterize WIDTH once — reuse everywhere.

1

ALU datapath architecture

A typical combinational ALU has two operand buses, a 3–4 bit opcode, one result bus, and a few flag outputs. Everything settles in one cycle (no registers inside the ALU itself).

ALU block diagram

Synthesis may share adder for ADD/SUB; the conceptual model is parallel compute + select.

A, B→ func units→ opcode mux→ result + flags
2

Opcode map — localparam discipline

Never scatter raw opcode values. Name them once with localparam — your case statement, decoder, and testbench all reference the same names.

OpcodeOperationResult
OP_ADD (0)Adda + b
OP_SUB (1)Subtracta - b
OP_AND (2)Bitwise ANDa & b
OP_OR (3)Bitwise ORa | b
OP_XOR (4)Bitwise XORa ^ b
OP_SHL (5)Shift lefta << 1
OP_PASS (6)Pass Aa
default (7)Pass Bb
opcode_defs.v
localparam OP_ADD  = 3'd0;
localparam OP_SUB  = 3'd1;
localparam OP_AND  = 3'd2;
localparam OP_OR   = 3'd3;
localparam OP_XOR  = 3'd4;
localparam OP_SHL  = 3'd5;
localparam OP_PASS = 3'd6;
// 3'd7 reserved or OP_PASS_B
3

Status flags — Z, N, C

Flags summarize the result for branch logic and interrupt handlers. Our minimal set:

FlagNameMeaningRTL
ZZeroResult is all zerosresult == 0
NNegativeMSB of result (signed sign)result[WIDTH-1]
CCarryUnsigned carry-out of add/subFrom wide arithmetic
Analogy

Flags are the ALU's facial expression — the datapath does the math, but Z/N/C tell the control unit "was it zero?", "was it negative?", "did we spill past the cup rim?" without re-reading the full result bus.

Trap

Computing Z/N from the wrong intermediate — e.g. flags from a+b while opcode selected AND — causes branch bugs that only show on certain instructions. Flags must follow the muxed result (and carry from the arithmetic path when relevant).

4

Full parameterized ALU — clean always @(*)

This is the reference implementation. One combinational block owns result and flags. Default assignments at the top prevent latches. Full case on opcode.

combo_alu.v
module combo_alu #(
  parameter WIDTH = 8
) (
  input  wire [WIDTH-1:0] a,
  input  wire [WIDTH-1:0] b,
  input  wire [2:0]       opcode,
  output reg  [WIDTH-1:0] result,
  output reg              flag_z,
  output reg              flag_n,
  output reg              flag_c
);
  localparam OP_ADD  = 3'd0;
  localparam OP_SUB  = 3'd1;
  localparam OP_AND  = 3'd2;
  localparam OP_OR   = 3'd3;
  localparam OP_XOR  = 3'd4;
  localparam OP_SHL  = 3'd5;
  localparam OP_PASS = 3'd6;

  wire [WIDTH:0] sum_wide;
  wire [WIDTH:0] sub_wide;
  assign sum_wide = a + b;
  assign sub_wide = a - b;

  always @(*) begin
    // Defaults — latch prevention
    result  = {WIDTH{1'b0}};
    flag_z  = 1'b0;
    flag_n  = 1'b0;
    flag_c  = 1'b0;

    case (opcode)
      OP_ADD: begin
        result  = sum_wide[WIDTH-1:0];
        flag_c  = sum_wide[WIDTH];
        flag_z  = (result == {WIDTH{1'b0}});
        flag_n  = result[WIDTH-1];
      end
      OP_SUB: begin
        result  = sub_wide[WIDTH-1:0];
        flag_c  = sub_wide[WIDTH];   // borrow as carry semantics
        flag_z  = (result == {WIDTH{1'b0}});
        flag_n  = result[WIDTH-1];
      end
      OP_AND: begin
        result = a & b;
        flag_z = (result == {WIDTH{1'b0}});
        flag_n = result[WIDTH-1];
      end
      OP_OR:  begin
        result = a | b;
        flag_z = (result == {WIDTH{1'b0}});
        flag_n = result[WIDTH-1];
      end
      OP_XOR: begin
        result = a ^ b;
        flag_z = (result == {WIDTH{1'b0}});
        flag_n = result[WIDTH-1];
      end
      OP_SHL: begin
        result = a << 1;
        flag_c = a[WIDTH-1];         // bit shifted out
        flag_z = (result == {WIDTH{1'b0}});
        flag_n = result[WIDTH-1];
      end
      OP_PASS: begin
        result = a;
        flag_z = (result == {WIDTH{1'b0}});
        flag_n = result[WIDTH-1];
      end
      default: begin
        result = b;
        flag_z = (result == {WIDTH{1'b0}});
        flag_n = result[WIDTH-1];
      end
    endcase
  end
endmodule
Structure pattern

1) Declare wide arithmetic wires outside always. 2) Default all outputs inside always. 3) One case on opcode. 4) Each branch sets result + flags together. This pattern scales to 16+ opcodes without becoming unreadable.

5

Alternative: compute-then-mux style

Some teams pre-compute all results into wires, then mux with a single assign. Equivalent hardware, different readability trade-off.

alu_mux_style.v
wire [WIDTH-1:0] w_add = a + b;
wire [WIDTH-1:0] w_sub = a - b;
wire [WIDTH-1:0] w_and = a & b;
wire [WIDTH-1:0] w_or  = a | b;
wire [WIDTH-1:0] w_xor = a ^ b;
wire [WIDTH-1:0] w_shl = a << 1;

always @(*) begin
  case (opcode)
    OP_ADD:  result = w_add;
    OP_SUB:  result = w_sub;
    OP_AND:  result = w_and;
    OP_OR:   result = w_or;
    OP_XOR:  result = w_xor;
    OP_SHL:  result = w_shl;
    OP_PASS: result = a;
    default: result = b;
  endcase
  flag_z = (result == {WIDTH{1'b0}});
  flag_n = result[WIDTH-1];
end

Synthesis note: Both styles usually produce similar netlists. The case-per-opcode style makes per-op carry semantics explicit; the mux style centralizes flag decode. Pick one and stay consistent across the project.

6

WIDTH parameter — one knob for the whole ALU

Parameterizing WIDTH lets you instantiate 8-bit ALUs for a microcontroller or 32-bit for a core without rewriting logic. All buses, zero compares, and MSB index scale automatically.

alu_instances.v
// 8-bit ALU for peripheral datapath
combo_alu #(.WIDTH(8)) u_alu8 (
  .a(a8), .b(b8), .opcode(op),
  .result(r8), .flag_z(z), .flag_n(n), .flag_c(c)
);

// 32-bit ALU — same module, wider parameter
combo_alu #(.WIDTH(32)) u_alu32 (
  .a(a32), .b(b32), .opcode(op),
  .result(r32), .flag_z(z), .flag_n(n), .flag_c(c)
);
7

Opcode mux = lesson 4.1 in practice

The ALU result mux is a textbook N:1 mux on width WIDTH. Your decoder lesson connects directly: an instruction decoder can drive opcode from a one-hot control bus converted to binary, or from register fields.

🔀

Mux (4.1)

Selects one of N pre-computed datapath results.

➕

Adder (4.2)

Shared add/sub paths with wide carry for C flag.

🏷️

Flags

Post-mux decode — Z/N from result, C from arithmetic.

▶ Simulate your ALU opcode table
8

ALU RTL checklist

  • parameter WIDTH on all internal buses and compares
  • Opcode values named with localparam
  • Single always @(*) owns result and flags
  • Default assignments before case — no latches
  • Full case with default for unused opcodes
  • Flags computed from muxed result, not stale intermediates
  • Module header documents signed/unsigned and flag semantics

🎯 What to remember

  • ALU = parallel function units + opcode mux + flag decode.
  • ADD/SUB/AND/OR/XOR/shift/pass cover most teaching and many real ISAs.
  • Z = zero, N = MSB, C = carry/borrow/shift-out — define C per opcode.
  • Parameter WIDTH once; instantiate at 8, 16, or 32 without rewriting.
  • Clean always @(*): defaults first, full case, flags with result.
Advertisement