A Width-Configurable ALU
Module 2 implemented a fixed 4-bit Arithmetic Logic Unit (ALU).
This article combines parameterization, dynamic bit-width calculation using $clog2, and elaboration loops to build a fully configurable ALU.
Parameterized ALU Architecture
The parameterized ALU scales operand widths and opcode widths dynamically:
`timescale 1ns / 1ps
`default_nettype none
module alu #(
parameter WIDTH = 8,
parameter NUM_OPS = 8,
parameter OP_WIDTH = $clog2(NUM_OPS)
) (
input wire [WIDTH-1:0] a, b,
input wire [OP_WIDTH-1:0] op,
output reg [WIDTH-1:0] result,
output wire zero
);
localparam [OP_WIDTH-1:0] OP_ADD = 0,
OP_SUB = 1,
OP_AND = 2,
OP_OR = 3,
OP_XOR = 4,
OP_NOT = 5,
OP_SLL = 6,
OP_SRL = 7;
always @(*) begin
result = {WIDTH{1'b0}};
case (op)
OP_ADD: result = a + b;
OP_SUB: result = a - b;
OP_AND: result = a & b;
OP_OR: result = a | b;
OP_XOR: result = a ^ b;
OP_NOT: result = ~a;
OP_SLL: result = a << b[$clog2(WIDTH)-1:0];
OP_SRL: result = a >> b[$clog2(WIDTH)-1:0];
default: result = {WIDTH{1'b0}};
endcase
end
assign zero = (result == {WIDTH{1'b0}});
endmodule
The design derives port widths from WIDTH and NUM_OPS.
When an engineer sets WIDTH = 32, the inputs, outputs, and shift operations scale automatically.
The opcode width OP_WIDTH evaluates dynamically to $\lceil \log_2(\text{NUM_OPS}) \rceil$.
Optimizing Shift Operator Bit Slices
Notice the shift operand expression: a << b[$clog2(WIDTH)-1:0].
A barrel shifter requires only $\lceil \log_2(\text{WIDTH}) \rceil$ control bits.
Shifting an 8-bit vector by 8 or more positions always produces zero.
Selecting only the necessary lower bits of operand b reduces the gate count of the synthesized shifter.
Passing the complete bus width causes synthesis tools to infer unnecessarily large multiplexer networks.
Multi-Configuration Testbench Verification
Verification suites must validate parameterized modules across multiple configuration values:
module tb_alu_multi_width;
genvar w;
generate
for (w = 4; w <= 16; w = w + 4) begin : gen_width_test
// Elaborate distinct ALU instances for 4-bit, 8-bit, 12-bit, and 16-bit configurations
reg [w-1:0] a, b;
reg [2:0] op;
wire [w-1:0] result;
wire zero;
alu #(.WIDTH(w)) dut (.a(a), .b(b), .op(op), .result(result), .zero(zero));
initial begin
#(w); // Stagger start timestamps to separate console output
a = {w{1'b1}}; b = 1;
op = 3'd0; // OP_ADD
#10;
$display("WIDTH=%0d: %0d + %0d = %0d (zero flag=%b)", w, a, b, result, zero);
end
end
endgenerate
endmodule
This testbench uses a generate for loop to instantiate four parallel ALU instances across distinct bit widths.
Each generated instance creates an isolated hierarchy containing dedicated stimulus registers.
A single test script validates module behavior across all intended configuration widths.
Verification Exercise
Add a bitwise XNOR operation (OP_XNOR) to the ALU architecture.
Increase NUM_OPS from 8 to 9.
Verify that OP_WIDTH widens from 3 bits to 4 bits automatically.
View Solution
module alu #(
parameter WIDTH = 8,
parameter NUM_OPS = 9, // Increased from 8 to 9
parameter OP_WIDTH = $clog2(NUM_OPS) // Evaluates dynamically to 4 bits
) (
input wire [WIDTH-1:0] a, b,
input wire [OP_WIDTH-1:0] op,
output reg [WIDTH-1:0] result,
output wire zero
);
localparam [OP_WIDTH-1:0] OP_ADD = 0,
OP_SUB = 1,
OP_AND = 2,
OP_OR = 3,
OP_XOR = 4,
OP_NOT = 5,
OP_SLL = 6,
OP_SRL = 7,
OP_XNOR = 8;
always @(*) begin
result = {WIDTH{1'b0}};
case (op)
OP_ADD: result = a + b;
OP_SUB: result = a - b;
OP_AND: result = a & b;
OP_OR: result = a | b;
OP_XOR: result = a ^ b;
OP_NOT: result = ~a;
OP_SLL: result = a << b[$clog2(WIDTH)-1:0];
OP_SRL: result = a >> b[$clog2(WIDTH)-1:0];
OP_XNOR: result = ~(a ^ b);
default: result = {WIDTH{1'b0}};
endcase
end
assign zero = (result == {WIDTH{1'b0}});
endmodule
Setting NUM_OPS = 9 causes $clog2(9) to evaluate to 4.
All related opcode ports and local constants expand automatically without manual recalculation.
This concludes Section 7.1. Section 7.2 examines signed arithmetic rules, two’s-complement overflow conditions, and arithmetic saturation logic.