Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing a long std_logic_vector to zeros

Tags:

vhdl

In simulation this works perfect. Is this is the best way of checking for zeros for a synthesisable code. What would be the resources generated?

signal vector_slv : std_logic_vector(2048 downto 0);
...
if (vector_slv = (vector_slv'range => '0')) then
  -- do something...

Is there any other optimal way to implement this solution considering h/w mapping (with optimal resource utilization).

I would be more interested in understanding the resources used.

like image 345
powernest Avatar asked Sep 25 '13 15:09

powernest


People also ask

How do you check if a vector is all zeros?

One way to check if a vector of any length is all zeros, is to convert it to an unsigned value and then compare it to its integer equivalent. This warning means that my_slv contains one or more bits that cannot be interpreted as neither '0' nor '1' .

What do you mean by Std_logic_vector?

The VHDL keyword “std_logic_vector” defines a vector of elements of type std_logic. For example, std_logic_vector(0 to 2) represents a three-element vector of std_logic data type, with the index range extending from 0 to 2.

How do I assign a zero to a vector in VHDL?

temp <= ( others => '0');


2 Answers

There's no way that makes more or less sense for synthesis. Write the code that best expresses your intention.

If you are comparing a vector for all zeros, the following should all produce the same results, or you should file a serious bug against the tool!

signal vector_slv : std_logic_vector(2048 downto 0);
constant zeros : std_logic_vector(vector_slv'range) := (others => '0');
...
if vector_slv = (vector_slv'range => '0') then
  -- do something...
if vector_slv = zeros then
  -- do something...
if unsigned(vector_slv) = to_unsigned(0, vector_slv'length) then
  -- do something...

and indeed for shorter vectors which fit in an integer:

if intvar = 0 then

will be exactly the same as any 32-bit vector comparison.

(BTW, note there is no need for parentheses around the if condition - VHDL is not C :)

like image 126
Martin Thompson Avatar answered Oct 27 '22 04:10

Martin Thompson


You could use the unary operators, e.g.:

  • and
  • nand
  • or
  • nor
signal vector_slv: std_logic_vector(2048 downto 0);
...
if and vector_slv then
    -- Do something for all 1...
elsif nand vector_slv then
    -- Do something for at least one 0...
elsif or vector_slv then
    -- Do something for at least one 1...
elsif nor vector_slv then
    -- Do something for all 0...
end if;

Or you could use the functions defined in std_logic_1164, e.g.:

function "and"  (l : STD_ULOGIC_VECTOR) return STD_ULOGIC;
function "nand" (l : STD_ULOGIC_VECTOR) return STD_ULOGIC;
function "or"   (l : STD_ULOGIC_VECTOR) return STD_ULOGIC;
function "nor"  (l : STD_ULOGIC_VECTOR) return STD_ULOGIC;
use ieee.std_logic_1164.all;
...
signal vector_slv: std_logic_vector(2048 downto 0);
...
if and(vector_slv) then
    -- Do something for all 1...
elsif nand(vector_slv) then
    -- Do something for at least one 0...
elsif or(vector_slv) then
    -- Do something for at least one 1...
elsif nor(vector_slv) then
    -- Do something for all 0...
end if;
like image 21
tim Avatar answered Oct 27 '22 04:10

tim