1

I need to use the current instance name (full hierarchical name) in a generate if condition:

generate
   if (current_instance_name() == "a.b.c.foo")
   ...

Is there any way to do that in Verilog or SystemVerilog? I know %m, but it only allows to print the instance name.

Thanks.

Qiu
  • 5,651
  • 10
  • 49
  • 56
Laurent A
  • 13
  • 2
  • 6
  • I'm pretty sure you can't do that. If you want a certain instance to contain different generated blocks, you'll have to use parameters and parameterize the instance accordingly. – Tudor Timi Sep 30 '14 at 11:39
  • Also, relying on full hierarchical paths inside a module is a very bad practice, because this breaks encapsulation. – Tudor Timi Sep 30 '14 at 11:40
  • The generate if condition requires a constant expression which can be evaluated during elaboration phase. – jclin Oct 01 '14 at 04:07

2 Answers2

3

The way to do this is

localparam string current_path = $sformatf("%m");

generate
if (current_path == "a.b.c.foo") begin ...
toolic
  • 57,801
  • 17
  • 75
  • 117
dave_59
  • 39,096
  • 3
  • 24
  • 63
0

You can still use %m by passing it to $psprintf which will return a string. The following function should solve your problem:

function string current_instance_name();
return $psprintf("%m");
endfunction
AldoT
  • 913
  • 1
  • 8
  • 34
  • 1
    While this does return the full path, I'm pretty sure he can't use it in a generate statement, because it doesn't resolve to an elaborate time constant. – Tudor Timi Sep 30 '14 at 11:41
  • 1
    Although the major simulator support `$psprintf` it is not part of the IEEE1364(Verilog) or IEEE1800(SystemVerilog) standards. To be complaint with the standard(s), use `$sformatf` – Greg Sep 30 '14 at 15:55
  • "%m" returns the current path where the format string locates, not the instance name. If you place the function in a package, and call the function inside your module/instance, the format string results in "package_name::current_instance_name" – jclin Oct 01 '14 at 03:03
  • @jclin, ha, ok that's also so true. It will return function location and function name. – AldoT Oct 01 '14 at 07:36
  • 2
    Instead of "%m", in VCS there is "%i" that will return string indicating from which instance/block the function was called. – AldoT Oct 01 '14 at 07:53