Skip to main content

ALU - Behavioural modelling


library ieee;
use ieee.std_logic_1164.all;

library work;
use work.Gates.all;

entity alu is
generic(
N : integer:=4
);
port (
A : in std_logic_vector(N-1 downto 0);
B : in std_logic_vector(N-1 downto 0);
Y : out std_logic_vector(5 downto 0)
) ;
end alu;

architecture beh of alu is

function max(A: in std_logic_vector; B : in std_logic_vector)
return std_logic_vector is
variable is_max : std_logic_vector(N-1 downto 0):= (others => '0');
begin
for i in N-1 downto 0 loop
if ((A(i) xor B(i)) = '1') then
if (A(i) = '1') then
return A;
else
return B;
end if;
end if;
end loop;
return is_max;
end max;

function eq(A: in std_logic_vector; B : in std_logic_vector)
return std_logic_vector is
variable is_eq : std_logic_vector(N-1 downto 0):= (others => '0');
begin
for i in N-1 downto 0 loop
if ((A(i) xor B(i)) = '1') then
return is_eq;
end if;
end loop;
return A;
end eq;

alias a3 : std_logic is A(N-1);
alias b3 : std_logic is B(N-1);
begin

alu1 : process( A,B )
begin
if ((b3 = '0') and (a3 = '0')) then
Y <= "00"& max(A,B);
elsif ((b3 = '0') and (a3 = '1')) then
Y <= "00"& (A and B);
elsif ((b3 = '1') and (a3 = '0')) then
Y <= "00"& (not A);
elsif ((b3 = '1') and (a3 = '1')) then
Y <= "00"& eq(A,B);
else
Y <= (others => '0');
end if;
end process ; -- alu1
end;