Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Verilog Syntax Basics: Tokens, Comments and Naming

Learn the lexical rules of Verilog: whitespace, comments, identifiers, keywords, case sensitivity, compiler directives, system tasks and the style habits that keep code clean.

Before you build bigger circuits, you need to be comfortable with how Verilog source text is organized. The rules are close to C, with a few important differences. This lesson covers the building blocks you will see in every file: whitespace, comments, identifiers, keywords, compiler directives, system tasks and a handful of style rules.

Whitespace and structure

Verilog ignores whitespace (spaces, tabs, newlines) between tokens. Statements end with a semicolon, and groups of statements use begin ... end (not curly braces). Modules are wrapped in module ... endmodule.

VERILOG
module demo;
    wire w;
    assign w = 1'b1;
endmodule

Braces { } do exist, but they mean concatenation, not a block (see vectors).

Comments

VERILOG
// A single-line comment

/* A multi-line
   comment */

assign y = a & b;  // trailing comment

Block comments do not nest. Write comments that explain why, not what the next line obviously does.

Identifiers

An identifier names a module, wire, register or instance.

  • It starts with a letter or underscore, then may contain letters, digits, underscores and $.
  • Identifiers are case sensitive: Count, count and COUNT are three different names. Keywords are lowercase and also case sensitive.
  • An escaped identifier starts with a backslash and ends at whitespace (\bus[3] ). It is mostly produced by tools.

Valid names: clk, data_in, _tmp, bus8. Invalid: 8bus (starts with a digit), my-signal (a hyphen is the minus operator), and wire (a keyword).

Common conventions that make hardware code readable:

Convention Example
Lowercase with underscores read_data
Active-low suffix _n rst_n, cs_n
Clock named clk clk, clk_sys
Parameters in UPPERCASE DATA_WIDTH
Registered signal suffix _q or _r count_q
Next-value suffix _d or _next count_d

Keywords you will meet first

module, endmodule, input, output, inout, wire, reg, assign, always, initial, begin, end, if, else, case, endcase, for, parameter, localparam, posedge, negedge, integer. SystemVerilog adds logic, always_ff, always_comb, typedef, enum, struct and more.

Number and string tokens

Numbers look like 4'b1010 (sized binary), 8'hFF (sized hex) or plain 42. They get their own lesson on numbers and literals. Strings use double quotes and appear in $display("count = %d", count);.

Compiler directives

A backtick introduces a compiler directive, processed before the code is compiled. Directives are not statements.

VERILOG
`timescale 1ns/1ps          // time unit / precision for delays
`define WIDTH 8             // text macro
`include "defs.vh"          // paste another file here
`ifdef SIMULATION
    // code only for simulation
`endif

wire [`WIDTH-1:0] bus;      // use a macro with a backtick

Notes:

  • A macro is text substitution, with no type checking. Prefer parameter for design constants (see modules and ports).
  • Without `timescale, delays like #5 have a tool-dependent unit; always set it in testbenches.
  • Do not put a semicolon after `define WIDTH 8 unless you want it inside the replacement text.

System tasks and functions

Names starting with $ are built-in simulation helpers. Most are not synthesizable.

Task Use
$display(...) Print a line immediately
$monitor(...) Print whenever an argument changes
$time Current simulation time
$finish End the simulation
$random, $urandom Pseudo-random numbers
$dumpfile, $dumpvars Record waveforms to a file
$clog2(n) Ceiling of log2, useful in synthesizable code for widths

Format codes in strings: %b binary, %d decimal, %h hex, %t time, %s string, %0d decimal without padding.

VERILOG
initial begin
    $dumpfile("wave.vcd");
    $dumpvars(0, top_tb);
    $display("time=%0t a=%b", $time, a);
end

Stand-in: formatting output like $display

The C program below is not Verilog. It shows what the common $display formats print for a value, using C's printf and a small helper that prints binary, because Verilog's %b has no C equivalent.

C Stand-in: $display-style number formats (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

$clog2 is handy when sizing an address bus: 1000 memory words need 10 address bits.

Case, style and file habits

  • One module per file, with the file named after the module (half_adder.v).
  • Verilog files commonly use .v; SystemVerilog uses .sv.
  • Indent with spaces, align port lists, and give every instance a name.
  • Declare every signal explicitly. If you mistype a name in a continuous assignment, Verilog may silently create a one-bit implicit wire. Adding `default_nettype none at the top of a file turns that into an error, which is good practice.
VERILOG
`default_nettype none
module clean (input wire a, output wire y);
    assign y = ~a;
endmodule
`default_nettype wire

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Are Verilog identifiers case sensitive?

    Verilog is case sensitive for all identifiers and for keywords.
  2. 2. What do curly braces { } mean in Verilog?

    Blocks use begin and end; braces concatenate bits.
  3. 3. What does a backtick like `define start?

    Backtick directives such as timescale, define and `include are handled before compilation.
  4. 4. What does `default_nettype none help prevent?

    With it, an undeclared name is an error instead of an implicit wire.

Summary

  • Verilog ignores whitespace, uses begin ... end for blocks and ; to end statements.
  • Identifiers and keywords are case sensitive; follow naming conventions such as _n for active-low.
  • Backtick directives ( `timescale, `define, `include, `ifdef) and $ system tasks are special.
  • Use $display and $finish in testbenches only, and $clog2 to size widths.
  • Add `default_nettype none to catch typos.

Next: Modules and Ports