
2.3 运算操作符和属性
2.3.1 运算操作符
运算操作符是进行信号、变量等对象运算时不可或缺的。表2.2是VHDL中的运算操作符,共8类。其中,运算操作符类别的序号越大,优先级越高。同类别运算符在运算时具有相同的优先级,不同类别运算符的优先级依据表2.2中的优先级先后运算。
表2.2 VHDL运算操作符

条件运算符
VHDL条件运算符如表2.3所示。
表2.3 VHDL条件运算符

运算符??将bit类型的取值‘0’和‘1’与boolean类型的false和true对应转换,一般用在条件表达式中。这个转换运算符也会在库中重载给其他数据类型,例如,ieee库中的std_logic_1164包集将std_ulogic的“1”和“H”转换为true,其他值转换为false。条件运算符一般是隐式的,不会在程序中以代码的形式体现出来。
逻辑运算符
逻辑运算符是执行逻辑运算操作的运算符。VHDL逻辑运算符如表2.4所示。
表2.4 VHDL逻辑运算符

逻辑运算符示例如下。

关系运算符
关系运算符是判断两个操作数之间的数值关系的运算符。VHDL关系运算符如表2.5所示。
表2.5 VHDL关系运算符

操作符=和/=为所有非文件类型、非保护类型和不包含文件类型、保护类型子元素的复合类型提供数值相等和不相等判断。进行相等操作的两个操作数相等时,表达式返回true,否则返回false;不相等操作与之相反。当且仅当两个同类型的标量类型对象的值完全相同时,两者相等。当且仅当两个同类型的复合类型对象的子元素一一匹配且完全相同时,两者相等。
对于大小比较的运算符,标量类型的对象依据两个操作数的值的大小进行比较。对于标量数组类型,如果左操作数是空数组,右操作数是非空数组,那么左操作数小于右操作数。如果左右操作数都是非空数组,那么就从最高位开始逐位比较。
运算操作符?=、?/=、?<、?<=、?>和?>=是std库和ieee库为bit类型和std_ulogic预定义的匹配操作符。对于bit类型的操作数,这些匹配操作符的运算操作与普通的操作符相同。对于std_ulogic类型的操作数,匹配操作符?=和?<的运算定义如表2.6和表2.7所示,其余的匹配操作符的运算定义可以依此类推。
表2.6 操作符?=运算定义

续表

表2.7 操作符?<运算定义

移位运算符
移位操作符是对目标对象进行移位操作的运算符。VHDL中的移位操作符如表2.8所示。
表2.8 VHDL移位运算符

运算符sll和srl实现数据移位,移位产生的空位用‘0’填充。运算符sla实现数据左移,移位结束后将最右端的位复制后填充到空位上;运算符sra则与之相反,实现数据右移,移位结束后,将最左端的位复制后填充到空位上。运算符rol和ror实现数据循环移位,将移出的位依次填充到空位上。
移位运算符的示例如下。

加法运算符
VHDL中的加法运算符包括+、-和串联运算符&。VHDL中的加法运算符如表2.9所示。
表2.9 VHDL加法运算符

运算符+和-的操作为常规的数学加减操作。串联运算符的位移为数组类型定义的操作符,用于数据的拼接。串联运算符的示例如下。


标志运算符
标志运算符分别表示标识和取反的+和-。VHDL标志运算符如表2.10所示。
表2.10 VHDL标志运算符

标志运算符的示例如下。

乘法运算符
VHDL中的乘法运算符如表2.11所示,包括*、/、mod和rem,分别表示乘法、除法、取模和取余4种操作。
表2.11 VHDL乘法运算符

运算符*、/对任意整数类型和浮点类型有常规的算术操作。对于整数类型,除法运算是整除运算,运算结果会舍去小数部分转换为整数类型。运算符mod和rem针对任意的整数类型实现取模和取余操作。
VHDL乘法运算符对物理类型的定义,如表2.12所示。
表2.12 VHDL乘法运算符对物理类型的定义

乘法运算符的示例如下。


其他运算符
VHDL中还有两个操作符,不属于以上分类,如表2.13和表2.14所示。
表2.13 VHDL运算符abs

表2.14 VHDL运算符**

运算符abs和**的示例如下。

2.3.2 属性
VHDL中的属性可以从指定的对象中获得相关的数据和信息,使得VHDL代码更加灵活。VHDL中预定义的属性可以划分为7类,即数据类型和对象的属性、数组类型的属性、信号的属性、命名实体的属性、范围的属性、PSL对象的属性和模式视图的属性。
数据类型和对象的属性
VHDL中的数据类型和对象的属性如表2.15所示。
表2.15 VHDL中的数据类型和对象的属性

续表

续表

续表

数组类型的属性
VHDL中的数组类型的属性如表2.16所示。
表2.16 VHDL中的数组类型的属性

续表

信号的属性
VHDL中信号的属性如表2.17所示。
表2.17 VHDL中信号的属性

续表

命名实体的属性
VHDL中的命名实体的属性如表2.18所示。
表2.18 VHDL中的命名实体的属性

续表

范围的属性
VHDL中的范围的属性如表2-19所示。
表2-19 VHDL中的范围的属性

PSL对象的属性
VHDL中的PSL对象的属性如表2.20所示。
表2.20 VHDL中的PSL对象的属性

模式视图的属性
VHDL中的模式视图的属性如表2.21所示。
表2.21 VHDL中的模式视图的属性

2.3.3 通用属性
通用属性(generic)是VHDL中定义的静态常规参数,可以在实体、元件等程序段的声明中定义。在通用属性定义的程序段中,通用属性是静态的,是不可更改的。通用属性实现内部参数与外部程序的联系,在元件实例化或者配置声明中可以重新配置参数的值,从而实现程序段的功能多样化,增加代码的灵活性和重用性。通用属性的语法结构如下。

其中,parameter_name是参数的名称;parameter_type是参数的类型;parameter_value是参数的默认值。在通用属性实例化时,如果没有重新配置参数的值,参数将保持默认值不变。
例2.3 通用属性示例
下方代码实现了2输入的通用门,定义通用参数gate_type是实现在多种基本逻辑门中切换。在实例化元件或配置声明时,重新配置gate_type的值,可以将元件或者实体的功能切换为目标逻辑门。

