The Ultimate Hitchhiker's Guide to Verification

Dumping ground of useful links/articles/tips/tricks on System Verilog/VMM/OVM as and when I stumble upon them and some of my views on it :)

Showing posts with label Macro. Show all posts
Showing posts with label Macro. Show all posts

System Verilog `define macros : Why and how to use them (and where not to use!!!)

I most confess, I have become a very big fan of SystemVerilog `define macro for the amount of effort I save because of using it. The big advantage of using this is that you can concisely describe your intention in a more readable (and less SystemVerilog syntax) using macro. Another advantage is you need to change only at one place if you find out that you need to change the expression you have used in 100 of places. The 3rd reason is that it is widely used in industry. Some example worth quoting are

  1. Using SystemVerilog Assertions for Functional Coverage
  2. Using SystemVerilog Assertions for Creating Property-Based Checkers
  3. SystemVerilog Assertions Design Tricks and SVA Bind Files
  4. VMM and especially VMM data macro
Next question : Where shall I get the required info on how to use SystemVerilog `define macro
Ans : Web (thanks to the all powerful demi-god of internet ... Google)
  1. http://www.google.co.in/#hl=en&safe=off&q=Systemverilog+Macro 
  2. System Verilog LRM 3.1a : Section : 25.2
  3. Sandeep Vaniya's Advanced Use of define macro in SystemVerilog (not so advanced actually !!!)
From reading these it might be apparent that define macro can be used for adding a postfix to the variable, but not prefix. I was thinking the same after reading the description (they don't have a single example of adding prefix to the variable via `define. Confused ?? Let me explain by giving a concrete example

// Example macro for a coverage class
// Aim : want to get ABC_cp : coverpoint ABC {bins b = {1}; }
// by calling `COV(ABC)
`define COV(__name) __name``_cp : coverpoint __name {bins b = {1}; }

// Next
//     What to do if I want cp_ABC in place of ABC_cp as for the above example
//     NOTE : I can't use cp_``__name as cp_ is not an input to the macro

// Solution
//     Use nested macros
`define PREFIX(__prefix, __name) __prefix``__name
`define COV2(__name) `PREFIX(cp_,__name) : coverpoint __name {bins b = {1}; }

Nested macro is not a new thing in SV. They are being extensively used in VMM data macro class. But no example of nested macro and achieving of addition pre_fix to variable name in `define macro is bit puzzling to me. I tried the above in vcs and seems to work perfectly fine.

Next, where not to use `define (SV other better alternatives to `define these cases)
  1. New Verilog-2001 Techniques for Creating Parameterized Models (or Down With `define and Death of a defparam!): Bit old but still usefull

About Me

My photo
I am from Sambalpur, Orissa, India. Have done Btech and Mtech in ECE from IIT Kharagpur. Currently working as Lead Member Technical Staff at Mentor Graphics Noida

My Feedburner

Followers

Search This Blog

My Shelfari Bookshelf

Shelfari: Book reviews on your book blog

 

Traffic Summary