我有一个来自ADC转换器的输入信号,它是8位的 (std_logic_vector(7 downto 0)
)。 我需要将它们转换为16位信号(std_logic_vector(15 downto 0)
),以用于16位信号处理到16位系统。
我有一个来自ADC转换器的输入信号,它是8位的 (std_logic_vector(7 downto 0)
)。 我需要将它们转换为16位信号(std_logic_vector(15 downto 0)
),以用于16位信号处理到16位系统。
如果将这个8位值解释为带符号的(2的补码),那么通用的和标准的VHDL转换方法是使用IEEE numeric_std库:
library ieee;
use ieee.numeric_std.all;
architecture sim of tb is
signal slv_8 : std_logic_vector( 8 - 1 downto 0);
signal slv_16 : std_logic_vector(16 - 1 downto 0);
begin
slv_16 <= std_logic_vector(resize(signed(slv_8), slv_16'length));
end architecture;
slv_16 <= std_logic_vector(resize(unsigned(slv_8), slv_16'length));
architecture RTL of test is
signal s8: std_logic_vector(7 downto 0);
signal s16: std_logic_vector(15 downto 0);
begin
s16 <= X"00" & s8;
end;
s16 <= x"ff" & s8
或s16 <= (7 downto 0 => '1') & s8
。如果是后者,你应该提取s8的第7位,并将第二个语句中的“1”替换为该位。 - Zhehao Maoarchitecture RTL of test is
signal s8: std_logic_vector(7 downto 0);
signal s16: std_logic_vector(15 downto 0) := (others => '0');
begin
s16(s8'range) <= s8;
end;
借助新发布的VHDL-2019标准,您可以进行以下操作:
larger_vec <= extend(shorter_vec);
其中extend
是以下函数的定义
function extend(vec : std_logic_vector) return target_vec of std_logic_vector is
variable result : std_logic_vector(target_vec'length - 1 downto 0) := (others => '0');
begin
assert vec'length <= target_vec'length report "Cannot extend to shorter vector";
result(vec'length - 1 downto 0) := vec;
return result;
end function;
工具支持仍然有些有限,但至少有一个模拟器支持此功能(Riviera-PRO)。
为了完整起见,还有一种偶尔有用的方式:
-- Clear all the slv_16 bits first and then copy in the bits you need.
process (slv_8)
begin
slv_16 <= (others => '0');
slv_16(7 downto 0) <= slv_8;
end process;
据我所知,我从未尝试过针对向量进行此操作,但在更加复杂的情况下,我确实需要这样做:将仅几个相关信号复制到更大、更复杂的记录中。
slv_16 <= (others => slv_8(7));
。 - Morten Zilmersigned
),然后使用resize
函数 - 就像你在答案中提出的一样 :) - Martin Thompson