Russell
Russell

Reputation: 3457

How to share constants between many files in Verilog?

I'm using a simple UART in Verilog that has as part of some definitions of ASCII values. For example:

parameter ASCII_a  = 8'h61;
parameter ASCII_b  = 8'h62;
parameter ASCII_c  = 8'h63;
parameter ASCII_d  = 8'h64;
parameter ASCII_e  = 8'h65;
parameter ASCII_f  = 8'h66;

etc. I'm trying to find the best way to create these parameters just once and then get access to them in multiple files. I'm used to VHDL when you can create a package and include that package where you like. I know that Verilog can use the include directive, but then I believe I need to surround it with ifdefs.

I would like to just create a SystemVerilog package, but I don't want to deal with Synthesis compatibility issues between vendor tools. (I know that Microsemi has this weird thing where you can include SV files, but they need to end in .v for example).

For you long-time Verilog coders, what's the preferred approach for this?

Upvotes: 5

Views: 6798

Answers (1)

teadotjay
teadotjay

Reputation: 1455

I wish I could say SystemVerilog packages would be the ideal choice, but like you, I have found compatibility issues with synthesis and formal verification tools that claim to be SystemVerilog compliant.

In lieu of that, I have seen two approaches commonly used:

1) Global `define statements, sourced once (or gratuitously sourced by every file that uses them):

`define ASCII_a 8'h61

I do not recommend this approach because of the risk of name collision, i.e. different IP modules using the same parameter name to represent different values. In this case, the effective value of the parameter is dependent on compile order, which is not what you want in most cases.

2) Parameter include files, commonly with a different extension (e.g. .vh, .vinc) to prevent them from being accidentally compiled outside a `include statement. For example:

my_params.vh:

localparam ASCII_a = 8'h61;
...

my_module.v:

`include "my_params.vh"

Some designers wrap their params file with ifdef, which is a good practice to use in cases where the IP includes multiple files and those files in turn include the parameter file.

`ifndef _my_params_h
`define _my_params_h
localparam ASCII_a = 8'h61;
...
`endif

Upvotes: 11

Related Questions