You have several ways to get the width of a logic vector that is declared as a localparam in SystemVerilog. The most direct and recommended method is using the $bits system function.
Here are the common methods:
1. Using $bits (Recommended)
The $bits system function returns the number of bits in an expression or a data type. This is the most straightforward and idiomatic way.
module get_width_example;
// Define a localparam as a logic vector
localparam logic [15:0] DATA_BUS = 16'hABCD;
localparam logic [7:0] ADDR_BUS = 8'h12;
localparam logic [3:0] CTRL_SIGS = 4'b1010;
// Get the width using $bits
localparam int DATA_BUS_WIDTH = $bits(DATA_BUS); // Will be 16
localparam int ADDR_BUS_WIDTH = $bits(ADDR_BUS); // Will be 8
localparam int CTRL_SIGS_WIDTH = $bits(CTRL_SIGS); // Will be 4
// You can also use $bits directly on the type if you have a typedef
typedef logic [31:0] my_dword_t;
localparam my_dword_t SOME_DWORD = 32'hCAFE_BABE;
localparam int DWORD_TYPE_WIDTH = $bits(my_dword_t); // Will be 32
localparam int DWORD_PARAM_WIDTH = $bits(SOME_DWORD); // Will also be 32
initial begin
$display("DATA_BUS width: %0d", DATA_BUS_WIDTH);
$display("ADDR_BUS width: %0d", ADDR_BUS_WIDTH);
$display("CTRL_SIGS width: %0d", CTRL_SIGS_WIDTH);
$display("DWORD_TYPE_WIDTH: %0d", DWORD_TYPE_WIDTH);
$display("DWORD_PARAM_WIDTH: %0d", DWORD_PARAM_WIDTH);
// Example of using it in a condition
if (DATA_BUS_WIDTH > 8) begin
$display("DATA_BUS is wider than a byte.");
end
end
endmodule
Output:
DATA_BUS width: 16
ADDR_BUS width: 8
CTRL_SIGS width: 4
DWORD_TYPE_WIDTH: 32
DWORD_PARAM_WIDTH: 32
DATA_BUS is wider than a byte.
2. Using $left, $right, $high, $low
You can also calculate the width using the system functions that return the bounds of the vector:
$left(expression): Returns the left bound of the expression's range.
$right(expression): Returns the right bound of the expression's range.
$high(expression): Alias for $left if [left:right], or right if [right:left] (i.e., the numerically higher index).
$low(expression): Alias for $right if [left:right], or left if [right:left] (i.e., the numerically lower index).
The width is then calculated as abs($left(param) - $right(param)) + 1 or, more commonly if you know the declaration style ([MSB:LSB]): $left(param) - $right(param) + 1.
module get_width_bounds_example;
localparam logic [15:0] DATA_BUS_UP = 16'hABCD; // [MSB:LSB]
localparam logic [0:7] ADDR_BUS_DOWN = 8'h12; // [LSB:MSB] (less common for ports/params)
localparam int DATA_BUS_MSB = $left(DATA_BUS_UP); // 15
localparam int DATA_BUS_LSB = $right(DATA_BUS_UP); // 0
localparam int DATA_BUS_WIDTH_CALC = DATA_BUS_MSB - DATA_BUS_LSB + 1; // 15 - 0 + 1 = 16
localparam int ADDR_BUS_LEFT = $left(ADDR_BUS_DOWN); // 0
localparam int ADDR_BUS_RIGHT = $right(ADDR_BUS_DOWN); // 7
// Careful with subtraction for [LSB:MSB] if not using abs()
localparam int ADDR_BUS_WIDTH_CALC = $abs(ADDR_BUS_LEFT - ADDR_BUS_RIGHT) + 1; // abs(0-7)+1 = 8
// Or using $high and $low
localparam int ADDR_BUS_WIDTH_HL = $high(ADDR_BUS_DOWN) - $low(ADDR_BUS_DOWN) + 1; // 7 - 0 + 1 = 8
initial begin
$display("DATA_BUS_UP: MSB=%0d, LSB=%0d, Width=%0d",
DATA_BUS_MSB, DATA_BUS_LSB, DATA_BUS_WIDTH_CALC);
$display("ADDR_BUS_DOWN: Left=%0d, Right=%0d, Width (abs)=%0d, Width (HL)=%0d",
ADDR_BUS_LEFT, ADDR_BUS_RIGHT, ADDR_BUS_WIDTH_CALC, ADDR_BUS_WIDTH_HL);
end
endmodule
Output:
DATA_BUS_UP: MSB=15, LSB=0, Width=16
ADDR_BUS_DOWN: Left=0, Right=7, Width (abs)=8, Width (HL)=8
While this method works, $bits is more concise and less prone to errors if you mix up left/right or indexing directions.
Why $bits is Preferred:
- Simplicity: It directly gives you the width without needing calculations.
- Clarity: The intent is immediately obvious.
- Robustness: It works correctly regardless of whether the vector is declared
[MSB:LSB] or [LSB:MSB].
In summary, always prefer $bits(your_localparam_vector) to get its width.