Electronic – Is it possible to use conditional statements to modify parameters at compile time in Verilog

system-verilogverilog

This question explains how to use Verilog parameters to combine constants from different modules at compile time. I am wondering if it is also possible to use conditional statements to modify parameter values at compile time.

Specifically, if the calculated total of the parameter values exceeds some maxLength, I want the parameter to 'wrap around' maxLength and be left with the remainder instead.

Pseudo-code example:

module toplevel {
    defparam myPole.offset = 95;

    pole myPole();
}

module pole {
    parameter offset = 0; // default value, overwritten by 95

    localparam x = 1;
    localparam y = 2;
    localparam z = 3;
    localparam maxLength = 100;

    localparam transitionConst = x + y + z + offset; // total is 101, this works as expected

    if(transitionConst > maxLength){
        transitionConst = transistionConst - maxLength;
    } // transitionConst should wrap around 100, and be left with 1
}

I looked at compiler directive tutorials at asic-world.com and TestBench.in, but the only if statements they describe are ifdefs. So, I suspect that Verilog does not support generic compile-time conditional statements. Is there a different approach I could use to accomplish this?

(P.S. I've seen Verilog Q&A on both EE.SE as well as StackOverflow. Which is the more appropriate community?)

Best Answer

You can use the conditional operator condition ? true_expression : false_expression as long as everything within the expression is a parameter or literal constant.

localparam transitionConst = (transitionConst <=  maxLength) ? x + y + z + offset : transistionConst - maxLength;

You can also use functions to define parameters, as long as all the function inputs are parameters or constants as well.