SV中宏的用法总结(System Verilog Macro: A Powerful Feature for Design Verification Projects)

本文主要是介绍SV中宏的用法总结(System Verilog Macro: A Powerful Feature for Design Verification Projects),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文链接:https://www.design-reuse.com/articles/45979/system-verilog-macro-a-powerful-feature-for-design-verification-projects.html

System Verilog Macro: A Powerful Feature for Design Verification Projects

By Ronak Bhatt, Verification Engineer, eInfochips
System Verilog Macro: A Powerful Feature for Design Verification Projects
By Ronak Bhatt, Verification Engineer, eInfochips

For any design verification (DV) project, following best coding practices make life easier for the teammates. On the other hand, bad coding style leads to a lot of issues when the code is reused, or when it is handed over from one owner to another for any future enhancements. At times, it leads to a lot of rework and patches in the code, and makes the code really difficult to maintain in later stages of the project or for the future projects where it needs to be reused.

Quite often, while working on a design verification project, a need arises to divide a larger piece of code into a smaller chunks to make the code easier to read and debug (and also for the reusability purpose). Such smaller pieces of codes can be used at varied locations in the DV environment for multiple components/modules, etc.

Tasks and functions can be used to break the large, complex code into smaller and much simpler pieces of code which is easy to read and understand. Functions and tasks are mainly used to execute common functionality at several places in verification environment, but their usage is limited to the module or class access boundary in which they are defined. For some piece of code which is common and frequently used in different and completely isolated modules, the function/task can’t be directly reused due to their access boundary limitations.

To take a simple example, if a task-function has common code for two different monitors and for two different interfaces, a DV engineer mostly adds duplicate code in both the monitors. There are many other cases where we see code duplication. “System Verilog Macro” is one of the many solutions to address such duplication.

Such macro is very efficient and can help save a lot of time if used properly in the SV environment. This paper talks about such SV Macro and their syntaxes and also offers a few examples of where it can be used to save time during design verification.

What is a macro?

The term ‘macro’ refers to the substitution of a line or a few lines of text code. The directive “`define” creates a macro for substitution code. Once the macro is defined, it can be used anywhere in a compilation unit scope, wherever required.

It can be called by (`) character followed by the macro name. A macro can be defined with argument(s). Argument(s) are useful to customize the macro to use widely, like a function-task. Such macro argument(s) can be defined with default values so that if an DV engineer won’t pass any specific value, the macro substitutes the default value.

Macro Examples

Single line macro:在这里插入图片描述

As shown in the sample code above, macro “val” and “addition” are single line macros.

Multiline macro:
在这里插入图片描述

The above code is an example of a multiline macro. As shown above, for the multiline macro, the new line is preceded by a backslash “”. If the backslash is not present for a line, it is considered as the last line of the macro. The backslash will not be present in actual code where the macro is used and where the actual macro code is substituted.

Note: There should be no space or character after the backslash “” at the end of a line –otherwise the compiler shouts an error.

Possible syntaxes used to define a macro based on the usage of the below three special characters (quotations) along with the arguments, the actual code that it replaces has a different meaning. All possible macros can be formed using these three quotes:

  1. “``” (Double tick)

The “``” quotation can be used to form a signal/variable name by using given argument.
在这里插入图片描述

Example:

Macro definition:

在这里插入图片描述

Macro usage:

在这里插入图片描述

Actual code the macro replaces:

在这里插入图片描述

We can see the ARG1 = 3 is used to form the variable’s name. (I.e. m_mst_3 and mst_3_lcl)

  1. “`” “(Tick followed by a double quote)

The “`” “ quotation can be used to interpret the argument as a string. Example:

Macro Definition:

在这里插入图片描述

Macro usage:

在这里插入图片描述

Actual code the macro replaces:

在这里插入图片描述

We can see the ARG1 is replaced with string name “a” in the $display statement.

  1. \” “(Tick followed by backslash followed by a double quote)

The “ `\” “ quotation is used to replace the argument with an escape sequence.

Example:

Macro definition:

在这里插入图片描述

Macro usage:
在这里插入图片描述

Actual code the macro replaces:
在这里插入图片描述

We can see that the argument is replaced with “ ”reg_a” “ in the $display line which prints as “Reg name : “reg_a”, value : ‘hXYZ” when it is executed.

The below section shows basic examples of use case scenarios where the macros can be used based on the DV needs. The usages of such macro can be extended to many more use cases.

Macro usage for coverpoint(s)

If the DV engineer wants to cover walk0/walk1 pattern bins of multiple variables of same width, then he/she can create and use the macro for all such signals for which walk1/walk0 cover bins are required.

Example:

Macro definition:

在这里插入图片描述

Macro usage:
在这里插入图片描述

Macro usage for a covergroup

Many times in a verification project, there is a need to write the same coverage at different places, for example, same code in master and slave components. We can define common macro for covergroup, which can be used in all such components.

Example:

Macro definition:

在这里插入图片描述

Macro usage:

在这里插入图片描述

“STRING” is an argument to the “bus_cg_macro” macro. Covergroup, coverpoints and their bins are substituted considering the “STRING” argument wherever the macro is used.

Macro usage in SV Assertion

As with coverage, many times in DV projects, we have some common assertions which can be used at multiple places and different components.

For example, there is a need to check that in master and slave, a signal value keeps changing at every clock cycle if the reset is not present. We can define a macro and use it for both master and slave.

Macro definition:

在这里插入图片描述

Macro usage:

在这里插入图片描述

Macro usage in Test Case Code

In a self-checking register write/read test, after each read, the read value is checked against expected read data. Considering design complexity, we may have multiple blocks and for each block we may have corresponding register test. For each such test we can have a common macro for self-check.

Self-check macro:

在这里插入图片描述

Macro usage:

在这里插入图片描述

Macro usage in Procedural block

Macro to cover procedural block code which is common across multiple places.

Macro for procedural block:

在这里插入图片描述

Macro usage:

在这里插入图片描述

This paper covers a few example macros, but DV engineers can create and use similar macros as per their project requirement and for reusability purpose.

Conclusion

Using SV macro, with the proper syntaxes explained in this paper, the DV engineer can break up the larger complex code in smaller chunk and can reuse it at many places. A macro can be used anywhere in the compilation unit after it is defined. The engineer can form identifier names using macro through input arguments.

Also, he/she can reuse names which are being used by ordinary identifiers/names. For example, ‘signal_name’ and ‘`signal_name’ are treated differently. He/she can define the macro multiple times and at the time of macro call, the latest definition is read by the compiler and is considered for that call.

SV macro is one of the most powerful features out there and if used properly with a thorough understanding and applied wisely in a DV project, it can help to save a lot of time and can make the code more readable and efficient.

About the author

Ronak Bhatt is a Verification Engineer at eInfochips. He has an
industry experience of 1.8 years in ASIC Design Verification and has
working experience in IP level and Chip level functional verification.
He has hands-on experience in Functional and SVA-based verification.

这篇关于SV中宏的用法总结(System Verilog Macro: A Powerful Feature for Design Verification Projects)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/154185

相关文章

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二