Integer to std_logic_vector?

S

Sebastian Eggers

Hi,

i have to count several values from 0 to 99, at the moment i do it in an
array of integer 0 to 99.

As output-port i use a std_logic_vector(6 downto 0), but i may change
this if necessary.

How can i convert the integer to the vector? i have no idea and not
found any solutions yet. Any hint would be helpful

thanks
Sebastian
 
A

ALuPin

use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;

conv_std_logic_vector(7, 9);

converts integer 7 to a std_logic_vector with 9 bits.

Rgds
André
 
N

Nicolas Matringe

use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;

conv_std_logic_vector(7, 9);

converts integer 7 to a std_logic_vector with 9 bits.

Nope! Please stop using std_logic_arith package.

use ieee.numeric_std.all

std_logic_vector(to_unsigned(natural_number, nb_bits));

Nico
 
Joined
Jul 26, 2010
Messages
1
Reaction score
0
converts integer to a std_logic_vector

It's simple firstly you should include these libraries
------------------------------------------
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.all;
USE ieee.numeric_std.ALL;
-------------------------------------

now suppose you have declared
signal x : integer;
signal sig : std_logic_vector (7 downto 0); --- (I am taking here length as 8 you take as much as you want)

-------------------------------------------------
x <= 2;
sig <= std_logic_vector (to_unsgined(x,8 )) -- x is the integer and 8 is the length of std_logic vector sig . If your siganl's length is n (suppose) then you have to write...


sig <= std_logic_vector (to_unsgined(x,n)) -- replace n by length of the vector decalred by you.
 
Joined
Jul 26, 2010
Messages
1
Reaction score
0
-- Generally preferably to only use "official" IEEE libraries
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;

-- ...

-- "natural" means >= 0. Add range to x to help synthesis
constant NUM_BITS : natural := 8;
signal x : natural range 0 to 2**NUM_BITS-1;
signal vect : std_logic_vector(NUM_BITS-1 downto 0);

-- ...

-- By using LENGTH attribute, any change to vect is handled automatically
-- Could also just use NUM_BITS
x <= 2;
vect <= std_logic_vector(to_unsigned(x, vect'LENGTH));


The above code might look a little more complex, but by using constants and attributes, your code will be much more forgiving of changes in the future.
 
Joined
Jun 2, 2009
Messages
23
Reaction score
1
I guess using IEEE official libraries is better coz they will not cause synthesis problems at a latter stage.

There are many synthesis tools, all of them don't follow the same libraries!!

Cheers,
Debayan
 
Joined
Nov 11, 2011
Messages
2
Reaction score
0
Is there a way to do this when the integer is a generic? The LENGTH attribute doesn't apply and I need something equivalent to declare vect (in the above example) as:

signal vect : std_logic_vector(x'HIGH-1 downto 0);

This is legacy code that is being upgraded and the module is used in various places with different generic values from 13 to 25000 so using a single common width is not feasible.

Thanks
George
 
Last edited:
Joined
Jan 29, 2009
Messages
152
Reaction score
0
Given that you are talking about generics, you could just calculate the length 'compile-time' using a log2() function (I think you'll have to write the function yourself, but that is trivial)
 
Joined
Nov 11, 2011
Messages
2
Reaction score
0
Thanks Joris, I was hoping there was some trick that was more intrinsic but knowing there isn't saved a lot of wasted time.

Thanks
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top