Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

Addition of a vector with a integer in vhdl code

There is a vector A(7 down to 0).Now we have to add this vector with an integer(let it be 5).B(7 down to 0) is output.Then how to write the vhdl code.

8 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    it depends what type A and B are. If A is a std_logic_vector you need to first convert it to an unsigned then add the integer. make sure you also include the numeric_std package (and do not add the std_logic_arith package).

    if A and B are already unsigned, you can just write:

    B <= A + 5;

    But we need to see the declaration of A and B to see what code you really need.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I have written the code as below:

    Library IEEE;

    use IEEE.std_logic_1164.all;

    use IEEE.numeric_std.all;

    use IEEE.STD_LOGIC_UNSIGNED.ALL;

    use IEEE.STD_LOGIC_ARITH.ALL;

    entity improc is

    port (clk : in std_logic;

    ipixel : in std_logic_vector(23 downto 0);

    opixel : out std_logic_vector(23 downto 0)

    );

    end entity improc;

    architecture rtl of improc is

    begin

    process (clk) begin

    if rising_edge (clk) then

    opixel(23 downto 16) <= std_logic_vector(unsigned( ipixel(23 downto 16)))+ conv_std_logic_vector(unsigned(9,8)));

    opixel(15 downto 8) <= std_logic_vector(unsigned( ipixel(15 downto 8)))+ conv_std_logic_vector(unsigned((9,8)));

    opixel(7 downto 0) <= std_logic_vector(unsigned( ipixel(7 downto 0)))+ conv_std_logic_vector(unsigned((9,8)));

    end if;

    end process;

    end architecture rtl;

    But it gives error.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The main problem is that you have included std_logic_arith AND numeric_std. They conflict with each other, so you need to remove std_logic_arith (its not standard VHDL. Neither is std_logic_unsigned, but Ill let you keep that)

    the answer would then be:

    opixel(23 downto 16) <= std_logic_vector( unsigned( ipixel(23 downto 16) ) + 9 );
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Is it necessaryto convert 9 into vector form using conv_std_logic_vector.Beacuse ipixel is 8 bit and 9 is constant.Plearse help...

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    No. Like I showed you, you can add an integer to an unsigned number. There is no need to use conv_std_logic_vector because you're adding an integer to an unsigned. (conv_std_logic_vector is NOT standard VHDL).

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Then what is the actual meaning of these "std_logic_vector(unsigned( ipixel(23 downto 16)))".Little bit confused...............

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    ipixel is a std_logic_vector

    unsigned( ipixel(23 downto 16)) converts the top 8 bits to an unsigned type.

    std_logic_vector( unsigned( ipixel(23 downto 16)) ) converts the unsigned back to a std_logic_vector.

    A std_logic_vector is not a number, it is just a collection of bits. An unsigned type is a similar array that can be treated as an unsigned number, hence the ability to do arithmatic with it.

    So the answer I proposed:

    opixel(23 downto 16) <= std_logic_vector( unsigned( ipixel(23 downto 16) ) + 9 );

    converts the input to an unsigned, adds 9 to it, then converts the result back to a std_logic_Vector.