Following is the excerpt from SystemVerilog LRM about important features of define macro.
1. In Verilog, the ‘define macro text can include a backslash ( \ ) at the end of a line to show continuation on the next line.
2. In SystemVerilog, the macro text can also include `", `\`" and ``.
1. In Verilog, the ‘define macro text can include a backslash ( \ ) at the end of a line to show continuation on the next line.
2. In SystemVerilog, the macro text can also include `", `\`" and ``.
An `" overrides the usual lexical meaning of ", and indicates that the expansion should include an actual quotation mark. This allows string literals to be constructed from macro arguments.
A `\`" indicates that the expansion should include the escape sequence \", e.g.
`define msg(x,y) `"x: `\`"y`\`"`"
This expands:
$display(`msg(left side,right side));
to:
$display("left side: \"right side\"");
3. A `` delimits lexical tokens without introducing white space, allowing identifiers to be constructed from arguments,
`define foo(f) f``_suffix
This expands:
`foo(bar)
to:
bar_suffix
These three are most important features because using them we can create customizable data_type and generic or reusable SystemVerilog Components.
For example consider following macro,
`define MY_DATA_TYPE(A) A
Using this macro, I can do following.
Instead of writing,
Instead of writing,
integer a;
I can write,
`MY_DATA_TYPE(integer) a;
You might think that is it advanced use of Macro? But when you read Reusable Channel using Define Macro of this article, you will realize that how this simple feature of define macro can help in creating generic/reusable components. Here, only intention is to convey that "Using define macro you can 'pass' data_type as argument".
define macro consider its argument as 'text only', it doesn't impose rule of 'keyword' or 'data type' on that. So passing data type as argument to define macro doesn't result into any compilation error.
These features can be used as an alternate option to 'Parameterization feature of SystemVerilog' (Parameterized Classes). If you are using EDA tool or SystemVerilog Compiler that doesn't support 'Parameterized class', you can use define macro as supplement for that, to make generic or reusable components.
Let's go through how to create generic/reusable SystemVerilog components using 'Parameterized Class' and Using 'Define Macro'.
Next: Reusable Channel using Parameterized class
Please provide examples other than BOOK, which can provide something new for us.
ReplyDeleteCan I open a file using $fopen in macro ?
Deletemacro can be used as a guard against multiple compilation before and after the class
ReplyDeletestatements. please give explanation to this statement.
Go read the book Angel. Why stopping by here?
ReplyDeleteCan I use define like this:
ReplyDelete`define type 1
`define times 10
`define cmd `times`type
Actually I want to achieve :
`cmd = 101 in binary so there should not be any gap in between two define values.
Very Helpful Sandeep bhai..!!
ReplyDelete