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.
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.
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).
Synthesis may share adder for ADD/SUB; the conceptual model is parallel compute + select.
Never scatter raw opcode values. Name them once with localparam — your case statement, decoder, and testbench all reference the same names.
| Opcode | Operation | Result |
|---|---|---|
OP_ADD (0) | Add | a + b |
OP_SUB (1) | Subtract | a - b |
OP_AND (2) | Bitwise AND | a & b |
OP_OR (3) | Bitwise OR | a | b |
OP_XOR (4) | Bitwise XOR | a ^ b |
OP_SHL (5) | Shift left | a << 1 |
OP_PASS (6) | Pass A | a |
| default (7) | Pass B | b |
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
Flags summarize the result for branch logic and interrupt handlers. Our minimal set:
| Flag | Name | Meaning | RTL |
|---|---|---|---|
| Z | Zero | Result is all zeros | result == 0 |
| N | Negative | MSB of result (signed sign) | result[WIDTH-1] |
| C | Carry | Unsigned carry-out of add/sub | From wide arithmetic |
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.
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).
This is the reference implementation. One combinational block owns result and flags. Default assignments at the top prevent latches. Full case on opcode.
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
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.
Some teams pre-compute all results into wires, then mux with a single assign. Equivalent hardware, different readability trade-off.
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.
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.
// 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) );
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.
Selects one of N pre-computed datapath results.
Shared add/sub paths with wide carry for C flag.
Post-mux decode — Z/N from result, C from arithmetic.
parameter WIDTH on all internal buses and compareslocalparamalways @(*) owns result and flagscase — no latchescase with default for unused opcodesresult, not stale intermediatesalways @(*): defaults first, full case, flags with result.