Macro

Deep Dive into Macros in SystemVerilog

SystemVerilog includes several special character sequences to assist with certain specific tasks. These sequences start with a backtick (`) and are followed by one or more characters. In this tutorial, we will focus on the double tick (``), the tick followed by a double quote (`"), and the tick followed by a backslash followed by a double quote (`\").

I. The Double Tick (``)

The double tick character sequence (``) is used to include whitespace characters, such as spaces, tabs, or newlines, in a macro definition. Normally, a macro definition ends at the end of the line, but if you use the double tick (``) sequence, the macro definition continues until it encounters another double tick.

Here's an example:

`define PRINT_MESSAGE `"
    This is a long message
    that spans multiple lines.
`"
initial begin
    $display(`PRINT_MESSAGE);
end

In the above example, `PRINT_MESSAGE is a macro that contains a string spanning multiple lines.

II. Tick Followed by a Double Quote (`")

The character sequence of a tick followed by a double quote (`") is used to insert a double quote character into a macro definition. Normally, double quotes in a macro definition are treated as starting or ending a string. But if you use `", the preprocessor inserts a double quote into the macro definition.

Here's an example:

`define DOUBLE_QUOTED_STRING `"This is a string inside double quotes`"
initial begin
    $display(`DOUBLE_QUOTED_STRING);
end

In the above example, `DOUBLE_QUOTED_STRING is a macro that contains a string enclosed in double quotes.

III. Tick Followed by a Backslash Followed by a Double Quote (`\")

The character sequence of a tick followed by a backslash followed by a double quote (`\") is used to insert a double quote character into a macro definition, just like `". The difference between `\" and `" is subtle and mostly comes into play when these sequences are used inside strings.

Here's an example:

`define QUOTED_STRING_WITH_SLASH `\"This string contains a backslash and double quotes.\`"
initial begin
    $display(`QUOTED_STRING_WITH_SLASH);
end

In this example, `QUOTED_STRING_WITH_SLASH is a macro that contains a string with a backslash followed by double quotes.

Have a Question?

Feel free to ask your question in the comments below.

Please Login to ask a question.

Login Now