15

I have a preprocessor macro that represents a hierarchical path into my design.

Example:

`define HPATH top.chip.block

I need to construct a string which holds the value of `HPATH, so in my example the string should equal top.chip.block.

Is there a way to construct such a string?

None of the following attempts worked:

string hpath;
hpath = "`HPATH";     // Results in hpath = "`HPATH"
hpath = \"``HPATH\";  // Doesn't compile
hpath = `HPATH;       // Doesn't compile

I want hpath to be equivalent to doing this assignment hpath = "top.chip.block", but by using `HPATH instead of specifying the path again.

I cannot use %m because I need the string within my top-level UVM environment, not within a module.

A little more background: the reason I want to do this is because I am using backdoor register access in the UVM class library. The backdoor API requires setting the hdl_path to the blocks within the design, as a string. I already have `defines for the hierarchical paths and am trying to reuse those when specifying the hdl_paths so I don't have the same path defined twice. My testbench will use both the hierarchical path and the string path.

0

4 Answers 4

25

It's not possible to use a `define macro within a string literal. According to the SystemVerilog LRM:

Macro substitution and argument substitution shall not occur within string literals.

However a string literal can be constructed by using a macro that takes an argument and including the quotes in the macro by using ``"`.

Again, from the LRM:

An `" overrides the usual lexical meaning of " and indicates that the expansion shall include the quotation mark, substitution of actual arguments, and expansions of embedded macros. This allows string literals to be constructed from macro arguments.

So this works:

`define STRINGIFY(x) `"x`"
`define HPATH top.chip.block
string hpath = `STRINGIFY(`HPATH);
$display(hpath);                       // Output: "top.chip.block"

The example code can be run here: http://www.edaplayground.com/s/4/879

3

The use of $sformatf allows additional information to be added if needed.

`define STRINGIFY(DEFINE) $sformatf("%0s", `"DEFINE`")
1

I think this is what you're looking for.

`define HPATH `"top.chip.block`"
string hpath = `HPATH;

As toolic pointed out, the escape sequence %m will give you the current hierarchy when used in a $display statement so that may be a better option.

1
  • Not exactly - I still need to use `HPATH as a hierarchical reference in some other contexts so simply defining the macros as a string will not work.
    – dwikle
    Commented Mar 13, 2013 at 3:01
-1

`define HPATH top.chip.block

string hpath = "HPATH`";

Hope it helps.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 10, 2023 at 19:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.