How to emulate $display using Verilog Macros?

雨燕双飞 提交于 2019-12-06 04:14:40

问题


I want to create a macro with multiple parameters just like $display.

My code looks like this but it doesn't work.

               `define format_macro(A) \
                      $write("%s", $sformatf(A)); \

This is how I called format_macro.

               `format_macro("variable = %d", variable)

How can I do this?


回答1:


I want to create a macro with multiple parameters just like $display.

You can't. Verilog and SystemVerilog do not support variadic macros.

Here is a workaround if your goal is to use this for formatting strings or output, and you want to avoid having to type $sformat all over the place. You can define the macro to have a single argument, and combine that argument with $sformat. The caveat with doing this is that you must wrap the argument in parentheses when using the macro.

Note the ()'s for $sformatf are not part of the macro:

`define format_macro(A) \
        $write("%s", $sformatf A ); \

Then you can do this:

  `format_macro(("a = %d", a))
  `format_macro(("a = %d, b = %d", a, b))

BTW, there is an excellent screencast here which shows how to customize messaging in UVM. In it, the author shows this macro technique, along with some other nice tips if you're using UVM.




回答2:


You are passing 2 arguments to your macro, "variable = %d" and variable, the macro only has 1 input defined. Reading the question it might not be multiple arguments that you want but a variable number.

For a static list either have macro setup to format the text:

`define say(n) $display("cowsay : %s", n);

initial begin
  `say("Moo")
end
=>cowsay : moo

Or create the string first and pass as a single argument.

`define say(n) $display("%s", n);

string msg;

initial begin 
  $sformat(msg, "variable is : %d", 3);
  `say(msg)
end
=>variable is :           3



回答3:


SystemVerilog now supports optional macro parameters, which allows you to create a clever kludge like this: http://ionipti.blogspot.com/2012/08/systemverilog-variable-argument-display.html

This allows you to modify the message format in the macro (prepend "ERROR", or maybe file and line number, or whatever else you like), which you can't do with the above approach of wrapping all parameters into parenthesis.



来源:https://stackoverflow.com/questions/12852771/how-to-emulate-display-using-verilog-macros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!