Menu Close

VHDL运算符概述

在前面几章内容中虽然在介绍VHDL的组织架构和数据类型时用到过运算符,但并没有详细介绍运算符的运算规则,使用限制以及详细的使用方法,从本章内容逐渐介绍运算符的类型及使用。

1. VHDL运算符的基本概念及运算规则

VHDL 各种表达式(Eexpression)中的操作数(Operands) 是由不同类型的运算符(Operator)相连而成的, 运算符又称为操作符。其中操作数就是前面介绍过的数据对象。VHDL运算符基本是又两种类型构成,一种为基本运算符,另一种为重载运算符。基本运算符是VHDL语言的基本要素,可以满足VHDL预定义类型运算,但VHDL为了更好的实现设计和验证的覆盖面和灵活性,往往引入扩展数据类型。由于扩展数据类型没有对应的运算符,因此在引入数据类型时,同时也会引入该类型的运算规则。如IEEE中的std_logic_1164程序包中定义的std_ulogic及std_logic类型。由于VHDL语言是强数据类型语言,对于运算规则要求非常严格,一般要遵循如下三条规则,

(1)参与运算的操作数必须是相同的数据类型。

VHDL语言中不同数据类型之间不能参与运算,即使数据类型非常接近的位类型 (bit) 和标准类型(std_logic)之间也不能相互运算。因此在std_logic_1164的程序包中定义了二者之间的数据类型转函数,便于二者运算上的统一。

(2)不同宽度的矢量之间不能直接运算。

位宽不同的矢量,即使由相同数据类型组成的矢量也不能直接参与运算,如bit_vector(3 downto 0)与bit_vector(4 downto 0)不能直接参与运算,更不要说不同数据类型组成的矢量。那么如何解决这个问题呢?

    • 一般提供数据类型的程序包会提供重载运算符解决这类问题。当然用户也可以自行写一个重载运算符实现不同宽度类型矢量之间的运算。
    • 可以使用并位运算符(&)实现位宽的匹配。

(3)操作数的类型应与操作符要求的类型一致

如VHDL语言中定义的算术运算符+,-,*,/,MOD, REM 等运算符只是对整数或实数类型及其子类型进行操作,对于bit,bit_vector, std_logic, std_logic_vector等信号(矢量)不能参与运算。

2. VHDL语言运算符

VHDL语言运算符种类比较丰富,有逻辑运算,算数运算,关系运算符,并位运算符,赋值运算符等。VHDL2008与VHDL1993相比又补充一些新的运算符,下面就这些运算符分门别类进行介绍。

  • 逻辑运算符

逻辑运算符包含与(and),或(or), 异或(xor),与非(nand),或非(nor),异或非(xnor)以及非(not)等运算符。打开STD库中的程序包standard程序包可以看出,逻辑运算符只是对boolean,bit类型及其矢量定义了相应的函数,而对实数,整数等变量都没有相应的运算规则。因此除了boolean,bit类型外,其他类型如果要实现逻辑运算,只能先进行数据类型转换,转换成bit,bit_vector,boolean,boolean类型才能参与逻辑运算。逻辑运算符又分单目和双目运算符。

    • 双目逻辑运算符

双目运算符要求运算符的左右各有一个变量参与运算,如 a and b;

      • boolean, bit类型逻辑双目运算符
type boolean is (FALSE, TRUE);

    -- The predefined operations for this type are as follows:

    function "and"  (l, r: boolean) return boolean;
    function "or"   (l, r: boolean) return boolean;
    function "nand" (l, r: boolean) return boolean;
    function "nor"  (l, r: boolean) return boolean;
    function "xor"  (l, r: boolean) return boolean;
    function "xnor" (l, r: boolean) return boolean;

    function "not" (anonymous: boolean) return boolean;


type bit is ('0', '1');

    -- The predefined operations for this type are as follows:

    function "and"  (l, r: bit) return bit;
    function "or"   (l, r: bit) return bit;
    function "nand" (l, r: bit) return bit;
    function "nor"  (l, r: bit) return bit;
    function "xor"  (l, r: bit) return bit;
    function "xnor" (l, r: bit) return bit;

    function "not" (anonymous: bit) return bit;
      • bit_vector, boolean_vector 逻辑运算符
type boolean_vector is array (natural range <>) of boolean;

    -- The predefined operations for these types are as follows:

    function "and"  (l, r: boolean_vector) return boolean_vector;
    function "or"   (l, r: boolean_vector) return boolean_vector;
    function "nand" (l, r: boolean_vector) return boolean_vector;
    function "nor"  (l, r: boolean_vector) return boolean_vector;
    function "xor"  (l, r: boolean_vector) return boolean_vector;
    function "xnor" (l, r: boolean_vector) return boolean_vector;
    --
    --
    function "and"  (l: boolean_vector; r: boolean) return boolean_vector;
    function "and"  (l: boolean; r: boolean_vector) return boolean_vector;
    function "or"  (l: boolean_vector; r: boolean) return boolean_vector;
    function "or"  (l: boolean; r: boolean_vector) return boolean_vector;
    function "nand"  (l: boolean_vector; r: boolean) return boolean_vector;
    function "nand"  (l: boolean; r: boolean_vector) return boolean_vector;
    function "nor"  (l: boolean_vector; r: boolean) return boolean_vector;
    function "nor"  (l: boolean; r: boolean_vector) return boolean_vector;
    function "xor"  (l: boolean_vector; r: boolean) return boolean_vector;
    function "xor"  (l: boolean; r: boolean_vector) return boolean_vector;
    function "xnor"  (l: boolean_vector; r: boolean) return boolean_vector;
    function "xnor"  (l: boolean; r: boolean_vector) return boolean_vector;
    --
    
type bit_vector is array (natural range <>) of bit;

    -- The predefined operations for this type are as follows:

    function "and"  (l, r: bit_vector) return bit_vector;
    function "or"   (l, r: bit_vector) return bit_vector;
    function "nand" (l, r: bit_vector) return bit_vector;
    function "nor"  (l, r: bit_vector) return bit_vector;
    function "xor"  (l, r: bit_vector) return bit_vector;
    function "xnor" (l, r: bit_vector) return bit_vector;
    --
    --
    function "and"  (l: bit_vector; r: bit) return bit_vector;
    function "and"  (l: bit; r: bit_vector) return bit_vector;
    function "or"   (l: bit_vector; r: bit) return bit_vector;
    function "or"   (l: bit; r: bit_vector) return bit_vector;
    function "nand" (l: bit_vector; r: bit) return bit_vector;
    function "nand" (l: bit; r: bit_vector) return bit_vector;
    function "nor"  (l: bit_vector; r: bit) return bit_vector;
    function "nor"  (l: bit; r: bit_vector) return bit_vector;
    function "xor"  (l: bit_vector; r: bit) return bit_vector;
    function "xor"  (l: bit; r: bit_vector) return bit_vector;
    function "xnor" (l: bit_vector; r: bit) return bit_vector;
    function "xnor" (l: bit; r: bit_vector) return bit_vector;
    • 单目逻辑运算符

单目逻辑运算符的操作数只有一个,操作符只对矢量内部的各个组成进行相互逻辑运算。单目运算符如下:

--not operator      
         function "not" (anonymous: boolean) return boolean;
         function "not" (anonymous: bit) return bit;
         function "not" (anonymous: boolean_vector) return boolean_vector;
         function "not" (anonymous: bit_vector) return bit_vector;

---reduction operator
---boolean_vector 
        function "and"  (anonymous: boolean_vector) return boolean;
    function "or"   (anonymous: boolean_vector) return boolean;
    function "nand" (anonymous: boolean_vector) return boolean;
    function "nor"  (anonymous: boolean_vector) return boolean;
    function "xor"  (anonymous: boolean_vector) return boolean;
    function "xnor" (anonymous: boolean_vector) return boolean;
---bit_vector
        function "and"  (anonymous: bit_vector) return bit;
    function "or"   (anonymous: bit_vector) return bit;
    function "nand" (anonymous: bit_vector) return bit;
    function "nor"  (anonymous: bit_vector) return bit;
    function "xor"  (anonymous: bit_vector) return bit;
    function "xnor" (anonymous: bit_vector) return bit;

 

 

关于逻辑运算的详细内容,std_logic 类型在std_logic_1164的程序包中对std_ulogic, std_logic, std_ulogic_vector,std_logic_vector类型实现了运算符重载。请参见逻辑运算符详解。

  • 算术运算符

算术运算符包括加(+),减(-),乘(*),除(/),指数(**),求模(MOD),求余(REM), 绝对值(ABS)。算术运算符也分单目和双目运算符。

这些运算符对操作数的类型也有要求,从standard的程序包中算术运算符的定义可以看出,算术运算符只能使用整形,实数型和时间类型的信号或变量及对应的矢量进行运算。在算术运算符中有两个函数maximum与minmum求最大值和最小值,虽然是函数,这里也归为算术运算符统一介绍。maximum与minmum不仅能对整形,实型,时间型操作数参与运算,也可以对字符串型数据对象进行运算,返回字符。

这些运算符都是预定义的运算符,在VHDL中只要操作类型适配,可以直接使用。运算符的定义如下:

type integer is range -2147483647 to 2147483647;

-- The predefined operations for this type are as follows:

-- function "+" (anonymous: integer) return integer;
-- function "-" (anonymous: integer) return integer;
-- function "abs" (anonymous: integer) return integer;
--
-- function "+" (l, r: integer) return integer;
-- function "-" (l, r: integer) return integer;
-- function "*" (l, r: integer) return integer;
-- function "/" (l, r: integer) return integer;
-- function "mod" (l, r: integer) return integer;
-- function "rem" (l, r: integer) return integer;
--
-- function "**" (l: integer; r: integer) return integer;
--
-- function minimum (l, r: integer) return integer;
-- function maximum (l, r: integer) return integer;

-- Implementation_defined
type real is range -1.7014111e+308 to 1.7014111e+308;

-- The predefined operations for this type are as follows:

--
-- function "+" (anonymous: real) return real;
-- function "-" (anonymous: real) return real;
-- function "abs" (anonymous: real) return real;
--
-- function "+" (l, r: real) return real;
-- function "-" (l, r: real) return real;
-- function "*" (l, r: real) return real;
-- function "/" (l, r: real) return real;
--
-- function "**" (l: real; r: integer) return real;
--
-- function minimum (l, r: real) return real;
-- function maximum (l, r: real) return real;

-- Predefined type time:
-- implementation_defined
type time is range -2147483647 to 2147483647
units
    fs;                             -- femtosecond
    ps = 1000 fs;                   -- picosecond
    ns = 1000 ps;                   -- nanosecond
    us = 1000 ns;                   -- microsecond
    ms = 1000 us;                   -- millisecond
    sec = 1000 ms;                  -- second
    min = 60 sec;                   -- minute
    hr = 60 min;                    -- hour
end units;

-- The predefined operations for this type are as follows:


-- function "+"   (anonymous: time) return time;
-- function "-"   (anonymous: time) return time;
-- function "abs" (anonymous: time) return time;
--
-- function "+" (l, r: time) return time;
-- function "-" (l, r: time) return time;
--
-- function "*" (l: time; r: integer) return time;
-- function "*" (l: time; r: real) return time;
-- function "*" (l: integer; r: time) return time;
-- function "*" (l: real; r: time) return time;
--
-- function "/" (l: time; r: integer) return time;
-- function "/" (l: time; r: real) return time;
-- function "/" (l, r: time) return integer;
--
-- function "mod" (l, r: time) return time;
-- function "rem" (l, r: time) return time;
--
-- function minimum (l, r: time) return time;
-- function maximum (l, r: time) return time;
--
  ------------ string 
    -- function minimum (l, r: string) return string;
-- function maximum (l, r: string) return string;
--
-- function minimum (l: string) return character;
-- function maximum (l: string) return character;
  • 移位操作符

VHDL语言中有逻辑左移(sll),逻辑右移(srl),算术左移(sla),算术右移(sra),循环左移(rol),循环右移(ror)等移位操作,该操作符只能对bit_vector 和boolean_vector矢量类型操作。

function "sll"  (l: boolean_vector; r: integer) return boolean_vector;
function "srl"  (l: boolean_vector; r: integer) return boolean_vector;
function "sla"  (l: boolean_vector; r: integer) return boolean_vector;
function "sra"  (l: boolean_vector; r: integer) return boolean_vector;
function "rol"  (l: boolean_vector; r: integer) return boolean_vector;
function "ror"  (l: boolean_vector; r: integer) return boolean_vector;
--
function "sll"  (l: bit_vector; r: integer) return bit_vector;
function "srl"  (l: bit_vector; r: integer) return bit_vector;
function "sla"  (l: bit_vector; r: integer) return bit_vector;
function "sra"  (l: bit_vector; r: integer) return bit_vector;
function "rol"  (l: bit_vector; r: integer) return bit_vector;
function "ror"  (l: bit_vector; r: integer) return bit_vector;
  • 关系运算符

VHDL语言中有预定义的等于(),不等(),小于(),小于等于(),大于(),大于等于()等关系运算符,返回值都为boolean类型。关系运算符是重载做多的运算符,几乎可以适应所有数据类型。

(1)bit及boolean类型

----------bit type
-- function "="  (l, r: bit) return boolean;
-- function "/=" (l, r: bit) return boolean;
-- function "<"  (l, r: bit) return boolean;
-- function "<=" (l, r: bit) return boolean;
-- function ">"  (l, r: bit) return boolean;
-- function ">=" (l, r: bit) return boolean;

------------boolean  type
-- function "=" (l, r: boolean) return boolean;
-- function "/="(l, r: boolean) return boolean;
-- function "<" (l, r: boolean) return boolean;
-- function "<="(l, r: boolean) return boolean;
-- function ">" (l, r: boolean) return boolean;
-- function ">="(l, r: boolean) return boolean;
--

(2)severity 类型

type severity_level is (NOTE, WARNING, ERROR, FAILURE);

    -- The predefined operations for this type are as follows:

    -- function "="  (l, r: severity_level) return boolean;
    -- function "/=" (l, r: severity_level) return boolean;
    -- function "<"  (l, r: severity_level) return boolean;
    -- function "<=" (l, r: severity_level) return boolean;
    -- function ">"  (l, r: severity_level) return boolean;
    -- function ">=" (l, r:severity_level) return boolean;

(3)整数类型

-- The predefined operations for this type are as follows:

-- function "="  (l, r: integer) return boolean;
-- function "/=" (l, r: integer) return boolean;
-- function "<"  (l, r: integer) return boolean;
-- function "<=" (l, r: integer) return boolean;
-- function ">"  (l, r: integer) return boolean;
-- function ">=" (l, r: integer) return boolean;

(4)实数类型

-- The predefined operations for this type are as follows:

-- function "="  (l, r: real) return boolean;
-- function "/=" (l, r: real) return boolean;
-- function "<"  (l, r: real) return boolean;
-- function "<=" (l, r: real) return boolean;
-- function ">"  (l, r: real) return boolean;
-- function ">=" (l, r: real) return boolean;

(5)时间类型

-- The predefined operations for this type are as follows:

    -- function "="  (l, r: time) return boolean;
    -- function "/=" (l, r: time) return boolean;
    -- function "<"  (l, r: time) return boolean;
    -- function "<=" (l, r: time) return boolean;
    -- function ">"  (l, r: time) return boolean;
    -- function ">=" (l, r: time) return boolean;

(6)字符串类型

-- The predefined operations for these types are as follows:

    -- function "="  (l, r: string) return boolean;
    -- function "/=" (l, r: string) return boolean;
    -- function "<"  (l, r: string) return boolean;
    -- function "<=" (l, r: string) return boolean;
    -- function ">"  (l, r: string) return boolean;
    -- function ">=" (l, r: string) return boolean;

(7) boolean_vector 与 bit_vector类型

 -------boolean_vector       
    -- function "="  (l, r: boolean_vector) return boolean;
    -- function "/=" (l, r: boolean_vector) return boolean;
    -- function "<"  (l, r: boolean_vector) return boolean;
    -- function "<=" (l, r: boolean_vector) return boolean;
    -- function ">"  (l, r: boolean_vector) return boolean;
    -- function ">=" (l, r: boolean_vector) return boolean;
-------bit_vector
    -- function "="  (l, r: bit_vector) return boolean;
    -- function "/=" (l, r: bit_vector) return boolean;
    -- function "<"  (l, r: bit_vector) return boolean;
    -- function "<=" (l, r: bit_vector) return boolean;
    -- function ">"  (l, r: bit_vector) return boolean;
    -- function ">=" (l, r: bit_vector) return boolean;

(8)integer_vector类型

-- function "="  (l, r: integer_vector) return boolean;
-- function "/=" (l, r: integer_vector) return boolean;
-- function "<"  (l, r: integer_vector) return boolean;
-- function "<=" (l, r: integer_vector) return boolean;
-- function ">"  (l, r: integer_vector) return boolean;
-- function ">=" (l, r: integer_vector) return boolean;
--

(9)real_vector与time_vector类型

---real_vector	
        -- function "="  (l, r: real_vector) return boolean;
    -- function "/=" (l, r: real_vector) return boolean;
---time_vector
    -- function "="  (l, r: time_vector) return boolean;
    -- function "/=" (l, r: time_vector) return boolean;

real_vector与time_vector两种类型的关系运算符只有相等和不等两种关系运算符。

(10)文件打开类型

type file_open_kind is (
    READ_MODE,                      -- Resulting access mode is read-only.
    WRITE_MODE,                     -- Resulting access mode is write-only.
    APPEND_MODE                     -- Resulting access mode is write-only; information is appended to the end of the existing file.
);

-- The predefined operations for this type are as follows:

-- function "="  (l, r: file_open_kind) return boolean;
-- function "/=" (l, r: file_open_kind) return boolean;
-- function "<"  (l, r: file_open_kind) return boolean;
-- function "<=" (l, r: file_open_kind) return boolean;
-- function ">"  (l, r: file_open_kind) return boolean;
-- function ">=" (l, r: file_open_kind) return boolean;

(11)文件打开的状态类型

  type file_open_status is (
    OPEN_OK,                        -- File open was successful.
    STATUS_ERROR,                   -- File object was already open.
    NAME_ERROR,                     -- External file not found or inaccessible.
    MODE_ERROR                      -- Could not open file with requested access mode.
);

-- The predefined operations for this type are as follows:

-- function "="  (l, r: file_open_status) return boolean;
-- function "/=" (l, r: file_open_status) return boolean;
-- function "<"  (l, r: file_open_status) return boolean;
-- function "<=" (l, r: file_open_status) return boolean;
-- function ">"  (l, r: file_open_status) return boolean;
-- function ">=" (l, r: file_open_status) return boolean;
--

(12) 扩展关系运算符

在VHDL2008的std库中又重载了带有通配符的关系运算符,如:

function "?="  (l, r: bit) return bit;
function "?/=" (l, r: bit) return bit;
function "?<"  (l, r: bit) return bit;
function "?<=" (l, r: bit) return bit;
function "?>"  (l, r: bit) return bit;
function "?>=" (l, r: bit) return bit;
  • 并位运算符

在VHDL语言中在矢量运算时经常会因为矢量的宽度不同而出现计算错误,在VHDL的基本类型中引入并位操作符&,可以使参与运算的bit_vector,boolean_vector等矢量位宽匹配,从而可以实现矢量之间的运算。

-- string concatination operator
    -- function "&" (l: string; r: string) return string;
    -- function "&" (l: string; r: character) return string;
    -- function "&" (l: character; r: string) return string;
    -- function "&" (l: character; r: character) return string;
    --


-- boolean_vector concatination operator
    -- function "&" (l: boolean_vector; r: boolean_vector) return boolean_vector;
    -- function "&" (l: boolean_vector; r: boolean) return boolean_vector;
    -- function "&" (l: boolean; r: boolean_vector) return boolean_vector;
    -- function "&" (l: boolean; r: boolean) return boolean_vector;
    --
-- bit_vector concatination operator
    -- function "&" (l: bit_vector; r: bit_vector) return bit_vector;
    -- function "&" (l: bit_vector; r: bit) return bit_vector;
    -- function "&" (l: bit; r: bit_vector) return bit_vector;
    -- function "&" (l: bit; r: bit) return bit_vector;

--integer_vector
    --
    -- function "&" (l: integer_vector; r: integer_vector) return integer_vector;
    -- function "&" (l: integer_vector; r: integer) return integer_vector;
    -- function "&" (l: integer; r: integer_vector) return integer_vector;
    -- function "&" (l: integer; r: integer) return integer_vector;
    --
--real_vector 
    --
    -- function "&" (l: real_vector; r: real_vector) return real_vector;
    -- function "&" (l: real_vector; r: real) return real_vector;
    -- function "&" (l: real; r: real_vector) return real_vector;
    -- function "&" (l: real; r: real) return real_vector;
    --

总结: 本节内容介绍了VHDL预定义运算符及重载函数,运算符分类,并对VHDL2008的扩展类型做了简单介绍。本节的着重点向大家介绍运算符的运算规则和操作数之间的匹配关系,其他的内容如扩展数据类型的运算,以及这些运算符详细的使用规则将在后续章节中结合实例逐一介绍。

 

 

 

 

 

 

 

Posted in FPGA, FPGA 教材教案, IC, IC 教材教案, IC课程习题库, VHDL, VHDL, 开发语言, 教材与教案, 文章, 编程语言

发表回复

相关链接