A basic question

What is the difference between
parameter
defparam
localparam
`define

defparam is a statement that can be used to modify the value of a parameter. A localparam can not be directly modified by defparam. But if the value of a localparam is defined as an expression involving one or more parameters, changing such a parameter would affect the value of the dependent localparam as well.

A parameter, though its visibility is generally limited to the module it is defined in, its value can be modified by a defparam statement anywhere in the verilog code via a hierarchical reference to the parameter. Ability to modify a parameter in this global manner makes the verilog code susceptible to unintended side-effects. Using localparam makes the code more readable/maintainable – you know the value of the parameter for sure, you do not have to search the whole code to see where this parameter could be re-defined. See section 6.20.4 of the 2017 LRM for more details.

`define is a completely different beast. Anything with a back-tick in Verilog concerns the verilog preprocessor. The preprocessor runs before the actual compiler and replaces all the macros with the literal definitions of the macros. A general coding guideline is to avoid preprocessor macros. But it becomes difficult to follow it in Verilog.

Use localparam where you can. If your code needs more configurability, you may need to use parameters. Use `define only if you can not avoid its usage.

1 Like