Module 4: Introduction to Verilog HDL · 4.2 Verilog Language Basics

Compiler Directives

Typographical errors in signal names cause severe simulation bugs. By default, Verilog creates an implicit 1-bit wire when it encounters an undeclared identifier. The design simulates without compiler warnings, but the implicit net remains undriven. Compiler directives eliminate this defect category.

Definition of Compiler Directives

A compiler directive begins with a backtick character (`). The preprocessor evaluates directives before HDL syntax compilation. Directives do not infer physical logic gates. Directives configure simulator time scales, perform textual substitutions, and control conditional compilation.

Directives maintain global scope across module boundaries within a compilation unit. Do not confuse the backtick directive symbol (`) with the apostrophe literal separator ('):

`timescale    // Compiler directive
4'b1010       // Sized numeric literal

The timescale Directive

The `timescale directive defines the simulation time unit and measurement precision:

`timescale <time_unit> / <time_precision>
  • <time_unit> defines the temporal duration of delay statements such as #1.
  • <time_precision> defines the minimum rounding resolution of the simulation engine.
`timescale 1ns / 1ps    // #1 represents 1 ns, rounded to 1 ps resolution
`timescale 1us / 1ns    // Low-frequency board-level interfaces
`timescale 1ps / 1fs    // High-speed multi-gigabit transceivers

Place the timescale directive at the top of the file before declaring modules:

`timescale 1ns / 1ps
`default_nettype none

module clk_gen (
    output reg clk
);
    initial clk = 1'b0;
    always #5 clk = ~clk;   // Inverts every 5 ns (100 MHz clock period in testbenches)
endmodule

Logic synthesis tools ignore delay statements (#) and timescale directives. Restrict delay specifications to simulation testbenches.

The define and ifdef Directives

The `define directive declares a global text substitution macro:

`define DATA_WIDTH 8

wire [`DATA_WIDTH-1:0] bus;    // Preprocessor evaluates expression as: wire [8-1:0] bus

`undef DATA_WIDTH              // Removes macro definition

Use module parameters for hardware values instead of untyped global macros. Macros are ideal for conditional compilation:

`timescale 1ns / 1ps
`default_nettype none

module uart_tx #(
    parameter CLK_FREQ  = 50_000_000,
    parameter BAUD_RATE = 115_200
)(
    input  wire       clk,
    input  wire       rst_n,
    input  wire [7:0] tx_data,
    input  wire       tx_valid,
    output wire       tx_serial,
    output wire       tx_ready
);

    localparam BAUD_DIV = CLK_FREQ / BAUD_RATE;

    // Transmit logic implemented here

`ifdef SIMULATION
    // Verification monitor active only during simulation runs
    always @(posedge clk) begin
        if (tx_valid && tx_ready)
            $display("[UART_TX t=%0t] Sending byte: 8'h%02h", $time, tx_data);
    end
`endif

endmodule

Define simulation flags in the simulator command line invocation (such as iverilog -DSIMULATION). This practice keeps verification code out of the synthesized gate netlist. The preprocessor supports `ifdef, `ifndef, `elsif, `else, and `endif blocks.

The include Directive

The `include directive inserts the text of an external file into the source code:

`include "defines.vh"

The .vh file extension identifies Verilog header files. Add preprocessor include guards to prevent multiple file insertions:

`ifndef PROJECT_DEFINES_VH
`define PROJECT_DEFINES_VH

`define DATA_BUS_WIDTH  32
`define ADDR_BUS_WIDTH  12

`endif

The default_nettype Directive

By default, Verilog creates an implicit 1-bit wire when it encounters an undeclared identifier:

module broken_adder (
    input  wire [7:0] input_a,
    input  wire [7:0] input_b,
    output wire [7:0] result
);
    // Typo: compiler creates implicit 1-bit undriven wire named imput_a
    assign result = imput_a + input_b;
endmodule

The design compiles and produces invalid outputs without syntax warnings. Add `default_nettype none to disable implicit net creation:

`timescale 1ns / 1ps
`default_nettype none

module correct_adder (
    input  wire [7:0] input_a,
    input  wire [7:0] input_b,
    output wire [7:0] result
);
    // Compiler generates error: imput_a is not declared
    assign result = imput_a + input_b;
endmodule

`default_nettype wire

When `default_nettype none is active, the compiler stops and reports the undeclared identifier. Restore `default_nettype wire at the bottom of the file to preserve compatibility with third-party IP libraries.

The celldefine Directive

The directives `celldefine and `endcelldefine mark modules as ASIC library cells. Foundries use these directives in gate-level standard cell libraries. User RTL designs rarely require cell directives.

Use this standard file template for all Verilog source modules:

// =============================================================================
// Module:      <module_name>
// Description: <functional description>
// =============================================================================

`timescale 1ns / 1ps
`default_nettype none

module <module_name> #(
    // Parameter declarations
)(
    // Port declarations
);

    // Module internal logic

endmodule

`default_nettype wire

Compiler Directive Scope

Compiler directives apply from the point of declaration to the end of the compilation unit (Figure 1). Directives cross module and file boundaries.

Directive scope
Figure 1: Scope of preprocessor compiler directives across compilation units

Use include guards in header files and restore default settings to prevent unexpected side effects across files.

Practice Exercise

Identify three syntax and configuration issues in this module and rewrite it correctly:

module sensor_read (
    input  wire clk,
    input  wire rst_n,
    input  wire [11:0] raw_adc,
    output reg  [11:0] filtered
);
    always @(posedge clk) begin
        if (!rst_n)
            filtered <= 0;
        else
            filtered <= raw_adc;
    end
endmodule
Solution

Issues identified:

  1. Missing `timescale directive: simulation delays lack temporal reference.
  2. Missing `default_nettype none: typographical errors will create implicit wires.
  3. Unsized literal 0: unsized constants default to 32 bits instead of the 12-bit register width.

Corrected implementation:

`timescale 1ns / 1ps
`default_nettype none

module sensor_read (
    input  wire        clk,
    input  wire        rst_n,      // Synchronous active-low reset
    input  wire [11:0] raw_adc,
    output reg  [11:0] filtered
);

    always @(posedge clk) begin
        if (!rst_n)
            filtered <= 12'd0;
        else
            filtered <= raw_adc;
    end

endmodule

`default_nettype wire

The final article in this section discusses design conventions, reset strategies, and readable source formatting.

← Number Literals Style and Conventions →