Lexical Conventions
Verilog HDL uses lexical conventions similar to those in the C programming language. It is a case-sensitive language with all keywords in lowercase.
Comments
Comments in Verilog enhance code readability and documentation. There are two types of comments:
Single-Line Comments:
Start with //
and continue to the end of the line.
Example:
// This is a single-line comment
Multiple-Line Comments:
Start with /*
and end with */
. Single-line comments can be embedded within multiple-line comments. However, multiple-line comments cannot be nested.
Example:
This is a multiple-line comment.
It can span multiple lines.
// You can use single-line comments within multiple-line comments.
*/
Attempting to nest multiple-line comments will result in an error:
/*
This will cause an error
/* Nested comment */
*/
Whitespace
Whitespace characters (spaces \b
, tabs \t
, and newlines \n
) are ignored by Verilog except when they separate tokens. Whitespace is not ignored within strings.
Operators
Operators in Verilog are categorized as follows:
Unary Operators: Operate on a single operand.
Example:a = ~b; // ~ is a unary operator
Binary Operators: Operate between two operands.
Example:a = b && c; // && is a binary operator
Ternary Operators: Use two operators to operate on three operands.
Example:a = b ? c : d; // ?: is a ternary operator
Data Types
In Verilog, data types represent different logic states:
1'b0
: Logic 0 (false or ground)1'b1
: Logic 1 (true or power)1'bX
: Unknown logic state1'bZ
: High impedance (floating)
Number Specification
Verilog supports two types of number specifications: sized and unsized.
Sized Numbers:
Represented as <size>'<base format> <number>
.
<size>
specifies the number of bits (decimal).<base format>
specifies the number's base: decimal ('d
or'D
), hexadecimal ('h
or'H
), binary ('b
or'B
), and octal ('o
or'O
).<number>
is the actual number.
Examples:
4'b1111 // 4-bit binary number (15 in decimal)
12'habc // 12-bit hexadecimal number (2748 in decimal)
16'd255 // 16-bit decimal number
12'h13x // 12-bit hexadecimal number with 4 least significant bits unknown
Unsized Numbers:
By default, these are decimal and 32 bits in size unless otherwise specified.
Examples:
23456 // 32-bit decimal number
'hc3 // 32-bit hexadecimal number
'o21 // 32-bit octal number
'bz // 32-bit high impedance number
Negative Numbers:
Indicate negative values by prefixing the size with a minus sign.
Examples:
-6'd3 // 8-bit negative number stored as 2's complement of 3
3'd-1 // Illegal specification
Underscore Characters:
Used to improve readability by separating digits in numbers.
Example:
8'b1000_1010 // Binary number with underscores
Question Mark (?
):
Represents an unknown state or high impedance, alternative to Z
.
Example:
2'b?? // Equivalent to 2'bzz
0 Comments