I want to write reusable SystemVerilog code so I need to have in module one variable with type, depending on a parameter (not different bus WIDTH, but different structs). Maybe it is possible to have one parametrized structure or to have different structures but in this case how to declare variable selecting needed struct as a type. Different structs could be declared in generte-if blocks, but they will be local to it blocks, so outside can't to get them. Maybe type conversion might help somehow.
P. S. Interfaces are not allowed to use as well as defines (macros are allowed).
Code example, I want to achieve:
typedef struct packed
{
logic a;
logic b;
} str_s;
typedef struct packed
{
logic a;
logic b;
logic c;
} str_s_with_c;
// A is a parameter
generate
if (A == "ON")
str_s_with_c array;
if (A == "OFF")
str_s array;
endgenerate
assign array.a = 1'b1;
logic t;
assign t = array.a;
or
generate
if (A == "ON")
begin
typedef struct packed
{
logic a;
logic b;
} str_s;
end
if (A == "OFF")
begin
typedef struct packed
{
logic a;
logic b;
logic c;
} str_s;
end
endhenerate
str_s array;
array.a = 1.b1;
A usual way of doing it is to pass struct as a parameter type to the module instance.
Answering the comment below for a single instance. In this case you can use a
generateblock, instantiating the module conditionally:One caveat. You'd better declare the structs outside of the generate statements. LRM does not prevent you from declaring them inside, but this feature is not implemented in at least some of the compilers.