Page 1 of 11 http://allaboutfpga.com/vhdl-code-for-4-bit-alu/
Online VHDL and FPGA Tutorials Create Profile
VHDL ! 4
VHDL code for 4-bit ALU
BY ADMIN · PUBLISHED MAY 19, 2014 · UPDATED MAY 23, 2016ALU’s comprise the combinational logic that implements logic operations such as AND, OR, NOT gate and arithmetic operations, such as Adder, Subtractor. Functionally, the operation of typical ALU is represented as shown in diagram FPGA Video Tutorial
VHDL VHDL Video Tutorial Xilinx Tutorial Altera Tutorial Simulation Tutorial " 30/4/17, 12*37 am VHDL code for 4-bit ALU
below,
Functional Description of 4-bit Arithmetic Logic Unit
Controlled by the three function select inputs (sel 2 to 0), ALU can perform all the 8 possible logic operations
Page 3 of 11 http://allaboutfpga.com/vhdl-code-for-4-bit-alu/
VHDL Code for 4-bit ALU 1 2 3 4 5 6 7 8
librarylibrary IEEE;
useuse IEEE.STD_LOGIC_1164.ALLALL; useuse IEEE.NUMERIC_STD.ALLALL; entityentity alu isis
Port ( inp_a : inin signed(3 downtodownto 0); inp_b : inin signed(3 downtodownto 0);
sel : inin STD_LOGIC_VECTOR (2 downtodownto 0);
30/4/17, 12*37 am VHDL code for 4-bit ALU
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
out_alu : outout signed(3 downtodownto 0)); endend alu;
architecturearchitecture Behavioral ofof alu isis beginbegin
processprocess(inp_a, inp_b, sel) beginbegin
casecase sel isis whenwhen "000" =>
out_alu<= inp_a + inp_b; --addition whenwhen "001" =>
out_alu<= inp_a - inp_b; --subtraction whenwhen "010" =>
out_alu<= inp_a - 1; --sub 1 whenwhen "011" =>
out_alu<= inp_a + 1; --add 1 whenwhen "100" =>
out_alu<= inp_a andand inp_b; --AND gate whenwhen "101" =>
out_alu<= inp_a oror inp_b; --OR gate whenwhen "110" =>
out_alu<= notnot inp_a ; --NOT gate whenwhen "111" =>
out_alu<= inp_a xorxor inp_b; --XOR gate whenwhen othersothers =>
NULLNULL; endend casecase;
endend processprocess; endend Behavioral;
Page 5 of 11 http://allaboutfpga.com/vhdl-code-for-4-bit-alu/
Testbench VHDL Code for 4-Bit ALU 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
LIBRARYLIBRARY ieee;
USEUSE ieee.std_logic_1164.ALLALL; USEUSE ieee.numeric_std.ALLALL;
ENTITYENTITY Tb_alu ISIS ENDEND Tb_alu;
ARCHITECTUREARCHITECTURE behavior OFOF Tb_alu ISIS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENTCOMPONENT alu PORTPORT(
inp_a : ININ signed(3 downtodownto 0); inp_b : ININ signed(3 downtodownto 0);
sel : ININ std_logic_vector(2 downtodownto 0); out_alu : OUTOUT signed(3 downtodownto 0) );
ENDEND COMPONENTCOMPONENT;
--Inputs
signalsignal inp_a : signed(3 downtodownto 0) := (othersothers => signalsignal inp_b : signed(3 downtodownto 0) := (othersothers => signalsignal sel : std_logic_vector(2 downtodownto 0) := (othersothers --Outputs
signalsignal out_alu : signed(3 downtodownto 0);
30/4/17, 12*37 am VHDL code for 4-bit ALU
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 BEGINBEGIN
-- Instantiate the Unit Under Test (UUT)
uut: alu PORTPORT MAPMAP ( inp_a => inp_a, inp_b => inp_b, sel => sel, out_alu => out_alu ); -- Stimulus process
stim_proc: processprocess beginbegin
-- hold reset state for 100 ns. waitwait forfor 100 ns;
-- insert stimulus here
inp_a <= "1001"; inp_b <= "1111";
sel <= "000"; waitwait forfor 100 ns; sel <= "001";
waitwait forfor 100 ns; sel <= "010";
waitwait forfor 100 ns; sel <= "011";
waitwait forfor 100 ns; sel <= "100";
waitwait forfor 100 ns;
30
Shares
23
4
Page 7 of 11 http://allaboutfpga.com/vhdl-code-for-4-bit-alu/
Tags: ALU VHDL code arithmetic and loogic unit vhdl code
YOU MAY ALSO LIKE...
Download Post as PDF Simulation Result for 4-bit ALU
61 62 63 64 65 66 67 68 sel <= "101"; waitwait forfor 100 ns; sel <= "110";
waitwait forfor 100 ns; sel <= "111";
endend processprocess; ENDEND;
$
30/4/17, 12*37 am VHDL code for 4-bit ALU
Comments 4 Pingbacks 0
4 RESPONSES
LAST UPDATED JUNE 8, 2016 Synchronous and Asynchronous Reset VHDL
0 !
LAST UPDATED MAY 19, 2016 VHDL 4 to 1 Mux (Multiplexer)
0 !
LAST UPDATED MAY 20, 2016 VHDL Code for Flipflop – D,JK,SR,T
2 !
! %
Admin& May 25, 2014 at 10:36 am
Soon, we will come up with step by step procedure to download bit in FPGA for all VHDL code and verify it.
Reply
⋆
user& October 5, 2015 at 3:17 pm
Temp <= std_logic_vector((unsigned("0" & Nibble1) + unsigned(Nibble2)));
i need an explanation to the above usage of "0" & nibble1….means i want to know the clear intention of the statement…..hope i will get a response
Page 9 of 11 http://allaboutfpga.com/vhdl-code-for-4-bit-alu/
thank you Reply
Admin& February 2, 2016 at 2:34 am
‘0’ is concatenated with nibble because the output result give carry at MSB. Reply
⋆
malu& October 14, 2016 at 10:16 am
i am not understand the program,another method will tri sir. Reply Name * Email * LEAVE A REPLY Comment Website 30/4/17, 12*37 am VHDL code for 4-bit ALU
Post Comment
Notify me of follow-up comments by email. Notify me of new posts by email.
WELCOME TO ALL ABOUT FPGA
Hi, If you are a beginner, looking to learn VHDL Programming and FPGA Design, this tutorials on VHDL and FPGA basics is a perfect match to getting started. This tutorial consist of various uses and features of the FPGA. It shows VHDL Programming written and explained with digital circuit development. Through many examples, you can be an expert in FPGA in no-time. You can reach us through allaboutfpga at gmail dot com
SUBSCRIBE TO ALL ABOUT FPGA Enter your email address to receive notifications of new Tutorial by email.
Email Address
Subscribe
Be the first of your friends to like this
All About FPGA 3,295 likes
Like Page Share
∠
All About FPGA © 2017. All Rights Reserved.
Page 11 of 11 http://allaboutfpga.com/vhdl-code-for-4-bit-alu/