6
Operators for Analog Blocks
This chapter describes the operators that you can use in analog blocks and explains how to use them to form expressions. For basic definitions, see
For information about precedence and short-circuiting, see
Verilog-AMS also supports additional operators for use in digital contexts. For more information, see the “Expressions” chapter, in the Verilog-XL Reference.
Overview of Operators
An expression is a construct that combines operands with operators to produce a result that is a function of the values of the operands and the semantic meaning of the operators. Any legal operand is also an expression. You can use an expression anywhere Verilog-AMS requires a value.
A constant expression is an expression whose operands are constant numbers and previously defined parameters and whose operators all come from among the unary, binary, and ternary operators described in this chapter.
All of the operators (except ==, !=, ===, and !==), functions, and statements used in continuous contexts report an error if the expressions they operate on contain x or z bits.
The operators listed below, with the single exception of the conditional operator, associate from left to right. That means that when operators have the same precedence, the one farthest to the left is evaluated first. In this example
A + B - C
the simulator does the addition before it does the subtraction.
When operators have different precedence, the operator with the highest precedence (the smallest precedence number) is evaluated first. In this example
A + B / C
the division (which has a precedence of 2) is evaluated before the addition (which has a precedence of 3). For information on precedence, see “Operator Precedence”.
You can change the order of evaluation with parentheses. If you code
(A + B) / C
the addition is evaluated before the division.
The operators divide into three groups, according to the number of operands the operator requires. The groups are the unary operators, the binary operators, and the ternary operator.
Unary Operators
The unary operators each require a single operand. The unary operators have the highest precedence of all the operators discussed in this chapter.
| Operator | Precedence | Definition | Type of Operands Allowed | Example or Further Information |
|---|---|---|---|---|
I = +13; // I = 13 |
||||
R = -13.1; // R = -13.1 |
||||
I = !(1==1); // I = 0 |
||||
|
See the Bitwise Unary Negation Operator figure. |
||||
Unary Reduction Operators
The unary reduction operators perform bitwise operations on single operands and produce a single bit result. The reduction AND, reduction OR, and reduction XOR operators first apply the following logic tables between the first and second bits of the operand to calculate a result. Then for the second and subsequent steps, these operators apply the same logic table to the previous result and the next bit of the operand, continuing until there is a single bit result.
The reduction NAND, reduction NOR, and reduction XNOR operators are calculated in the same way, except that the result is inverted.
Reduction operators can be used in the initial and always blocks of modules but are not supported in the analog block of Verilog-AMS modules.
| & | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
| | | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
| ^ | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
Binary Operators
The binary operators each require two operands.
| Operator | Precedence | Definition | Type of Operands Allowed | Example or Further Information |
|---|---|---|---|---|
R = 10.0 + 3.1; // R = 13.1 |
||||
I = 10 - 13; // I = -3 |
||||
R = 2.2 * 2.0; // R = 4.4 |
||||
I = 9 / 4; // I = 2 |
||||
I = 10 % 5; // I = 0 |
||||
I = 5 < 7; // I = 1 |
||||
I = 5 > 7; // I = 0 |
||||
I = 5.0 <= 7.5; // I = 1 |
||||
I = 5.0 >= 7; // I = 0 |
||||
|
a equal to b; evaluates to 0, 1, or |
I = 5.2 == 5.2; // I = 1 |
|||
|
a not equal to b; evaluates to 0, 1, or |
I = 5.2 != 5.2; // I = 0 |
|||
I = 1 === 1'bx; // I = 0 |
||||
I = 1 !== 1'bx; // I = 1 |
||||
I = (1==1)&&(2==2); // I = 1 |
||||
I = (1==2)||(2==2); // I = 1 |
||||
|
See the Bitwise Binary AND Operator figure. |
||||
|
See the Bitwise Binary OR Operator figure. |
||||
|
See the Bitwise Binary Exclusive OR Operator figure. |
||||
|
See the Bitwise Binary Exclusive NOR Operator figure. |
||||
|
See the Bitwise Binary Exclusive NOR Operator figure. |
||||
I = 1 << 2; // I = 4 |
||||
I = 4 >> 2; // I = 1 |
||||
@(initial_step or cross(V(vin)-1)) |
Bitwise Operators
The bitwise operators evaluate to integer values. Each operator combines a bit in one operand with the corresponding bit in the other operand to calculate a result according to these logic tables.
| & | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
| | | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
| ^ | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
| ^~ or ~^ | 0 | 1 |
|---|---|---|
| 0 | ||
| 1 |
| ~ | |
|---|---|
| 0 | |
| 1 |
Ternary Operator
There is only one ternary operator, the conditional operator. The conditional operator has the lowest precedence of all the operators listed in this chapter.
| Operator | Precedence | Definition | Type of Operands Allowed | Example or Further Information |
|---|---|---|---|---|
I= 2==3 ? 1:0; // I = 0 |
A complete conditional operator expression looks like this:
conditional_expr?true_expr:false_expr
If conditional_expr is true, the conditional operator evaluates to true_expr, otherwise to false_expr.
The conditional operator is right associative.
This operator performs the same function as the if-else construct. For example, the contribution statement
V(out) <+ V(in) > 2.5 ? 0.0 : 5.0 ;
If (V(in) > 2.5) V(out) <+ 0.0 ; else V(out) <+ 5.0 ;
Operator Precedence
The following table summarizes the precedence information for the unary, binary, and ternary operators. Operators at the top of the table have higher precedence than operators lower in the table.
| Precedence | Operators | |
|---|---|---|
![]() |
||
Expression Short-Circuiting
Sometimes the simulator can determine the value of an expression containing logical AND ( && ), logical OR ( || ), or bitwise AND ( &) without evaluating the entire expression. By taking advantage of such expressions, the simulator operates more efficiently.
integer varInt;
real varReal;
@(initial_step) begin varInt = 123;
varReal = 7.890121212e2;
end
For this example, retString receives the value "Use Integer 123, string 456 and real 789.0 to create a string 123456789.0!"
Return to top
