In: Electrical Engineering
Write VHDL code for ALU 32bit. ALU must perform addition and subtraction. You are not allowed to use other libraries only this is allowed to use library ieee; use ieee.std_logic_1164.all; Please write the comments for me to fully understand. Thank you.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL ;
use IEEE.NUMERIC_STD.ALL ;
entity Simple_ALU is Port ( in_A, in_B : in STD_LOGIC_VECTOR(31 downto 0); --inputs of 32-bit Select : in STD_LOGIC_VECTOR(2 downto 0); -- to select the operation to be done ALU_Output : out STD_LOGIC_VECTOR(31 downto 0); -- Output of 32 bit ); end Simple_ALU;
- - The above code is for entity declaration and the input and output ports declaration.
- - For an ALU it is easy to use BEHAVIORAL model soo
architecture Behavioral of ALU is begin process(in_A,in_B,Select) -- based these inputs and select # operation is done begin case(Select) is when "000" => ALU_Output <= in_A + in_B ; -- # Addition of A and B when "001" => ALU_Output <= in_A - in_B ; -- # Subtraction of A and B when "010" => ALU_Output <= in_A and in_B; -- Logical and operation when "011" => ALU_Output <= in_A or in_B; -- Logical or operation when "100" => ALU_Output <= in_A xor in_B; -- Logical xor operation when "101" => ALU_Output <= in_A nor in_B; -- Logical nor operation when "110" => ALU_Output <= in_A nand in_B; -- Logical nand operation when "111" => -- Equal comparison if(A==B) then ALU_Output <= "1" ; -- Equal else -- Not Equal ALU_Output <= "0" ; end if; when others => ALU_Output <= in_A + in_B ; -- Here I prefer addition because end case; -- You ask ALU must perform Addition end process;
end Behavioral;
It is a Simple VHDL code for a Simple ALU. In the ALU we have more operations like Multiplication, Division, Left Ship, Right Shift, Less Than, Greater Than, Rotational Shifts.