Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

padding out std_logic_vector with leading zeros

Tags:

vhdl

ok, what I would like to do is assign a smaller std_vector to a large one, padding out the upper bits with zeros. But, I want something generic and simple that doesn't involve knowing the size of each first.

for instance if I have:

signal smaller_vec: std_logic_vector(15 downto 0);
signal larger_vec: std_logic_vector(31 downto 0);

I could do:

larger_vec <= X"0000" & smaller_vec;

But what if I don't know the size of the smaller vector. Is there a was of specifying that all upper bits are zero.

I know about the others clause, but that would get messy as I'd need a couple of lines:

larger_vec(smaller_vec'high downto 0) <= smaller_vec;
larger_vec(31 downto smaller_vec'length) <= (others => '0');

I thought I could use:

larger_vec <= "" & smaller_vec;

but this didn't work. any ideas?

like image 853
James0 Avatar asked Mar 18 '15 12:03

James0


2 Answers

Have you tried:

larger_vec <= (31 downto smaller_vec'length => '0') & smaller_vec;

In the past I have had synthesis tool issues with code like that, so I have used:

constant ZERO : std_logic_vector(larger_vec'range) := (others => '0');
. . .
larger_vec <= ZERO(31 downto smaller_vec'length) & smaller_vec;
like image 117
Jim Lewis Avatar answered Oct 06 '22 07:10

Jim Lewis


James0's 2nd post was close, but the <= is facing the wrong direction, see below for a working example from duolos. I would edit, but at the time of this post I did not have enough reputation.

In https://www.doulos.com/knowhow/vhdl_designers_guide/vhdl_2008/vhdl_200x_ease/ in the Vectors in aggregates section it says:

    variable V : std_logic_vector(7 downto 0);
  begin
    V := (others => '0');                    -- "00000000"
    V := ('1', '0', others => '0');          -- "10000000"
    V := (4 => '1', others => '0');          -- "00010000"
    V := (3 downto 0 => '0', others => '1'); -- "11110000"
    -- V := ("0000", others => '1');         -- illegal!
larger_vec <= (smaller_vec'high downto 0 => smaller_vec, others => '0');

should work.

like image 26
Rafe H Avatar answered Oct 06 '22 07:10

Rafe H