VHDL 可综合向量比较
VHDL Synthesizable Vector Compare
我目前正在用 VHDL 编写 ALU,我正在使用 Cyclone II 板。我需要比较两个 std_logic_vectors 以查看一个是否大于另一个。我在条件中使用了大于(>)但我不知道FPGA是否可以合成该运算符。有没有一种解决方法可以在不使用“>”的情况下比较两个向量并合成它?
MCVE 会有帮助。但是,您尝试做的事情在模拟和综合中都是可能的。从你的问题中不清楚的是向量的格式是什么。他们签字了吗?未签名?二的补码?浮点?符号大小?
但是,假设这两个向量是有符号的,2 的补码。一个例子:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity foo is
port
(
a : in std_logic_vector(31 downto 0);
b : in std_logic_vector(31 downto 0);
y : out std_logic
);
end entity foo;
architecture rtl of foo is
signal a_signed : signed(a'range);
signal b_signed : signed(b'range);
begin
a_signed <= signed(a);
b_signed <= signed(b);
y <= '1' when (a_signed > b_signed) else '0';
end architecture rtl;
还有其他解决方案。以及针对不同格式的其他实现。
数据类型 std_logic_vector
只是一组位(有 9 个值...),但它没有定义任何算术行为。
要像您预期的那样使用加法、乘法或比较等算术运算 "greater than",您需要另一种数据类型,例如 UNSIGNED
或 SIGNED
。这些类型定义了额外的算术运算。因此,当您将信号声明为 (UN)SIGNED 时,VHDL 将知道额外的运算符,并且合成器将推断出正确的硬件。
-- declare the signals in a declarative region
signal mySignal1 : UNSIGNED(7 downto 0);
signal mySignal2 : UNSIGNED(7 downto 0);
signal greater : BOOLEAN;
-- now compare them
greater <= mySignal1 > mySignal2;
我目前正在用 VHDL 编写 ALU,我正在使用 Cyclone II 板。我需要比较两个 std_logic_vectors 以查看一个是否大于另一个。我在条件中使用了大于(>)但我不知道FPGA是否可以合成该运算符。有没有一种解决方法可以在不使用“>”的情况下比较两个向量并合成它?
MCVE 会有帮助。但是,您尝试做的事情在模拟和综合中都是可能的。从你的问题中不清楚的是向量的格式是什么。他们签字了吗?未签名?二的补码?浮点?符号大小?
但是,假设这两个向量是有符号的,2 的补码。一个例子:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity foo is
port
(
a : in std_logic_vector(31 downto 0);
b : in std_logic_vector(31 downto 0);
y : out std_logic
);
end entity foo;
architecture rtl of foo is
signal a_signed : signed(a'range);
signal b_signed : signed(b'range);
begin
a_signed <= signed(a);
b_signed <= signed(b);
y <= '1' when (a_signed > b_signed) else '0';
end architecture rtl;
还有其他解决方案。以及针对不同格式的其他实现。
数据类型 std_logic_vector
只是一组位(有 9 个值...),但它没有定义任何算术行为。
要像您预期的那样使用加法、乘法或比较等算术运算 "greater than",您需要另一种数据类型,例如 UNSIGNED
或 SIGNED
。这些类型定义了额外的算术运算。因此,当您将信号声明为 (UN)SIGNED 时,VHDL 将知道额外的运算符,并且合成器将推断出正确的硬件。
-- declare the signals in a declarative region
signal mySignal1 : UNSIGNED(7 downto 0);
signal mySignal2 : UNSIGNED(7 downto 0);
signal greater : BOOLEAN;
-- now compare them
greater <= mySignal1 > mySignal2;