Vhdl 用前导零填充标准逻辑向量

Vhdl 用前导零填充标准逻辑向量,vhdl,Vhdl,好的,我想做的是将一个较小的std_向量分配给一个较大的std_向量,用零填充高位。但是,我想要一些通用的和简单的东西,不需要知道每个第一个的大小 例如,如果我有: signal smaller_vec: std_logic_vector(15 downto 0); signal larger_vec: std_logic_vector(31 downto 0); 我可以做到: larger_vec <= X"0000" & smaller_vec; 就我而言,我也喜欢以下内容

好的,我想做的是将一个较小的std_向量分配给一个较大的std_向量,用零填充高位。但是,我想要一些通用的和简单的东西,不需要知道每个第一个的大小

例如,如果我有:

signal smaller_vec: std_logic_vector(15 downto 0);
signal larger_vec: std_logic_vector(31 downto 0);
我可以做到:

larger_vec <= X"0000" & smaller_vec;

就我而言,我也喜欢以下内容:

larger_vec <= (smaller_vec'high downto 0 <= smaller_vec, others => '0');
larger_vec <= (larger_vec'range => '0') + shorter_vec;
更大的\u vec您是否尝试过:

larger_vec <= (31 downto smaller_vec'length => '0') & smaller_vec;
较大的向量“0”);
. . .

我遇到过类似的问题,并尝试了以下方法:

larger_vec <= (smaller_vec'high downto 0 <= smaller_vec, others => '0');
larger_vec <= (larger_vec'range => '0') + shorter_vec;

较大的向量James0的第二个帖子很接近,但是零填充或截断任何std_逻辑向量或std_逻辑,精确到16位:

function pad16(x: std_logic_vector) 
    return std_logic_vector is 
    constant ZERO : std_logic_vector(15 downto 0) := (others => '0');
begin
    if (x'length < 16) then
        return ZERO(15 downto x'length) & x;
    else
        return x(15 downto 0);
    end if;
end function;

--overload function for single bit
function pad16(x: std_logic)
    return std_logic_vector is
    constant ZERO : std_logic_vector(15 downto 0) := (others => '0');
begin
    return ZERO(15 downto 1) & x;
end function;

-- USAGE:
--
--    reg16 <= pad16(X"3");

功能pad16(x:std_逻辑_向量)
返回标准逻辑向量为
常数0:std_逻辑_向量(15到0):=(其他=>'0');
开始
如果(x'长度<16),则
返回零(15到x'长度)&x;
其他的
返回x(15到0);
如果结束;
末端功能;
--单位过载功能
功能pad16(x:std_逻辑)
返回标准逻辑向量为
常数0:std_逻辑_向量(15到0):=(其他=>'0');
开始
返回零(15到1)&x;
末端功能;
--用法:
--

--reg16我认为前面的问题基本上回答了这个问题,但要注意,这个答案将扩展符号,用“unsigned”替换“signed”表示零…使用
ieee.std_logic\u unsigned时可能会重复。所有
,您也可以尝试:
更大的向量您也可以做
更大的向量'0')&更小的向量31
std\u logic\u unsigned
已弃用。使用
数字\u标准\u无符号
    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');
function pad16(x: std_logic_vector) 
    return std_logic_vector is 
    constant ZERO : std_logic_vector(15 downto 0) := (others => '0');
begin
    if (x'length < 16) then
        return ZERO(15 downto x'length) & x;
    else
        return x(15 downto 0);
    end if;
end function;

--overload function for single bit
function pad16(x: std_logic)
    return std_logic_vector is
    constant ZERO : std_logic_vector(15 downto 0) := (others => '0');
begin
    return ZERO(15 downto 1) & x;
end function;

-- USAGE:
--
--    reg16 <= pad16(X"3");