Compare std_logic_vector to a constant using std_logic_vector package ONLY



I use the following package only in my VHDL file:

library IEEE;

In the code, I compare a std_logic_vector signal : A with a constant value, e.g

...if A<="00001011" then

yet the code was checked correctly by Xilinx ISE. My understanding is that STD_LOGIC_1164 package does not include an implementation of inequalities having as an operand std_logic_vector so why the above code statement was accepted and will the above comparison treat A as signed or unsigned number?





— Copying my comp.lang.vhdl reply to this post. Sorry some duplicates, but some does not.

All enumerated types and arrays of enumerated types implicitly define the regular ordering relational operators (>, >=, <, <=). Unfortunately it is not numerically ordered, so the results may not be as expected. Instead it is dictionary ordered.

First you have to look at the element type, which is std_logic whose base type is std_ulogic. For an enumerated type, such as std_ulogic, left values are less than right values, hence, for std_ulogic (and std_logic):
‘U’ < ‘X’ < ‘0’ < ‘1’ < ‘Z’ < ‘W’ < ‘L’ < ‘H’ < ‘-‘

For equal length arrays whose element base type is std_ulogic (such as std_logic_vector or std_ulogic_vector) whose values are only 0 or 1, things work out fine:
“1010” > “0101”

Note that dictionary comparisons always compare the left element first. Hence, for string, something that starts with ‘S’ is always less than something that starts with ‘T’ independent of length. This is great for sorting strings into a dictionary and is the only practical default – if we are going to provide such as thing.

OTOH, this is not so great if you are thinking things are numeric. For example, if the arrays are not equal length, then the following is true because the leading ‘1’on the left parameter is > the leading ‘0’ of the right parameter.
“100” > “0111”

Hence, with only “use ieee.std_logic_1164.all”, you have potential exposure to bad coding practices that mistakenly think of std_logic_vector as numeric (such as unsigned).

Many will argue, never use std_logic_vector for math and “>” is math. I generally agree.

So what do I do? How do I protect my design and design team from this. First you have to decide a policy and how to implement it.

1) Forbid use of regular ordering relational operators (>, >=, <, <=) with std_logic_vector and enforce it with a lint tool. However this means you have to buy and require the use of a lint tool.

2) Forbid use of regular ordering relational operators (>, >=, <, <=) with std_logic_vector and enforce it by using the both of the following package references. Note that this generates errors by referencing two definitions for each of the operators, and hence, when used the expression becomes ambiguous. Note this may be problematic since numeric_std_unsigned was introduced in 1076-2008 and it may not yet be supported by your synthesis tools.
library ieee ;
use ieee.numeric_std_unsigned.all ;
use ieee.std_logic_unsigned.all ;

3) Relax the rules some. Our biggest concern is design correctness. Allow std_logic_vector to be interpreted as an unsigned value and either reference numeric_std_unsigned (preferred, but it is VHDL-2008 and may not be implemented by your synthesis tool yet – but if it is not be sure to submit a bug report) or std_logic_unsigned (not preferred – this is an old shareware package that is not an IEEE standard and perhaps does not belong in the IEEE library – OTOH, it is well supported and it plays nice with other packages – such as numeric_std).

The nice result of this is that it also allows comparisons that include integers:
if A <= 11 then

Note, some suggest that the overloading of “>” and friends in numeric_std_unsigned/std_logic_unsigned is illegal. This was a very conservative interpretation of 1076 prior to VHDL-2008. It was fixed for all revisions of VHDL with an ISAC resolution prior to VHDL-2008 that determined that explicitly defined operators always overload implicitly defined operators without creating any ambiguity. I note that even the VHDL FAQ is out of date on this issue.

4) Be formal, but practical. Never use std_logic_vector. Only use numeric types, such as unsigned and signed from package ieee.numeric_std. Types signed and unsigned also support comparisons with integers.

There are probably a few strategies I left out.

Note that VHDL-2008 introduces matching operators which also address this issue by not defining them for types that do not have a numeric interpretation. These operators are: ?=, ?/=, ?>, ?>=, ?<, ?<=


Facebook Comments

Post a comment