{"id":149,"date":"2024-09-27T07:55:30","date_gmt":"2024-09-27T07:55:30","guid":{"rendered":"https:\/\/technicalley.com\/central\/ee\/?p=149"},"modified":"2024-09-27T07:55:30","modified_gmt":"2024-09-27T07:55:30","slug":"systemverilog-define-gotchas","status":"publish","type":"post","link":"https:\/\/technicalley.com\/central\/ee\/2024\/09\/27\/systemverilog-define-gotchas\/","title":{"rendered":"SystemVerilog: `define gotchas"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">It is very common to use macros using `define <code>in Verilog and SystemVerilog. While this may be an easy way to define some commonly used values, there are some potential pitfalls.  Here are some examples to illustrate some things to watch out for when<\/code>`defines.<\/pre>\n\n\n\n<p>Example 1: Here is an example where a macro is used to define the width of a bus.<\/p>\n\n\n<pre>`define BUS_WIDTH 16<br \/><br \/>module m ( input logic [`<code>BUS_WIDTH-1:0] i, output logic [<\/code>`BUS_WIDTH-1:0] o);<br \/>logic [`BUS_WIDTH-1:0] internal_bus;<br \/>\/\/ some code<br \/>endmodule<\/pre>\n\n\n<p>Example 2: This example uses more macros with expressions. See how the macro causes the expression to expand in an unexpected way!<\/p>\n\n\n<pre>`define BUS_WIDTH 16<br \/>`define MIN_LEVEL 5<br \/>`define MAX_LEVEL 10<br \/><code>`define RANGE<\/code> `MAX_LEVEL - `MIN_LEVEL<br \/><br \/>module m ( input logic [`<code>BUS_WIDTH-1:0] i, output logic [<\/code>`BUS_WIDTH-1:0] o);<br \/>logic [`BUS_WIDTH-1:0] internal_bus;<br \/>const int twice_range = `RANGE * 2; \/\/ Danger!!<br \/>\/\/ The RHS results in 10 - 5 * 2 = 0 (probably not intended!!)<br \/>endmodule<\/pre>\n\n\n<p>Since the macro RANGE is processed by the preprocessor and not evaluated, it is replaced  with the expression 10 &#8211; 5, because the preprocessor does simple string substitution.  This results in the RSH to be evaluated as shown in the comment.<\/p>\n\n\n\n<p>Example 3: This example shows a safe way to create expressions with macros, using parentheses.<\/p>\n\n\n<pre>`define BUS_WIDTH 16<br \/>`define MIN_LEVEL 5<br \/>`define MAX_LEVEL 10<br \/><code>`define RANGE<\/code> (`MAX_LEVEL - `MIN_LEVEL) \/\/ using parentheses<br \/><br \/>module m ( input logic [`<code>BUS_WIDTH-1:0] i, output logic [<\/code>`BUS_WIDTH-1:0] o);<br \/>logic [`BUS_WIDTH-1:0] internal_bus;<br \/>const int twice_range = `RANGE * 2; \/\/ Safe!<br \/>\/\/ The RHS results in (10 - 5) * 2 = 10 (probably what was expected)<br \/>endmodule<\/pre>\n\n\n<p>In this example, the macro is still processed by the preprocessor, and it replaces RANGE with (10 &#8211; 5) including the parentheses as defined in the macro. The parentheses makes this usage safe.<\/p>\n\n\n\n<p>So any expressions in macros must be enclosed  by parentheses to make them safe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is very common to use macros using `define in Verilog and SystemVerilog. While this may be an easy way to define some commonly used&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,10],"tags":[],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-systemverilog","category-verilog","wpcat-9-id","wpcat-10-id"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":0,"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/technicalley.com\/central\/ee\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}