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 :)

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)
  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


martin August 19, 2010 at 3:43 AM  

Can I have a prefix and a suffix?

e.g. to cover individual bits of a register, I might normally have:

covergroup cgMyReg0OnWrite @(Trigger);
cpToggleReg0Bit0 : coverpoint MyReg0[0];
cpToggleReg0Bit1 : coverpoint MyReg0[1];
cpToggleReg0Bit2 : coverpoint MyReg0[2];
cpToggleReg0Bit3 : coverpoint MyReg0[3];

Repeat ad nauseum for MyReg1, MyReg2, MyReg3 etc.

I'd like to just write `REG_CG(Reg0) to expand to the group above. The following doesn't work:

`define REG_CG(Name) \
covergroup `PREFIX(cgMy, Name)``OnWrite @(Trigger); \
`PREFIX(cpToggle, Name``Bit0) : coverpoint `PREFIX(My, Name)``[0]; \
`PREFIX(cpToggle, Name``Bit1) : coverpoint `PREFIX(My, Name)``[1]; \
`PREFIX(cpToggle, Name``Bit2) : coverpoint `PREFIX(My, Name)``[2]; \
`PREFIX(cpToggle, Name``Bit3) : coverpoint `PREFIX(My, Name)``[3]; \

Subash August 22, 2010 at 12:21 PM  

In such situation I would have done this

`define JOIN(__prefix,__name,__postfix) __prefix``__nanme``__posfix

`define REG_CG(__Name) \
covergroup `JOIN(cgMy,__Name,OnWrite) @(Trigger); \
`JOIN(cpToggle,__Name,Bit0) : coverpoint `JOIN(My,__Name,[0]); \
`JOIN(cpToggle,__Name,Bit1) : coverpoint `JOIN(My,__Name,[1]); \
`JOIN(cpToggle,__Name,Bit2) : coverpoint `JOIN(My,__Name,[2]); \
`JOIN(cpToggle,__Name,Bit3) : coverpoint `JOIN(My,__Name,[3]); \

adwait August 26, 2010 at 5:33 AM  

In my case i define the top level path for RTL as a macro whose value is passed from command line.
i need to use that value to pass to a string variable. for example
`define TOP x.y
string F;
how would this look like ?

Post a Comment

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


Search This Blog

My Shelfari Bookshelf

Shelfari: Book reviews on your book blog


Traffic Summary