VHDL符号值

VHDL符号值,vhdl,Vhdl,我在大学刚开始学习VHDL模块,我的讲师不善于讲解。如何在VHDL中使用/声明有符号值 这是我学过的基本代码格式,我目前正在编写一个2比特减法器。其他网站上的信息相当混乱 library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; use ieee.std_logic_arith.all; entity TwoBitSubtractor is port( x,y :in integer range 0

我在大学刚开始学习VHDL模块,我的讲师不善于讲解。如何在VHDL中使用/声明有符号值

这是我学过的基本代码格式,我目前正在编写一个2比特减法器。其他网站上的信息相当混乱

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_arith.all;

entity TwoBitSubtractor is port(
    x,y     :in integer range 0 to 3;
    result  :out integer range 0 to 3);
end TwoBitSubtractor;

architecture gates of TwoBitSubtractor is
begin
    result<= x - y;
end gates;
ieee库;
使用ieee.std_logic_1164.all;
使用ieee.numeric_std.all;
使用ieee.std_logic_arith.all;
实体TwoBitSubtractor是端口(
x、 y:在0到3的整数范围内;
结果:超出整数范围0到3);
结束二位减法器;
二位减法器的结构门是
开始

结果您应该使用
signed
类型来指定有符号值。Integer还可以用于以更易于人类阅读的方式声明值,但这样就没有位级定义,在我看来,这在VHDL中是不需要的。例如,您忽略了使用
integer
的任何信号所使用的位数,这对于高级语言来说可能很好,但对于VHDL来说并不太有用

library ieee;
use ieee.numeric_std.all;

entity TwoBitSubtractor is port(
    x      : in signed(2 downto 0);
    y      : in signed(2 downto 0);
    result : out signed(2 downto 0));
end TwoBitSubtractor;

architecture gates of TwoBitSubtractor is
begin
    result <= x - y;
end gates;
ieee库;
使用ieee.numeric_std.all;
实体TwoBitSubtractor是端口(
x:已签名(2到0);
y:已签名(2到0);
结果:签出(2到0);
结束二位减法器;
二位减法器的结构门是
开始

我想补充的结果是:如果您必须管理有符号/无符号,请不要使用ieee.std_logic_arith.all和其他ieee.std_logic_****all LIB。ieee.numeric\U std.all可以更好、正确地管理他们所做的所有操作。