gpt4 book ai didi

vhdl - 我如何取 std_logic_vector 的绝对值?在 VHDL 中

转载 作者:行者123 更新时间:2023-12-05 00:50:58 25 4
gpt4 key购买 nike

我不知道如何取两个 std_logic_vector(31 downto 0) 的绝对值;

这是代码的示例:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all; -- for the signed, unsigned types and arithmetic ops
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
...
...
port (
X: in std_logic_vector(31 downto 0);
Y: in std_logic_vector(31 downto 0);
F: out std_logic_vector(31 downto 0)
);

..
..
..
process(X,Y)
begin
F <= abs(X-Y) --this doesnt work

最佳答案

抛弃非标准库包含并使用标准 signed内置 abs 的类型功能:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all; -- this is the standard package where signed is defined
-- never use non-standard ieee.std_logic_arith and ieee.std_logic_unsigned

...

port (
X: in std_logic_vector(31 downto 0);
Y: in std_logic_vector(31 downto 0);
F: out std_logic_vector(31 downto 0)
);

...

process(X,Y) is
begin
F <= std_logic_vector(abs(signed(X)-signed(Y)));
end process;

最后一行有很多 [可能不需要] 在 std_logic_vector 之间转换和 signed ,所以如果它对你的设计的其余部分有意义,你可能更喜欢这个界面:
port (  
X: in signed(31 downto 0);
Y: in signed(31 downto 0);
F: out signed(31 downto 0)
);

那么最后一行就是:
 F <= abs(X-Y);

关于vhdl - 我如何取 std_logic_vector 的绝对值?在 VHDL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21940548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com