Of course. In SystemVerilog, checking for overflow when assigning a larger integer to a smaller fixed-width localparam is a crucial compile-time check to prevent silent truncation and ensure design correctness.
The problem is that this assignment is perfectly legal in SystemVerilog, but it often indicates a mistake:
// This will compile without any warning or error by default!
localparam integer WIDE_VAL = 70000; // Needs 17 bits (2^16-1 = 65535)
localparam logic [15:0] NARROW_VAL = WIDE_VAL; // Silently truncates to 4464 (70000 - 65536)
The best way to catch this is by using a generate block with a conditional check that calls the $error system task. This halts the compilation (or more accurately, the elaboration step) if the condition is met.
Best Practice: Using generate and $error
This is the most robust and standard method for performing compile-time parameter validation.
1. Checking for Unsigned Overflow
If your 16-bit localparam is unsigned, you need to check two things:
- The source value is not negative.
- The source value is not larger than the maximum value the 16-bit parameter can hold (
2^16 - 1 or 65535).
A more generic way to represent the maximum value is '1 cast to the target width, i.e., 16'('1).
Example:
module parameter_checker;
// --- Source Parameters ---
localparam integer SOURCE_VAL_OK = 65535;
localparam integer SOURCE_VAL_OVERFLOW = 65536;
localparam integer SOURCE_VAL_NEGATIVE = -1;
// --- Target Parameter Width ---
localparam int WIDTH = 16;
// --- The check and assignment ---
// Change SOURCE_VAL_* to see different outcomes
localparam integer SOURCE_VAL = SOURCE_VAL_OVERFLOW;
// The actual 16-bit localparam
localparam logic [WIDTH-1:0] TARGET_VAL = SOURCE_VAL;
// The compile-time check
initial begin : check_param_range
if (SOURCE_VAL < 0 || SOURCE_VAL > (1 << WIDTH) - 1) begin
$fatal(1, "FATAL: SOURCE_VAL (%0d) is out of range for a %0d-bit unsigned value.",
SOURCE_VAL, WIDTH);
end else begin
$info("INFO: SOURCE_VAL (%0d) is valid for a %0d-bit unsigned value.",
SOURCE_VAL, WIDTH);
end
end
// Alternative using a generate block, which is more common for this task
generate
if (SOURCE_VAL < 0) begin : check_negative
// This will fire if SOURCE_VAL is SOURCE_VAL_NEGATIVE
$error("COMPILE-TIME ERROR: SOURCE_VAL (%0d) cannot be negative for an unsigned target.", SOURCE_VAL);
end
// We can use $clog2 for a more concise check for positive values
// $clog2(N) returns the number of bits required to represent N
if (SOURCE_VAL > 0 && $clog2(SOURCE_VAL) > WIDTH) begin : check_overflow
// This will fire if SOURCE_VAL is SOURCE_VAL_OVERFLOW
$error("COMPILE-TIME ERROR: SOURCE_VAL (%0d) requires more than %0d bits.", SOURCE_VAL, WIDTH);
end
endgenerate
endmodule
How to Read the Output:
- If you compile with
SOURCE_VAL_OK, the compilation will succeed.
- If you compile with
SOURCE_VAL_OVERFLOW, the check_overflow generate block will trigger, and your compiler will halt with an error message like:
Error: COMPILER-TIME ERROR: SOURCE_VAL (65536) requires more than 16 bits.
- If you compile with
SOURCE_VAL_NEGATIVE, the check_negative generate block will trigger, halting compilation.
2. Checking for Signed Overflow
If your target is a signed type, the range is different. For 16 bits, it's -32768 to 32767.
- Maximum Value:
2^(WIDTH-1) - 1
- Minimum Value:
-2^(WIDTH-1)
Example:
module signed_parameter_checker;
localparam int WIDTH = 16;
// Define the valid range using the width
localparam signed int MAX_VAL = (1 << (WIDTH-1)) - 1; // 32767
localparam signed int MIN_VAL = -(1 << (WIDTH-1)); // -32768
// --- Source Parameters ---
localparam integer SOURCE_VAL_OK = -32768;
localparam integer SOURCE_VAL_TOO_HIGH = 32768;
localparam integer SOURCE_VAL_TOO_LOW = -32769;
// --- The check and assignment ---
localparam integer SOURCE_VAL = SOURCE_VAL_TOO_HIGH; // Change me to test
localparam logic signed [WIDTH-1:0] TARGET_VAL = SOURCE_VAL;
// The compile-time check
generate
if (SOURCE_VAL < MIN_VAL || SOURCE_VAL > MAX_VAL) begin : check_signed_range
$error("COMPILE-TIME ERROR: SOURCE_VAL (%0d) is outside the valid %0d-bit signed range [%0d:%0d].",
SOURCE_VAL, WIDTH, MIN_VAL, MAX_VAL);
end
endgenerate
endmodule
How to Read the Output:
- Compiling with
SOURCE_VAL_TOO_HIGH will trigger the $error and halt compilation, showing that 32768 is out of the valid range.
- Similarly,
SOURCE_VAL_TOO_LOW will also fail the check.
Alternative: Using a Function
For reusability, you can encapsulate the check inside a function. This is cleaner if you perform the same check in many modules.
// A function to check and return the value
// Note: Constant functions used for `localparam` assignments cannot have side effects like `$error`.
// So, we use the function in the generate block's condition instead.
function automatic bit is_unsigned_overflow(input integer val, input int width);
return val < 0 || (val > 0 && $clog2(val) > width);
endfunction
module functional_checker;
localparam int WIDTH = 16;
localparam integer SOURCE_VAL = 70000; // Will cause an error
localparam logic [WIDTH-1:0] TARGET_VAL = SOURCE_VAL;
generate
if (is_unsigned_overflow(SOURCE_VAL, WIDTH)) begin : check_it
$error("Overflow detected by function: %0d does not fit in %0d bits.", SOURCE_VAL, WIDTH);
end
endgenerate
endmodule
What Not to Use
assert statement: assert is primarily a simulation-time construct for checking dynamic behavior. While there are assert final and assertions in generate blocks, using a simple if...$error inside a generate block is the most direct and idiomatic way to perform a compile-time check on a static localparam value.
Summary
| Method | Pros | Cons | Best for |
|---|
generate/if/$error | Standard, idiomatic, clear. Halts compilation on failure. Highly flexible for any condition. | Can be slightly verbose if repeated often. | The recommended approach for all compile-time parameter validation. |
$clog2 in a check | Very concise for checking positive unsigned values. | Only works for positive integers. Doesn't check for negative values. | Quick check for bit-width requirements of a positive value. |
Function Call in a generate | Encapsulates logic, promotes reuse, makes the generate block cleaner. | Adds a layer of abstraction (the function definition). | Reusing the same validation logic across many different modules. |