我无法弄清楚如何取两个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
Run Code Online (Sandbox Code Playgroud)
沟通非标准库包括并使用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;
Run Code Online (Sandbox Code Playgroud)
最后一行有很多[可能是不必要的]在std_logic_vector和之间进行转换signed,所以你可能更喜欢这个界面,如果它对你的设计的其余部分有意义:
port (
X: in signed(31 downto 0);
Y: in signed(31 downto 0);
F: out signed(31 downto 0)
);
Run Code Online (Sandbox Code Playgroud)
然后最后一行是:
F <= abs(X-Y);
Run Code Online (Sandbox Code Playgroud)