Hardware Description Language
3. Lexical conventions
3.2 White space
White space shall contain the characters for spaces, tabs, newlines, and formfeeds. These characters shall be ignored except when they serve to separate other lexical tokens. However, blanks and tabs shall be considered significant characters in strings (see 3.6).
3.3 Comments
The Verilog HDL has two forms to introduce comments. A one-line comment shall start with the two characters // and end with a newline. A block comment shall start with /* and end with */. Block comments shall not be nested. The one-line comment token // shall not have any special meaning in a block comment.
3.4 Operators
Operators are single-, double-, or triple-character sequences and are used in expressions. Clause 5 discusses the use of operators in expressions.
Unary operators shall appear to the left of their operand. Binary operators shall appear between their operands. A conditional operator shall have two operator characters that separate three operands.
3.5 Numbers
Constant numbers can be specified as integer constants (defined in 3.5.1) or real constants.
Syntax 3-1—Syntax for integer and real numbers number ::= (From A.8.7)
| unsigned_number [ . unsigned_number ] exp [ sign ] unsigned_number exp ::= e | E
decimal_number ::=
unsigned_number
| [ size ] decimal_base unsigned_number
| [ size ] decimal_base x_digit { _ }
| [ size ] decimal_base z_digit { _ } binary_number ::=
[ size ] binary_base binary_value octal_number ::=
[ size ] octal_base octal_value hex_number ::=
[ size ] hex_base hex_value sign ::= + | -
size ::= non_zero_unsigned_number
non_zero_unsigned_numbera ::= non_zero_decimal_digit { _ | decimal_digit}
unsigned_numbera ::= decimal_digit { _ | decimal_digit } binary_valuea ::= binary_digit { _ | binary_digit } octal_valuea ::= octal_digit { _ | octal_digit }
aEmbedded spaces are illegal.
3.5.1 Integer constants
Integer constants can be specified in decimal, hexadecimal, octal, or binary format.
There are two forms to express integer constants. The first form is 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. The second form specifies a based constant, which shall be composed of up to three tokens—an optional size constant, an apostrophe character (', ASCII 0x27) followed by a base format character, and the digits representing the value of the number. It shall be legal to macro-substitute these three tokens.
The first token, a size constant, shall specify the size of the constant in terms of its exact number of bits. It shall be specified as a nonzero unsigned decimal number. For example, the size specification for two hexadecimal digits is 8 because one hexadecimal digit requires 4 bits.
The second token, a base_format, shall consist of a case-insensitive letter specifying the base for the number, optionally preceded by the single character s (or S) to indicate a signed quantity, preceded by the apostrophe character. 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 third token, an unsigned number, shall consist of digits that are legal for the specified base format. The unsigned number token shall immediately follow the base format, optionally preceded by white space. The hexadecimal digits a to f shall be case insensitive.
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. A plus or minus operator between the base format and the number is an illegal syntax.
Negative numbers shall be represented in twos-complement form.
An x represents the unknown value in hexadecimal, octal, and binary constants. A z represents the high-impedance value. See 4.1 for a discussion of the Verilog HDL value set. An x shall set 4 bits to unknown in the hexadecimal base, 3 bits in the octal base, and 1 bit in the binary base. Similarly, a z shall set 4 bits, 3 bits, and 1 bit, respectively, to the high-impedance value.
If the size of the unsigned number is smaller than the size specified for the constant, the unsigned number shall be padded to the left with zeros. If the leftmost bit in the unsigned number is an x or a z, then an x or a z shall be used to pad to the left, respectively. If the size of the unsigned number is larger than the size specified for the constant, the unsigned number shall be truncated from the left.
The number of bits that make up an unsized number (which is a simple decimal number or a number without the size specification) shall be at least 32. Unsized unsigned constants where the high-order bit is unknown (X or x) or three-state (Z or z) shall be extended to the size of the expression containing the constant.
NOTE—In IEEE Std 1364-1995, in unsized constants where the high-order bit is unknown or three-state, the x or z was only extended to 32 bits.
The use of x and z in defining the value of a number is case insensitive.
When used in a number, the question-mark (?) character is a Verilog HDL alternative for the z character. It sets 4 bits to the high-impedance value in hexadecimal numbers, 3 bits in octal, and 1 bit in binary. The question mark can be used to enhance readability in cases where the high-impedance value is a do-not-care condition. See the discussion of casez and casex in 9.5.1. The question-mark character is also used in user-defined primitive (UDP) state tables. See Table 8-1 in 8.1.6.
In a decimal constant, the unsigned number token shall not contain any x, z, or ? digits, unless there is exactly one digit in the token, indicating that every bit in the decimal constant is x or z.
The underscore character (_) shall be legal anywhere in a number except as the first character. The underscore character is ignored. This feature can be used to break up long numbers for readability purposes.
For example:
Example 1—Unsized constant numbers
659 // is a decimal number 'h 837FF // is a hexadecimal number 'o7460 // is an octal number
4af // is illegal (hexadecimal format requires 'h) Example 2—Sized constant numbers
4'b1001 // is a 4-bit binary number 5 'D 3 // is a 5-bit decimal number
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 Example 3—Using sign with constant numbers
8 'd -6 // this is illegal syntax
-8 'd 6 // this defines the two's complement of 6, // held in 8 bits—equivalent to -(8'd 6) 4 'shf // this denotes the 4-bit number '1111', to
// be interpreted as a 2's complement number, // or '-1'. This is equivalent to -4'h 1 -4 'sd15 // this is equivalent to -(-4'd 1), or '0001' 16'sd? // the same as 16'sbz
Example 4—Automatic left padding reg [11:0] a, b, c, d;
initial begin
a = 'h x; // yields xxx b = 'h 3x; // yields 03x c = 'h z3; // yields zz3 d = 'h 0z3; // yields 0z3 endreg [84:0] e, f, g;
e = 'h5; // yields {82{1'b0},3'b101}
f = 'hx; // yields {85{1'hx}}
g = 'hz; // yields {85{1'hz}}
Example 5—Using underscore character in numbers 27_195_000
16'b0011_0101_0001_1111 32 'h 12ab_f001
Sized negative constant numbers and sized signed constant numbers are sign-extended when assigned to a reg data type, regardless of whether the reg itself is signed.
The default length of x and z is the same as the default length of an integer.
3.5.2 Real constants
The real constant numbers shall be represented as described by IEEE Std 754-1985, an IEEE standard for double-precision floating-point numbers.
Real numbers can be specified in either decimal notation (for example, 14.72) or in scientific notation (for example, 39e8, which indicates 39 multiplied by 10 to the eighth power). Real numbers expressed with a decimal point shall have at least one digit on each side of the decimal point.
For example:
1.2 0.1
2394.26331
1.2E12 (the exponent symbol can be e or E) 1.30e-2
0.1e-0 23E10 29E-2
236.123_763_e-12 (underscores are ignored)
The following are invalid forms of real numbers because they do not have at least one digit on each side of the decimal point:
.12 9.
4.E3 .2e-7 3.5.3 Conversion
Real numbers shall be converted to integers by rounding the real number to the nearest integer, rather than by truncating it. Implicit conversion shall take place when a real number is assigned to an integer. The ties shall be rounded away from zero. For example:
— The real numbers 35.7 and 35.5 both become 36 when converted to an integer and 35.2 becomes 35.
— Converting –1.5 to integer yields –2, converting 1.5 to integer yields 2.
3.6 Strings
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.
3.6.1 String variable declaration
String variables are variables of reg type (see 4.2) with width equal to the number of characters in the string multiplied by 8.
For example:
To store the 12-character string "Hello world!" requires a reg 8 * 12, or 96 bits wide.
reg[8*12:1] stringvar;
initial begin
stringvar = "Hello world!";
end
3.6.2 String manipulation
Strings can be manipulated using the Verilog HDL operators. The value being manipulated by the operator is the sequence of 8-bit ASCII values.
For example:
module string_test;
reg [8*14:1] stringvar;
initial begin
stringvar = "Hello world";
$display("%s is stored as %h", stringvar,stringvar);
stringvar = {stringvar,"!!!"};
$display("%s is stored as %h", stringvar,stringvar);
end endmodule The output is as follows:
Hello world is stored as 00000048656c6c6f20776f726c64 Hello world!!! is stored as 48656c6c6f20776f726c64212121
When a variable is larger than required to hold a string value being assigned, the value is right-justified, and the leftmost bits are .padded with zeros, as is done with nonstring values. If a string is larger than the destination string variable, the string is right-justified, and the leftmost characters are truncated.
3.6.3 Special characters in strings
Certain characters can only be used in strings when preceded by an introductory character called an escape character. Table 3-1 lists these characters in the right-hand column, with the escape sequence that represents the character in the left-hand column.