Check the value of `define macro

Hi ,

I want to define some code based on the macro value of `define

Let us suppose that `define TEST 1

I want to enable some code if `TEST define value is ‘1’

Regards,
Phani

In reply to kpkrishna:

One can use ifdef... endif to check whether the macro is defined or not. Moreover, one can use an optional generate block to test the value of macro/parameters.


module top();
  `ifdef MY_MACRO
    if(`MY_MACRO == 1) begin                   // If MY_MACRO is defined and value is 1
      initial $display("MY_MACRO equal to 1");
    end else begin                             // If MY_MACRO is defined and value is not 1
      initial $display("MY_MACRO not equal to 1");
    end
  `else 
      initial $display("MY_MACRO not defined");  // If MY_MACRO is not defined 
  `endif
endmodule

Here if MY_MACRO is defined and value is 1, then the first initial block will be executed.

Refer to this section for more information.

In reply to sharvil111:

Sometimes, you can check the macro value in procedural code. There’s no performance penalty because the way compilers propagate constants and eliminate dead code.

module top();
  `ifdef MY_MACRO
    initial if(`MY_MACRO == 1) begin                   // If MY_MACRO is defined and value is 1
       $display("MY_MACRO equal to 1");
           end else begin                              // If MY_MACRO is defined and value is not 1
       $display("MY_MACRO not equal to 1");
    end
  `else 
      initial $display("MY_MACRO not defined");  // If MY_MACRO is not defined 
  `endif
endmodule

In reply to sharvil111:

I don’t want the else block to get compiled. Will it get compiled in this case

In reply to kpkrishna:

And i want to use in class

In reply to kpkrishna:

yes Macro can be used in class too. And for your formal query dave_59 already answered it will eliminate else code if value is set to 1

In reply to kpkrishna:

You can remove the else/else conditions as per your requirement. As a side note, else is a compiler directive so it will not compile if MY_MACRO is defined.

In reply to kpkrishna:

`define TEST 5
class  A;
  rand int x;
  function void post_randomize();
     if (`TEST  == 32'd45)
     begin
      `define TEST1
       $display("TEST 1 DEFINE");
     end
     else
     begin
      `define TEST0
      ;
     end
     `ifdef TEST1
        $display("TEST 1 DEFINED ");
     `else
       KKKKKKKKKKKKKKKK
     `endif
  endfunction
endclass
module test;
 A a = new();
 initial
 begin
  a.randomize();
 end
endmodule

Can you help me in finding the issue here. Since TEST define value is 5 , i should get compilation error. But i am not getting

In reply to kpkrishna:

Macros are processed in the preprocessor, and are constant at runtime. Your ‘if (`TEST == 32’d45)’ statement is not evaluated by the preprocessor. Which means both TEST1 and TEST0 are being defined.

To the preprocessor, your code simply looks like:

`define TEST 5
// some text
     `TEST
// some text
      `define TEST1
// some text
      `define TEST0
// some text
     `ifdef TEST1
// some text
     `else
// some text
     `endif
// some text