How to emulate $ display using Verilog Macros?

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

My code looks like this, but it does not work.

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

This is what I called format_macro.

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

How can i do this?

+4
source share
3 answers

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

You can not. Verilog and SystemVerilog do not support variable macros .

The following is a workaround if your goal is to use this to format strings or output, and you don't need to type $sformat everywhere. You can define a macro for a single argument and combine this argument with $sformat . The caveat to this is that you must wrap the argument in parentheses when using a macro.

Note that () 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)) 

By the way, there is an excellent screencast here that shows how to set up UVM messaging. In it, the author shows this macro technique, as well as some other useful tips if you use UVM.

+10
source

You pass 2 arguments to your macro, "variable = %d" and variable , only the macro has only one input entered. Reading the question, this may not be the few arguments you want, but a variable number.

For a static list, either there is a macro setting for formatting the text:

 `define say(n) $display("cowsay : %s", n); initial begin `say("Moo") end =>cowsay : moo 

Or first create a string and pass it 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 
+2
source

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

This allows you to change the format of the message in the macro (add "ERROR", or perhaps a file and line number, or something else that you like), which you cannot do with the above approach, enclosing all parameters in brackets .

+1
source

Source: https://habr.com/ru/post/1439297/


All Articles