在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 8940|回复: 14

[求助] systemverilog如何实现可变参数宏

[复制链接]
发表于 2013-12-10 19:14:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
想实现的功能有点BT,希望有牛人能够帮忙解答。
假设我想实现如下的功能.
`define DIS(str) $display(str)

然后,`DIS("hello,world,%d",'h8);

编译会报错。我想把 "hello,world,%d",'h8 当做统一的参数传进去,单是编译器会检测到“,”认为是两个参数。


大家不要说DIS的宏给两个参数,因为我可能需要打印这样的信息,"hello,world,%d,%d",'h8,'h19


其实我想实现的是类似于C语言中可变参数宏的功能,求大牛指导。
发表于 2013-12-10 22:56:50 | 显示全部楼层
你只给了display一个参数, dispay会把参数当成一个字符串打印出来,`DIS("hello,world,%d",'h8) 这样会把括号里的当成一个字符串来处理。最后打印出来的就是"hello,world,%d",'h8。
    再则`DIS("hello,world,%d",'h8)这么写是给了两个参数的。应写成`DIS(“\"hello,world,%%d\",'h8”) 是给一个字符串参数。
发表于 2013-12-11 15:09:16 | 显示全部楼层
应该转义
发表于 2013-12-11 15:40:46 | 显示全部楼层
2# 的方法好像不可以 编译都有问题
    是不是根本实现不了?

systemverilog lrm 3.1a 关于define就下面几行说明:

25.2 ‘define macros
In Verilog, the ‘definemacro text can include a backslash ( \) at the end of a line to show continuation on
the next line.
In SystemVerilog, the macro text can also include `", `\`"and ``.
An `"overrides the usual lexical meaning of", and indicates that the expansion should include an actual quotation mark. This allows string literals to be constructed from macro arguments.
A `\`"indicates that the expansion should include the escape sequence \", e.g.
`define msg(x,y) `"x: `\`"y`\`"`"
This expands:
$display(`msg(left side,right side));
to:
$display("left side: \"right side\"");
A ``delimits lexical tokens without introducing white space, allowing identifiers to be constructed from arguments, e.g.
`define foo(f) f``_suffix
This expands:
‘foo(bar)
to:
bar_suffix
Accellera
SystemVerilog 3.1a Extensions to Verilog-2001
344 Copyright 2004 Accellera. All rights reserved .
The ‘includedirective can be followed by a macro, instead of a literal string:
‘define home(filename) ‘"/home/foo/filename‘"
‘include ‘home(myfile)
发表于 2013-12-11 20:00:04 | 显示全部楼层
我只用vcs,以下结论是在vcs下得出的,有条件的同学可以用其他编译器试试。

  sv的手册中很多例子是有问题的,不能只看手册中写的, 要动手测测。这个问题有可能是编译器的问题。

   你说的`define msg(x,y) `"x: `\`"y`\`"`"   这个写法没有问题,但是不加重音号(`)也是没问题的,这个不知道你在vcs下测过吗。

    其实有一种情况是必须加重音号的,就是宏的引用在双引号内部。下面有个例子。还有楼主要注意的一点是define的字符串替换时在编译阶段,不是在仿真阶段,一定要保证编译阶段字符串替换的正确性。
图像 4.png
图像 4.png
 楼主| 发表于 2013-12-11 22:52:51 | 显示全部楼层
楼上的同学,你觉得我想打印的效果是,
"hello,world,%d",'h8
吗????

查了很多资料,试了很多方法,都木有实现。
估计sv根本就没办法实现这样的功能吧。
发表于 2013-12-12 11:31:34 | 显示全部楼层
define DIS $display 就可以实现一楼的操作 你想宏参数是个数组 后面替换部分就要实现数组引用
发表于 2013-12-12 14:52:33 | 显示全部楼层
既然是宏处理的话,自己用perl写个宏预处理程序。在编译前先处理一下,不必强求用SV实现。
发表于 2013-12-21 11:12:03 | 显示全部楼层
楼主是要实现这样的功能吗?

`define DIS "hello,world,%d,%h",'h8,5'd15
$display(`DIS);

//打印信息
# hello,world,         8,0f
发表于 2013-12-21 11:14:09 | 显示全部楼层
回复 6# eesub


   sv很强大的,不是不能实现,而是你不会而已
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-7 08:28 , Processed in 0.031164 second(s), 9 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表