Macros

What is the purpose of using macros in the beginning of a SV code.

For ex,

ifndef GUARD_TESTCASE define GUARD_TESTCASE

program testcase(…);

initial
begin
$display(" ******************* Start of testcase ****************");

#1000;
end

final
$display(" ******************** End of testcase *****************");

endprogram
`endif

Hi,

Verilog/System Verolog has following conditional compiler directives.

ifdef else
elsif endif
`ifndef

The ifdef compiler directive checks for the definition of a text_macro_name. If the text_macro_name is defined, then the lines following the ifdef directive are included. If the text_macro_name is not defined and an else directive exists, then this source is compiled. The ifndef compiler directive checks for the definition of a text_macro_name. If the text_macro_name is not defined, then the lines following the ifndef directive are included. If the text_macro_name is defined and an else directive exists, then this source is compiled. If the elsif directive exists (instead of the else) the compiler checks for the definition of the text_macro_name. If the name exists the lines following the elsif directive are included. The elsif directive is equivalent to the compiler directive sequence else ifdef … endif. This directive does not need a corresponding endif directive. This directive must be preceded by an ifdef or ifndef directive.

If you define your source code under any directive, it will compile according to directive definition. If compiler came across the same file again then due to the text_macro_name, it will not look into to the source code and do not compile again. Which will help you to save the compilation time.

I hope this will solve your doubt.

Best Regards,
Chetan Shah
Sr. ASIC Verification Engineer

Product Engineering Services
Software | Embedded | Semiconductor
www.einfochips.com
Frost & Sullivan Company of the Year 2013-14

I’m assuming you are really asking why the `ifdef’s are being used as a wrapper around the code, not necessarily how they work.

This is a technique take from other languages like C/C++ that have header files that get included in your source code. These header files contain definitions and function prototypes used by your source code. Sometimes you have multiple files that include the same header file, and the `ifdef wrapper prevents the file from being compiled twice. This saves compilation time, and prevents compiler errors in cases where the same definition is not allows to be declared twice.

Sometimes, people put these wrappers around all their code even if they know their compilation scripts only compile their code once. Modules, programs and interfaces are typically not `included, but the wrapper is a hard habit to break.

In reply to cashah85:

thanks Chetan. It was useful.

In reply to dave_59:

thank you.