12
\$\begingroup\$

I am new to Verilog, and would like to learn how to compare two numbers. For example, let's compare a parameter or reg (say a) with the number 2 (2'b10). How this will be written in Verilog?

travisbartley
4,9133 gold badges27 silver badges41 bronze badges
asked Sep 24, 2013 at 21:43
\$\endgroup\$

2 Answers 2

13
\$\begingroup\$

Equality and Relational Operators (return X if an operand has X or Z)

m == n // is m equal to n? (1-bit True/False result)
m != n // is m not equal to n? (1-bit True/False result)
m < n // is m less than n? (1-bit True/False result)
m > n // is m greater than n? (1-bit True/False result)
m <= n // is m less than or equal to n? (1-bit True/False result)
m >= n // is m greater than or equal to n? (1-bit True/False result)

Identity Operators (compare logic values 0, 1, X, and Z)

m === n // is m identical to n? (1-bit True/False results)
m !== n // is m not identical to n? (1-bit True/False result)

Example

If reg a is less than 2'b10, store 2'b11 in a.

if (a < 2'b10) begin
 a = 2'b11;
end

Caveats

  1. For most operations, the operands may be nets, variables, constants or function calls. Some operations are not legal on real (floating-point) values.
  2. Operators which return a true/false result will return a 1-bit value where 1 represents true, 0 represents false, and X represents indeterminate
  3. The === and !== operators are not supported for synthesis, because Z and X do not have the same meaning in simulation and hardware.
  4. If you compare two numbers of unequal width, the smaller will be expanded. Unsigned operands are expanded by left-extending with zero. Signed operands are expanded by left-extending with the value of the mostsignificant bit (the sign bit).

Source: "Verilog HDL Quick Reference Guide based on the Verilog-2001 standard (IEEE Std 1364-2001)" by Stuart Sutherland

answered Sep 25, 2013 at 3:37
\$\endgroup\$
1
\$\begingroup\$

Verilog numerical comparison operators are similar to those in C: ==, !=, <,>, <=,>=.

answered Sep 24, 2013 at 22:27
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.