Verilog supports a few compiler directives that essentially direct the compiler to treat the code in a certain way. For example, a portion of the code may represent an implementation of a certain feature and there should be some way to not include the code in the design if the feature is not used.

This can be solved with conditional compilation where the designer can wrap the code within compiler directives which tell the compiler to either include or exclude the code for compilation when the given named flag is set.

Syntax

Conditional compilation can be achieved with the Verilog `ifdef and `ifndef compiler directives, together with `elsif, `else and `endif. These directives can appear anywhere in the design and can be nested one inside the other.

The `ifdef directive tells the compiler to include the piece of code until the next `elsif, `else or `endif if the given macro called FLAG is defined using a `define directive.


// Style #1: Only single `ifdef
`ifdef <FLAG>
	// Statements
`endif

// Style #2: `ifdef with `else part
`ifdef <FLAG>
	// Statements
`else
	// Statements
`endif

// Style #3: `ifdef with additional ifdefs
`ifdef <FLAG1>
	// Statements
`elsif <FLAG2>
	// Statements
`elsif <FLAG3>
	// Statements
`else
	// Statements
`endif

The `ifndef directive tells the compiler to include the piece of code until the next `elsif, `else or `endif if the given macro called FLAG is not defined using a `define directive.

Design Example with `ifdef


module my_design (input clk, d, 
`ifdef INCLUDE_RSTN
                  input rstn,
`endif                  
                  output reg q);
  
  always @ (posedge clk) begin
`ifdef INCLUDE_RSTN
    if (!rstn) begin
      q <= 0;
    end else 
`endif
    begin
      q <= d;
    end
  end
endmodule

Testbench


module tb;
  reg clk, d, rstn;
  wire q;
  reg [3:0] delay;
  
  my_design u0 ( .clk(clk), .d(d),
`ifdef INCLUDE_RSTN
                .rstn(rstn),
`endif
                .q(q));
  
  always #10 clk = ~clk;
  
  integer i;

  initial begin
    
    {d, rstn, clk} <= 0;
    
	#20 rstn <= 1;    
    for (i = 0 ; i < 20; i=i+1) begin
      delay = $random;
      #(delay) d <= $random;
    end
    
    #20 $finish;
  end
endmodule

Note that by default, rstn will not be included during compilation of the design and hence it will not appear in the portlist. A macro takes effect only from the point where the compiler reads it. So if a macro called INCLUDE_RSTN is either defined with `define before the design is compiled (earlier in the same file, or in a file compiled before it) or passed through the command line to the compiler (for example +define+INCLUDE_RSTN), rstn will be included in compilation and the design will have it.

Experiment by adding and removing +define+INCLUDE_RSTN from 'Compile & Run Options' on the left pane to know the difference.

Verilog `ifdef `elsif Example

The following example has two display statements in two branches of one `ifdef ... `elsif chain, which does not have a default `else part to it. So this means that by default nothing will be displayed. If only the macro MACRO1 or only MACRO2 is defined, the display message in that branch is included and will be displayed during simulation. If both are defined, only the first matching branch (the one for MACRO1) is compiled.


module tb;
  initial begin

`ifdef MACRO1
    $display ("This is MACRO1");
    
`elsif MACRO2
    $display ("This is MACRO2");
    
`endif
  end
endmodule
Output
# With no macros defined
ncsim> run
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+MACRO1
ncsim> run
This is MACRO1
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+MACRO2
ncsim> run
This is MACRO2
ncsim: *W,RNQUIE: Simulation is complete.

Verilog `ifndef `elsif Example

The same code can be written with `ifndef in place of the first `ifdef. This inverts only the first test: that branch is compiled when MACRO1 is not defined, and the `elsif MACRO2 branch is reached only when MACRO1 is defined.


module tb;
  initial begin

`ifndef MACRO1
    $display ("This is MACRO1");
    
`elsif MACRO2
    $display ("This is MACRO2");
    
`endif
  end
endmodule
Output
# With no macros defined
ncsim> run
This is MACRO1
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+MACRO1
ncsim> run
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+MACRO2
ncsim> run
This is MACRO1
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+MACRO1 +define+MACRO2
ncsim> run
This is MACRO2
ncsim: *W,RNQUIE: Simulation is complete.

Verilog Nested `ifdef Example

`ifdef and its flavors can be nested one inside the other to create complex ways of code inclusion and exclusion with defined macros.


module tb;
  initial begin
    `ifdef FLAG
    	$display ("FLAG is defined");
    	`ifdef NEST1_A
    		$display ("FLAG and NEST1_A are defined");
    		`ifdef NEST2
    			$display ("FLAG, NEST1_A and NEST2 are defined");
    		`endif
    	`elsif NEST1_B
    		$display ("FLAG and NEST1_B are defined");
    		`ifndef WHITE
    			$display ("FLAG and NEST1_B are defined, but WHITE is not");
    		`else
    			$display ("FLAG, NEST1_B and WHITE are defined");
    		`endif
    	`else
    		$display ("Only FLAG is defined");
    	`endif
    `else
    	$display ("FLAG is not defined");
    `endif
  end
endmodule

When running with Icarus Verilog, use -D<MACRO_NAME> in compilation args to apply the macro.

Output
# Without defining any macro
ncsim> run
FLAG is not defined
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+FLAG +define+NEST1_B
ncsim> run
FLAG is defined
FLAG and NEST1_B are defined
FLAG and NEST1_B are defined, but WHITE is not
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+FLAG +define+NEST1_B +define+WHITE
ncsim> run
FLAG is defined
FLAG and NEST1_B are defined
FLAG, NEST1_B and WHITE are defined
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+FLAG
ncsim> run
FLAG is defined
Only FLAG is defined
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+WHITE
ncsim> run
FLAG is not defined
ncsim: *W,RNQUIE: Simulation is complete.

# With +define+NEST1_A
ncsim> run
FLAG is not defined
ncsim: *W,RNQUIE: Simulation is complete.

Note that as long as the parent macro is not defined, definition of any other nested macro within it does not get compiled. For example, NEST1_A or WHITE macro definitions without FLAG does not make the compiler pick up the nested code.

Practice Exercise

Challenge: Which Branch Is Compiled?

Predict what this code prints. Then predict the output when SLOW is also defined on the command line, and when the `define FAST line is deleted and nothing is defined on the command line.


`define FAST

module tb;
  initial begin
`ifdef SLOW
    $display("slow");
`elsif FAST
    $display("fast");
`else
    $display("default");
`endif
`ifndef DEBUG
    $display("no debug");
`endif
  end
endmodule
Hint

Only the first branch whose macro is defined is compiled. `ifndef is compiled when the macro is not defined.

Solution
CaseOutput
As writtenfast, no debug
SLOW also defined (+define+SLOW, or -DSLOW in Icarus Verilog)slow, no debug
`define FAST deleteddefault, no debug

When SLOW is defined, the `elsif FAST branch is skipped even though FAST is defined too, because the first matching branch wins. DEBUG is never defined, so "no debug" is printed every time.

Hands-On Drills

Write and run these in ChipVerify Lab. Each drill is graded, and when your code fails, the grader explains which mistake it found.

Debug Prints with `ifdef

Add debug prints that compile only when asked for.

Easy, 12 minPRO
Did you find what you were looking for?

Quiz

Test Your Knowledge

1 of 5 Score: 0 / 5