0

Is it possible to have variable module name which could then be picked by some parameter? I am looking for the syntax for if...else inside a macro definition.

module test;

  `define NAME(x) if (x == 0) mod_e else mod_w
  generate
    for (genvar i = 0; i < 2; i++) begin
      `NAME(i) inst_name (.a(a),.b(b),...);
    end
  endgenerate

endmodule

One way is to do it like below, but it requires connecting all ports 2 times, which is inconvenient for modules with hundreds of IOs and prone to errors.

module test;

  generate
    if (SOME_PARAM == 0) begin
      mod_e inst_name (.a(a),.b(b),...);
    end else begin
      mod_w inst_name (.a(a),.b(b),...);
    end
  endgenerate

endmodule
1
  • There's some expression evaluation happening in the port list and the names don't match. I didn't show it here. Commented May 6, 2020 at 23:13

2 Answers 2

1

As a possible solution to your hundreds of ports issues, assuming that all instances have the same set of ports, you can approach this problem differently, by defining an instance macro:

`define INST(mod_name) mod_name inst_name(.a(a), .b(b), ...);

if(SOME_PARAM == 0)
   `INST(mod_e)
else
   `INST(mod_w)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this would be a good solution within the given language limitations. Thank you!
0

You can't use macros to do instance specific mapping. Macros get expanded in a pre-processing step before the generate block is parsed.

The only thing that comes close to this would be the config block, which lets you choose a module of the same name from a different library for a specific instance. But there's no conditional operators.

1 Comment

OK, thanks for explaining the difference. I believe Serge's answer below comes close and is a good way to achieve what I am looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.