Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

Calculating parameter value with self-written function

Hi,

I'm currently working on a project where I need to calculate the length of a specific vector with the help of a (constant) function.

I use QuartusII (13sp1) and Systemverilog.

The module parameters/ports look like this:

module LZD# (
    parameter INPUT_WIDTH = 54,
    parameter OUTPUT_WIDTH = 6
) (
    input logic  input_string,
    output logic  count
);

Inside this module I have two localparams:

localparam INTERN_WIDTH = 2**OUTPUT_WIDTH;
localparam POS_WIDTH = get_array_length;

Where get_array_length is a function, that returns a value based on some calculation only with the other parameters.

function int get_array_length(int OUT = OUTPUT_WIDTH, int N = INTERN_WIDTH);
    int result = 0,k;
    for (k=0; k<OUT; k++) begin : LOOP
        result = result + (OUT-k)*N/(2**(OUT-k));
    end
    return result;
endfunction

From what I have read about parameters, they can be assigned using functions if these are constant at compile time. Which my function should be, as it only uses parameters as inputs.

When I compile this module on its own, I don't get any errors and synthesis also looks as intended. But this module is just a submodule. When I try to compile the top-module, I always get the following error:

Error (10192): Verilog HDL Defparam Statement error at LZD.sv(24): value for parameter "POS_WIDTH" must be constant expression

(Line 24 is "localparam POS_WIDTH = get_array_length"; )

In the top-module, LZD is instantiated via

LZD# (
    .INPUT_WIDTH(2*MANTISSA_WIDTH+1),
    .OUTPUT_WIDTH(COUNT_WIDTH+1)
) LZD_Instance(
    .input_string(mantissa_negate),
    .count(leading_zero_count),
    .no_one(no_one)
);

and MANTISSA_WIDTH and COUNT_WIDTH are parameters inside the top-module.

parameter MANTISSA_WIDTH = 27;
parameter COUNT_WIDTH = 5;

Is this a bug, is QuartusII not able to get the parameter values out of the hierarchy or is there anything else wrong in my code? I tried to find anything related to such things, but all I could find were papers or posts that mention that it's possible to calculate a parameter with a constant function. (Which seems to work in case the module is not further instantiated)

Thanks for any help!
No RepliesBe the first to reply