Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

ifdef Design Example

Verilog Conditional Compilation

Use ifdef to include the code only if the macro is defined in compilation args

Open in IDE
design.v
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.v
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;
  
  initial begin
    integer i;
    
    {d, rstn, clk} <= 0;

    for (i = 0 ; i < 10; i=i+1) begin
      delay = $random;
      #(delay) d <= $random;
    end
    
	#20 rstn <= 1;    

    for (i = 0 ; i < 10; i=i+1) begin
      delay = $random;
      #(delay) d <= $random;
    end
    
    #20 $finish;
  end
  
  initial begin
    $dumpvars;
    $dumpfile ("dump.vcd");
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.