"and" every element of std_logic_vector

P

Peter Larsen

Hi,

I have a std_logic_vector of variable length due to a generic constant. How
do I "and" or "or" every element of this vector with each other?

Thanks in advance
 
K

KJ

Peter Larsen said:
Hi,

I have a std_logic_vector of variable length due to a generic constant.
How do I "and" or "or" every element of this vector with each other?

Thanks in advance
Write a function that implements whatever it is you want to do....example to
simply 'or' together all of the even numbered bits in a std_logic_vector is
below

function My_Func(V: std_logic_vector) return std_logic is
variable RetVal: std_logic := '0';
begin
for i in V'range loop
if ((i mod 2) = 0) then
RetVal := RetVal or V(i);
end if;
end loop;
return(RetVal);
end function My_Func;

Then call this function wherever you need it as you would any other
function.

signal y: std_logic;
signal Some_Vector: std_logic_vector(....);
.....
y <= My_Func(Some_Vector)

Kevin Jennings
 
E

Eric Smith

Peter Larsen said:
I have a std_logic_vector of variable length due to a generic constant. How
do I "and" or "or" every element of this vector with each other?

v_and <= '1' when some_vector = (some_vector'range => '1') else '0';

v_or <= '1' when some_vector != (some_vector'range => '0') else '0';
 
K

kennheinrich

v_and <= '1' when some_vector = (some_vector'range => '1') else '0';

v_or <= '1' when some_vector != (some_vector'range => '0') else '0';

Applause! Beautiful use of the discrete range aggregate choice. But
this only holds for two-valued logic; if you want to preserve 'X's
you'll need to do an explicit loop, preferably done in a function. But
be careful with the loop example given above; it will only check every
other bit in the vector. You probably want a loop more like the one
below in your function.


variable product : std_logic := '1'; -- use '0' if ORING
....
for i in V'range do
product := product AND V(i);
end loop;
return product;

- Kenn
 
M

Mayan Moudgill

KJ said:
Write a function that implements whatever it is you want to do....example to
simply 'or' together all of the even numbered bits in a std_logic_vector is
below

function My_Func(V: std_logic_vector) return std_logic is
variable RetVal: std_logic := '0';
begin
for i in V'range loop
if ((i mod 2) = 0) then
RetVal := RetVal or V(i);
end if;
end loop;
return(RetVal);
end function My_Func;

Then call this function wherever you need it as you would any other
function.

signal y: std_logic;
signal Some_Vector: std_logic_vector(....);
....
y <= My_Func(Some_Vector)

Kevin Jennings

And how about:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY or_vec IS
PORT(
v : IN std_logic_vector;
vor : OUT std_logic
);
END ENTITY or_vec;

ARCHITECTURE behavior OF or_vec IS
COMPONENT or_vec IS
PORT(
v : IN std_logic_vector;
vor : OUT std_logic
);
END COMPONENT or_vec;
BEGIN
base_case: IF v'length = 1 GENERATE
BEGIN
vor <= v(v'low);
END GENERATE base_case;

induction_case: IF v'length > 1 GENERATE
CONSTANT mid : integer := (v'high + v'low)/2;
SIGNAL vor_l : std_logic;
SIGNAL vor_h : std_logic;
BEGIN
or_lo: or_vec PORT MAP(v(mid-1 DOWNTO v'low), vor_l);
or_hi: or_vec PORT MAP(v(v'high DOWNTO mid), vor_h);
vor <= vor_l or vor_h;
END GENERATE induction_case;
END ARCHITECTURE behavior;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top