Module's parameter initialization troubles

54 Views Asked by At

I want to have universal function to initialize same module with different parameters. One of them is packed array with size depends on another parameter. I've tried something like this:

package my_pkg;

    class helper #(
        parameter seed = 0
    );
        
    static function int GetRand();
        return seed + 10;
    endfunction

    endclass : helper

endpackage : my_pkg

module myModule #(
    parameter myParam = 0
);

    initial begin
        $display("My value = %d", myParam);
    end

endmodule : myModule

module test_class #(
);

    myModule #(
        .myParam(my_pkg::helper #(.seed(5))::GetRand())
    ) i_myModule ();

endmodule : test_class 

Vivado synthesis tool builds it without any errors, but Questa Sim doesn't allow to simulate this code. I'm getting following errors:

\*\* Error: ../../../..TEST.sv(54): External function '\<class-spec#1\>::GetRand' may not be used in a constant expression.
\*\* Error: ../../../../TEST.sv(54): The expression for a parameter actual associated with the parameter name ('myParam') for the module instance ('i_myModule') must be constant.

May be some one could advice some workaround for this trouble?

Same code with class without any parameters simulates with no problems. But function stops be universal...

1

There are 1 best solutions below

0
toolic On

You can just declare the function in the package without a class:

package my_pkg;

    function int GetRand(int seed);
        return seed + 10;
    endfunction

endpackage : my_pkg

module test_class #();
    myModule #(
        .myParam( my_pkg::GetRand(5) )
    ) i_myModule ();
endmodule : test_class 

This compiled for me on questa without errors.