Product Documentation
Cadence Verilog-AMS Language Reference
Product Version 22.09, April 2022

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.

Unary Operators

Operator Precedence Definition Type of Operands Allowed Example or Further Information

+

1

Unary plus

Integer, real

I = +13;       // I = 13
I = +(-13);   // I = -13

-

1

Unary minus

Integer, real

R = -13.1;     // R = -13.1
I = -(4-5);   // I = 1

!

1

Logical negation

Integer, real

I = !(1==1);   // I = 0
I = !(1==2); // I = 1
I = !13.2; // I = 0
/*Result is zero for a non-
zero operand*/

~

1

Bitwise unary negation

Integer

See the Bitwise Unary Negation Operator figure.

&

1

Unary reduction AND

integer

See “Unary Reduction Operators.”

~&

1

Unary reduction NAND

integer

See “Unary Reduction Operators.”

|

1

Unary reduction OR

integer

See “Unary Reduction Operators.”

~|

1

Unary reduction NOR

integer

See “Unary Reduction Operators.”

^

1

Unary reduction exclusive OR

integer

See “Unary Reduction Operators.”

^~ or ~^

1

Unary reduction exclusive NOR

integer

See “Unary Reduction Operators.”

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.

Unary Reduction AND Operator

& 0 1
0

0

0

1

0

1

Unary Reduction OR Operator

| 0 1
0

0

1

1

1

1

Unary Reduction Exclusive OR Operator

^ 0 1
0

0

1

1

1

0

Binary Operators

The binary operators each require two operands.

Binary Operators

Operator Precedence Definition Type of Operands Allowed Example or Further Information

+

3

a plus b

Integer, real

R = 10.0 + 3.1; // R = 13.1

-

3

a minus b

Integer, real

I = 10 - 13;    // I = -3

*

2

a multiplied by b

Integer, real

R = 2.2 * 2.0;  // R = 4.4

/

2

a divided by b

Integer, real

I = 9 / 4;      // I = 2
R = 9.0 / 4;   // R = 2.25

%

2

a modulo b

Integer, real

I = 10 % 5;     // I = 0
I = -12 % 5; // I = -2
R = 10 % 3.75 // R = 2.5
/*The result takes sign of the
first operand.*/

<

5

a less than b; evaluates to 0 or 1

Integer, real

I = 5 < 7;      // I = 1
I = 7 < 5;   // I = 0

>

5

a greater than b; evaluates to 0 or 1

Integer, real

I = 5 > 7;      // I = 0
I = 7 > 5;   // I = 1

<=

5

a less than or equal to b; evaluates to 0 or 1

Integer, real

I = 5.0 <= 7.5; // I = 1
I = 5.0 <= 5.0; // I = 1
I = 5 <= 4;   // I = 0

>=

5

a greater than or equal to b; evaluates to 0 or 1

Integer, real

I = 5.0 >= 7;   // I = 0
I = 5.0 >= 5; // I = 1
I = 5.0 >= 4.8; // I = 1

==

6

a equal to b; evaluates to 0, 1, or x (if any bit of a or b is x or z).

Integer, real

I = 5.2 == 5.2; // I = 1
I = 5.2 == 5.0; // I = 0
I = 1 == 1'bx;  // I = x

!=

6

a not equal to b; evaluates to 0, 1, or x (if any bit of a or b is x or z).

Integer, real

I = 5.2 != 5.2; // I = 0
I = 5.2 != 5.0; // I = 1

===

6

case equality; x and z bits included; evaluates to 0 or 1

integer

I = 1 === 1'bx; // I = 0

!==

6

case inequality; X and Z bits included; evaluates to 0 or 1

integer

I = 1 !== 1'bx; // I = 1

&&

10

Logical AND; evaluates to 0 or 1

Integer, real

I = (1==1)&&(2==2);  // I = 1
I = (1==2)&&(2==2); // I = 0
I = -13 && 1;   // I = 1

||

11

Logical OR; evaluates to 0 or 1

Integer, real

I = (1==2)||(2==2);  // I = 1
I = (1==2)||(2==3); // I = 0
I = 13 || 0;   // I = 1

&

7

Bitwise binary AND

Integer

See the Bitwise Binary AND Operator figure.

|

9

Bitwise binary OR

Integer

See the Bitwise Binary OR Operator figure.

^

8

Bitwise binary exclusive OR

Integer

See the Bitwise Binary Exclusive OR Operator figure.

^~

8

Bitwise binary exclusive NOR (Same as ~^)

Integer

See the Bitwise Binary Exclusive NOR Operator figure.

~^

8

Bitwise binary exclusive NOR (Same as ^~)

Integer

See the Bitwise Binary Exclusive NOR Operator figure.

<<

4

a shifted b bits left

Integer

I = 1 << 2;     // I = 4
I = 2 << 2; // I = 8
I = 4 << 2;   // I = 16

>>

4

a shifted b bits right

Integer

I = 4 >> 2;     // I = 1
I = 2 >> 2;   // I = 0

or

11

Event OR

Event expression

@(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.

Bitwise Binary AND Operator

& 0 1
0

0

0

1

0

1

Bitwise Binary OR Operator

| 0 1
0

0

1

1

1

1

Bitwise Binary Exclusive OR Operator

^ 0 1
0

0

1

1

1

0

Bitwise Binary Exclusive NOR Operator

^~ or ~^ 0 1
0

1

0

1

0

1

Bitwise Unary Negation Operator

~
0

1

1

0

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.

Conditional Operator

Operator Precedence Definition Type of Operands Allowed Example or Further Information

?:

12

exp ? t_exp : f_exp

Valid expressions

I= 2==3 ? 1:0;     // I = 0
R= 1==1 ? 1.0:0.0; // R=1.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 ;

is equivalent to

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

1

+ - ! ~ (unary)

Highest precedence

2

* / %

3

+ - (binary)

4

<< >>

5

< <= > >=

6

== != === !==

7

&

8

^ ~^ ^~

9

|

10

&&

11

||

12

?: (conditional operator)

Lowest precedence

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
 ⠀
X