【发布时间】:2013-07-01 08:23:37
【问题描述】:
我有一个来自 8 位 ADC 转换器的输入信号 (std_logic_vector(7 downto 0))。我必须将它们转换为 16 位信号 (std_logic_vector(15 downto 0)),以便将 16 位信号处理到 16 位系统。
【问题讨论】:
标签: vhdl
我有一个来自 8 位 ADC 转换器的输入信号 (std_logic_vector(7 downto 0))。我必须将它们转换为 16 位信号 (std_logic_vector(15 downto 0)),以便将 16 位信号处理到 16 位系统。
【问题讨论】:
标签: vhdl
如果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;
所以首先将std_logic_vector转换为有符号值,然后应用resize,这将对有符号值进行符号扩展,最后将结果转换回std_logic_vector。
转换过程相当冗长,但它的优点是它是通用的,即使稍后更改目标长度也可以工作。
'length 属性只返回 slv_16 std_logic_vector 的长度,即 16。
对于无符号表示而不是有符号,可以使用unsigned而不是signed来完成,因此使用以下代码:
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' 替换为那个。
如果任何一个 std_logic_vector 发生变化,则无需编辑零的宽度即可处理转换:
architecture 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;
【讨论】:
为了完整起见,还有另一种偶尔有用的方法:
-- 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));完成的
signed),然后使用resize 函数 - 正如你在你的回答中提出:)
借助新发布的 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)。
【讨论】: