9. Compiler Directive

(1) `elseif

//`define ELSIF
`define ELSIF2
`define ELSIF3

`ifdef ELSIF

        module elsif_test;
                initial $display("Fail");
        endmodule
`elsif ELSIF2
        module elsif_test2;
                initial $display("Pass");
        endmodule
`endif


Here is "preout.v" preprocessed by Veritak preprocessor.

`line 1 "F:\regression_test\elsif_test1.v"










module elsif_test2;
initial $display("Pass");
endmodule






Here is rather complexed example.

//Jan.13.2005 

`define ELSIF1
`define ELSIF2
`define ELSIF3
//`define ELSIF4
`define ELSIF5
`define ELSIF6
//`define ELSIF7
//`define ELSIF8
//`define ELSIF9
`define ELSIF10
`define ELSIF11
`define ELSIF12
`define ELSIF13

//`define IF1
`define IF2
`define IF3
//`define IF4
`define IF5
`define IF6

module elsif_test2;
`ifdef IF1
        `ifdef IF2              
                
                initial $display("Fail");
                
                `ifdef IF3
                        
                                initial $display("Fail");
                        
                `elsif ELSIF3
                        
                                initial $display("Fail");
                        
                        `ifndef IF4
                                initial $display("Fail");
                        
                        `elsif ELSIF4
                                initial $display("Fail");
                        
                        `elsif  ELSIF5
                                initial $display("Fail");
                        
                        `endif
                        
                `endif
        `elsif ELSIF6
                        
                                initial $display("Fail");
                        
                
        `endif
`elsif ELSIF7
        
                initial $display("Fail");
        
`elsif ELSIF8
        
                initial $display("Fail");
        

`elsif  ELSIF9
        
                initial $display("OK9");
        
`else
                
                        `ifndef IF4
                                initial $display("OK10");
                        
                        `elsif ELSIF4
                                initial $display("Fail");
                        
                        `elsif  ELSIF5
                                initial $display("Fail");
                        
                        `endif

                        `ifdef IF4
                                initial $display("Fail");
                        
                        `elsif ELSIF4
                                initial $display("Fail");
                        
                        `elsif  ELSIF5
                                initial $display("OK10");
                        
                        `endif
                
`endif
endmodule


Here is a preprocessed output.

`line 1 "F:\regression_test\elsif_test2.v"























module elsif_test2;















































initial $display("OK10");
















initial $display("OK10");




endmodule




(2) `define macro
Macro can be used. However this coding style is obsolete.

`define wordsize 8
`define var_nand(dly) nand #dly
`define first_half  "string is %b" 
`define max(a,b)((a) > (b) ? (a) : (b))
module define_test;
reg [1:`wordsize] data;
wire q21,q2;
reg n10,n11;
//define a nand with variable delay
`var_nand(2) g1 (q21, n10, n11);
`var_nand(5) g2 (q22, n10, n11);
//Following is illegal example. not closed  ""
//`define first_half "start of string  
//$display (`first_half end of string");
initial begin
 n10=1'b1;
 n11=1'b1;
 #10;
 $display (`first_half,  `max(q21+n10,q22+n11));
end
endmodule

Preprocessed output is as follows.

`line 1 "F:\regression_test\define_test.v"




module define_test;
reg [1: 8] data;
wire q21,q2;
reg n10,n11;

 nand #  2 g1 (q21, n10, n11);
 nand #  5 g2 (q22, n10, n11);



initial begin
 n10=1'b1;
 n11=1'b1;
 #10;
 $display ( "string is %b",   ( (  q21+n10 ) > (  q22+n11 ) ? (  q21+n10 ) : (  q22+n11 ) ));
end
endmodule