Thursday, February 3, 2022

Verilog HDL - Lexical Conventions


 

HDLs provide ways to represent the digital circuits in the textual form. Verilog HDL is a HARDWARE DESCRIPTION LANGUAGE to model the digital circuits, this source code is written in a text file with the extension [ *.v ]. 


Any source code is created with the combination of characters and words which are called KEYWORDS and SYNTAX/SEMANTICS. These are globally called lexical tokens. Combination of more than one lexical tokens originate LEXICAL CONVENTIONS.



1.  LEXICAL CONVENTIONS

Lexical tokens in Verilog HDL are given below:

  • white space
  • comment
  • operator
  • number
  • string
  • identifier
  • keyword

1.1  white space or blank spaces

White spaces are used in the strings which can be incorporated using the double quotes (“). Follow the below table containing the semantics required to provide the white spaces in the string sequence:

Escape String

Character produced by escape string

\n

New line character

\t

Tab character

\\

\ character

\”

“ character

\ddd

A character specified in 1-3 octal digits (0 <= d <= 7)


If less than three characters are used, the following character shall not be an octal digit. 

Implementations may issue an error if the character represented is greater than \377.


1.2  comments

Comments are used to increase the readability of code. Comments are ignored by the Simulator. Verilog HDL has two forms to introduce comments in the source code which are given below:

1.2.1.  Single Line Comment

// This line is commented.

1.2.2.  Multiple-Line Comments

     /* This line is commented.

          This line is commented on too. */


1.3  operators

The operation which is to be performed is decided by operators. There are three kinds of operators available in Verilog HDL which are given below:

1.3.1.  Unary Operator: work on single operands.

1.3.2.  Binary Operator: work on two operands.

1.3.3.  Ternary Operator: work on three operands.

1.4  numbers

Constant numbers are represented in two ways:

  • Integer Constants

  • Real Constants


1.4.1  Integer Constants

Integer constants can be specified in decimal, hexadecimal, octal, or binary format.

There are two forms to express integer constants which are given below:

  • A.  Simple decimal number, which shall be specified as a sequence of digits 0 through 9, optionally starting with a plus or minus unary operator.
  • B.  Based constant, which shall be composed of up to three tokens.
    • an optional size constant, shall specify the size of the constant in terms of its exact number of bits.
    • an apostrophe character (', ASCII 0x27) followed by a base format character, shall consist of a case insensitive letter specifying the base for the number. Legal base specifications are d, D, h, H, o, O, b, or B, for the bases decimal, hexadecimal, octal, and binary respectively. The apostrophe character and the base format character shall not be separated by any white space.
    • the digits representing the value of the number.

It shall be legal to macro substitute these three tokens.

Negative Constant Numbers

Simple decimal numbers without the size and the base format shall be treated as signed integers,

whereas the numbers specified with the base format shall be treated as signed integers if the s designator is included or as unsigned integers if the base format only is used.

The s designator does not affect the bit pattern specified, only its interpretation.

A plus or minus operator preceding the size constant is a unary plus or minus operator.

e.g., -4 or +4

A plus or minus operator between the base format and the number is an illegal syntax.

-4'd7 // Used for performing unsigned integer math, will be stored as 2's complement of 7

-4'sd7 // Used for performing signed integer math

4'd-7 // Illegal

Unsized Constant Numbers

Numbers specified without a base format are considered as decimal numbers by default. Numbers that are written without size are simulator/machine specific but must be at least 32 bits.

659 // is a decimal number

'h 837FF // is a hexadecimal number

'o7460 // is an octal number

4af // is illegal (hexadecimal format requires 'h)

Sized Constant Numbers

4'b1001 // is a 4-bit binary number

'D 3 // is a 5-bit decimal number (Simulator/Machine Specific)

X or Z Values

3'b01x // is a 3-bit number with the least

// significant bit unknown

12'hx // is a 12-bit unknown number

16'hz // is a 16-bit high-impedance number

16'sd? // the same as 16'sbz

The default length of x and z is the same as the default length of an integer.

1.4.2  Real Constants

Real numbers are specified as given below:

  • decimal notation (e.g., 14.72)

  • scientific notation (e.g., 39e8)

  • scaled notation (e.g., 24.7K)

Real numbers expressed with a decimal point shall have at least one digit on each side of the decimal point.

Underscore Character

The underscore character is legal anywhere in an integer constant.

The underscore character is legal anywhere in a real constant except as the first character of the constant or the first character after the decimal point. The underscore character is ignored by the simulator.

E,g., 16’b1011_1000_1111_1010 // Underscore increase the readability of a number constant


1.5  string

A string is a sequence of characters enclosed by double quotes ( " ) and contained on a single line. Strings used as operands in expressions and assignments shall be treated as unsigned integer constants represented by a sequence of 8-bit ASCII values, with one 8-bit ASCII value representing one character.


1.6  Identifier

Identifier shall be used to give an object a unique name so it can be referenced. An identifier shall either be a simple identifier or an escaped identifier.

Simple Identifier

It shall be any sequence of letters,digits, dollar signs ($), and the underscore characters (_).

The first character of an identifier shall not be a digit or $; it can be a letter or an underscore. Identifiers shall be case sensitive.

e.g.,

    shiftreg_a

    busa_index

    error_condition

    merge_ab

    _bus3

    n$657

Implementations may set a limit on the maximum length of identifiers, but they shall be at least 1024 characters. If an identifier exceeds the implementation-specified length limit, an error shall be reported.

Escaped Identifiers

Escaped identifiers shall start with the backslash character (\) and end with white space (space, tab, newline, or formfeed).

They provide a means of including any of the printable ASCII characters in an identifier (the decimal values 33 through 126 or 21 through 7E in hexadecimal).

Neither the leading backslash character nor the terminating white space is considered to be part of the identifier. Therefore, an escaped identifier \cpu3 is treated the same as a non-escaped identifier cpu3.

e.g.,

    \busa+index

    \-clock

    \***error-condition***

    \net1/\net2

    \{a,b}

    \a*(b+c)



1.7  Keyword

Keywords are special identifiers reserved to define the language constructs. Keywords are in lowercase.