What are Verilog Macros ?
Verilog macros allow you to define a piece of code that can be reused throughout your design. When a macro is invoked, it gets replaced by its defined content during compilation. This capability is especially useful for defining constants, parameterized expressions, or frequently used code snippets.
Syntax
The basic syntax for defining a macro is as follows:
`define MACRO_NAME [ (arguments) ] macro_body
- MACRO_NAME: The name of the macro.
- arguments: Optional parameters that can be passed to the macro. The opening parenthesis must follow the macro name with no space in between, otherwise the parenthesis becomes part of the macro body.
- macro_body: The code or expression that replaces the macro when it is invoked.
Macro Example
In this example, the ADD macro is defined to compute the sum of two numbers. When invoked, it expands to the addition operation.
`define ADD(a, b) ((a) + (b))
module example;
initial begin
$display("Sum: %d", `ADD(5, 3)); // Expands to ((5) + (3))
end
endmodule
Key Features of Verilog Macros
- Macros perform text substitution wherever they are referenced in the code.
- Macros can accept arguments, allowing for more dynamic and reusable code.
- Using directives like
`ifdef,`ifndef, and`endif, you can conditionally compile sections of code based on whether certain macros are defined. - Macros can span multiple lines using a backslash (\) at the end of each line except the last.
Multiline Macro Example
`define MULTI_LINE_MACRO \
begin \
$display("This is a multi-line macro."); \
end
Best Practices for Using Macros
1. Use Descriptive NamesChoose clear and descriptive names for your macros to convey their purpose. For example, use MAX_BUFFER_SIZE instead of MBS. This enhances readability and understanding of the code.
2. Consistent Naming ConventionsEstablish a naming convention for macros. A common approach is to use uppercase letters with underscores separating words (e.g., DATA_WIDTH). This helps differentiate macros from regular variables and functions.
3. Limit Macro ComplexityKeep macros simple and avoid complex logic within them. If a macro becomes too complicated, consider using a function or task instead. This prevents confusion and makes debugging easier.
4. Avoid Side EffectsWhen passing arguments to macros, ensure that they do not have side effects. This means that the arguments should not alter any state or produce unexpected results when expanded.
5. Parenthesize ArgumentsAlways parenthesize macro arguments to prevent unintended behavior due to operator precedence when the macro is expanded. For example:
`define SQUARE(x) ((x) * (x))
6. Document MacrosProvide comments explaining what each macro does and how it should be used. This documentation is especially useful for teams or when revisiting code after some time.
`define ADDR_WIDTH 32 // Address width for AHB data bus
// Pass hdl hierarchy path and module name to connect
`define MONITOR_IF(hdl_path, module) \
m_monitor_if.addr = ``hdl_path.``module.addr; \
m_monitor_if.data = ``hdl_path.``module.data; \
m_monitor_if.wr = ``hdl_path.``module.wr;
Note that the above code is possible only in SystemVerilog, but is a very common way to use macros.
7. Avoid Overuse of MacrosWhile macros can simplify code, overusing them can lead to reduced readability and maintainability. Use them judiciously, especially in critical sections of code where clarity is paramount. Use macros only when compilation has to process different text.
// How hard is it to read this ?
`ifdef PROJECT_X
`ifdef FEATURE1_ENABLE
delay = 10;
`endif
// statements
`elsif PROJECT_Y
`ifndef FEATURE2_ENABLE
`ifdef RESET_ALL
delay = 100;
`else
delay = 1000;
`endif
`else
delay = 50;
`endif
`endif
8. Utilize Include Files for Macro LibrariesCreate a separate file for commonly used macros and include it in your projects using the include directive. This promotes reuse and keeps your main code cleaner.
// common_macros.vh
`define DATA_WIDTH 16
`define ADDR_WIDTH 32
...
// main_module.v
`include "common_macros.vh"
module my_design (...);
...
endmodule
Practice Exercise
Challenge: Predict the Macro Results
Predict the three numbers this code prints.
`define SQ(x) x * x
`define SQP(x) ((x) * (x))
`define MAX(a, b) ((a) > (b) ? (a) : (b))
module tb;
initial begin
$display("%0d", `SQ(2 + 1));
$display("%0d", `SQP(2 + 1));
$display("%0d", 10 - `MAX(3, 4));
end
endmodule
Hint
A macro is replaced by its text before the code is compiled, so write out each call with the argument pasted in, then apply operator precedence.
Solution
5 9 6
`SQ(2 + 1) becomes 2 + 1 * 2 + 1, and multiplication goes first: 2 + 2 + 1 = 5. `SQP puts parentheses around the argument and the whole result, so it gives (3) * (3) = 9. `MAX is also fully parenthesized, so 10 - 4 = 6. Without the outer parentheses, the subtraction would be applied to the condition instead.
Hands-On Drills
Write and run these in ChipVerify Lab. Each drill is graded, and when your code fails, the grader explains which mistake it found.
Set a register's width and reset value with macros.
Fix a macro that breaks inside expressions.
Read more on Verilog Compiler Directives.