Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I increment this `std_logic_vector`

Tags:

vhdl

What's going on here? Why am I getting an 'operator argument type mismatch', and what can I do to fix it?

--
-- 32-bit counter with enable and async reset
--
architecture synthesis1 of counter_32bit is    
signal nextvalue : std_logic_vector ( 31 downto 0 );    
begin

  --
  -- combo
  --
  nextvalue <= value + 1; -- here

  --
  -- sequential
  --
  ff:process( clk, rst )
  begin

    if( rst = '1' ) then
      value <= 0; -- and here...
    elsif( clk'event and ( clk ='1' ) ) then
      if( ena = '1' ) then
         value <= nextvalue;
      end if;
    end if;

  end process ff;    

end synthesis1;

Thanks

like image 890
Marty Avatar asked May 12 '09 20:05

Marty


People also ask

What is std_logic_vector?

The std_logic_vector type is used for arrays of std_logic variables and signals. The basic VHDL logic operations are defined on this type: and , nand , or , nor , xor , xnor . These must be given two arrays of the same size; they do the operation on ecah position and return another array.

Is std_logic_vector signed?

Both std_logic_vector and unsigned are unconstrained arrays of std_logic . As is the signed type. std_logic_vector is declared in the std_logic_1164 package; unsigned and signed are declared in the package numeric_std .

How do I declare unsigned in VHDL?

The syntax for declaring signed and unsigned signals is: signal <name> : signed(<N-bits> downto 0) := <initial_value>; signal <name> : unsigned(<N-bits> downto 0) := <initial_value>; Just like with std_logic_vector, the ranges can be to or downto any range.


3 Answers

Try this code:

use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
...
nextvalue <= value + "1";

In my case this solution is works!

like image 34
Vladislav Avatar answered Nov 09 '22 23:11

Vladislav


you can't increment std_logic directly, you need to convert it to unsigned and the result back to std_logic_vector using the numeric_std package.

use ieee.numeric_std.all
...
nextvalue <= std_logic_vector( unsigned(value) + 1 );

See How Do Perform STD_LOGIC_VECTOR Addition Using IEEE.NUMERIC_STD for example.

like image 125
danielpoe Avatar answered Nov 09 '22 22:11

danielpoe


One more way is to overload the "+" in this case you can write:

function "+" ( a : std_logic_vector; b : integer ) return std_logic_vector is
    variable result : unsigned(a'range);
begin
    result := unsigned( a ) + 1 ;
    return std_logic_vector( result ) ;
end function ;

create a package and include this function in that package and this will do the trick . One more thing do include the ieee numeric_std package because it contains the conversion functions.

like image 37
abkds Avatar answered Nov 09 '22 22:11

abkds