VHDL 运算符及类型转换函数
一. 运算符二. 类型转换函数
一、数据类型
1. VHDL标准预先提供,无需添加任何库文件
integer:32位整数类型,范围-2147483647 ~ 2147483646;
signal tmp : integer range 0 to 31;
natural:integer 子类型,非负实数;
定义:subtype NATURAL is INTEGER range 0 to INTEGER'HIGH
positive:integer 子类型,正实数;
定义:subtype POSITIVE is INTEGER range 1 to INTEGER'HIGH
real:实数浮点类型,范围- 1.0E38 ~ + 1.0E38,不可综合,只在仿真平台使用;
bit :逻辑值,只能取值0,1;vhdl2008版标准定义:type BIT is ('0','1');
与std_logic的区别是std_logic有九种状态,如在判断上升沿的时候可以体现:
bit:1)if clk’event and clk = ‘1’ then… 2)if rising_edge(clk) then …
std_logic:1)if rising_edge(clk) then …
bit_vector:位矢量,逻辑值的组合;
定义:type BIT_VECTOR is array (NATURAL range <>) of BIT;
boolean:布尔型,取值true,false,定义:type BOOLEAN is (FALSE, TRUE);
severity level:消息类型,不可综合;
定义:type SEVERITY_LEVEL is (NOTE, WARNING, ERROR, FAILURE);
time:时间物理类型,不可综合,取值称为物理文字包括:1)整数或者浮点类型;
2)单位名称(fs,ps,ns,us,ms,sec,min,hr)
fs是基单位,ps/ns/ms…为导出单位;取值如下:
1 fs --> 注意中间的空格
5 ps --> 5000fs
4.3 ps --> 4300fs
6.4 fs --> 6fs, fs是基单位,所以抹掉小数点
范围-(2^31 - 1) ~ 2^31 – 1 fs
character:字符,256个取值;
string:字符串,区分大小写;
定义:type STRING is array (POSITIVE range <>) of CHARACTER
2. 自定义类型
1. std_logic_1164库定义,使用前必须声明此库的使用,如:
1)std_ulogic:未决断的标准逻辑位,共九种状态;
2)std_logic:已决断的标准逻辑位,共九种状态;
定义:subtype std_logic is resolved std_ulogic;
3)std_logic_vector:标准逻辑位矢量;
定义:type std_logic_vector is array ( NATURAL RANGE <>) of std_logic;
2. numeric_std库定义,使用前必须声明此库的使用,如:
1)signed/unsigned:有/无符号数;
定义:type unsigned is array ( NATURAL range <> ) of std_logic;
注意与std_logic_vector的区别,虽然定义一样,但是表示的意义不一样,如”1001”
std_logic_vector仅仅表示这是四个二进制位的组合;
signed表示有符号数-7(补码);
unsigned表示无符号数9;
3. 用户自定义
1)一维数组:type value_type is array (7 downto 0) of integer;
type value_type is array (0 to 7) of std_logic_vector(3 downto 0);
type array3 is array(natural range <>) of std_logic_vector(2 downto 0);
signal tmp : array3(7 downto 0);
数组初始化:
1)integer类型:integer_array <= (others => 0);
std_logic_vector类型:std_vector_array <= (others => (others => '0'));
2)使用for循环:
for i in 0 to 1 loop
integer_array(i) <= 0;
std_vector_array(i) <= (others => '0');
end loop;
2)二维数组:type value_type is array (7 downto 0,2 downto 0) of integer; // 8行3列
type value_type is array (7 downto 0,2 downto 0) of std_logic_vector(3 downto 0);
3)枚举类型:type fsm_state is (state0,state1,state2,state3);
4)子类型:subtype digit is integer range 0 to 9; //对已有的类型做约束
Note:
1)std_logic_arith、std_logic_unsigned、std_logic_signed是Synopsys等公司出的扩展库,numeric_std是IEEE标准库文件,如果同时声明这些库的使用会导致unsigned/signed函数重载的冲突,建议只使用numeric_std,其内容可以完全替代std_logic_arith、std_logic_unsigned、std_logic_signed;
2)resolved决断函数的定义是该数据类型是否可由多个驱动器进行驱动,如std_ulogic是未决断的,则其如果由多个驱动器进行驱动则会报错;
二. 运算符
1. 赋值运算
1)<=用于给signal赋值;
2):= 用于给variable,constant赋值;
2. 逻辑运算:NOT, AND, OR, NAND(与非), NOR(或非), XOR(异或),XNOR(同或);
3. 算术运算:+,-,*,/,**(指数),MOD(取模),REM(取余),ABS(绝对值);
A rem B = A - ( A / B ) * B rem运算符:A/B的运算结果是向0的方向取整;
A mod B = A - ( A / B ) * B mod运算符:A/B的运算结果是向下取整;
示例:5 mod 3 = 2;(-5) mod 3 = 1;5 mod (-3) = -1;(-5) mod (-3) = -2;
5 rem 3 = 2;(-5) rem 3 = -2;5 rem (-3) = 2;(-5) rem (-3) = -2;
4. 比较运算:=(等于),/=(不等于),>,<,>=,<=;
5. 并置运算:&
6. 移位运算:
1. 标准预定义,无需导入任何库的使用,仅支持bit_vector和boolean_vector类型;
sll:左移,右边移空位补零,定义:
示例:“1100” sll 1 =“1000”
srl:右移,左边移空位补零,示例:“1100” srl 1 =“0110”
sla:左移,右边第一位的数值保持原值不变,示例:”1001” sla 1 = “0011”
sra:右移,左边第一位的数值保持原值不变,示例:”1001” sra 1 = “1100”
rol:循环左移,示例:”1100” rol 1 = ”1001”
ror:循环右移,示例:”1100” ror 1 = ”0110”
2. numeric_std库定义,需声明该库的使用,仅支持unsigned/signed类型;
shift_left:左移,示例:shift_left(“1100”, 1) = “1000”
shift_right:右移,示例:shift_right(“1011”, 2) = “0010”;
rotate_left:循环左移,示例:rotate_left(“1100”, 1) = “1001”;
rotate_right:循环右移,示例:rotate_left(“1100”, 1) = “0110”;
Note:建议使用numeric_std库定义的移位操作符,标准预定义的在2008之前的版本可能产生奇怪的结果。
赋值运算 | 逻辑运算 | 算数运算 | 比较运算 | 并置运算 | 移位运算sll | 移位运算shift_left | ||
integer/ natural/ positive | Y tmp <= 3; | N | Y tmp <= tmp+3; | Y If tmp = 3 then… | N | N | N | |
bit | Y tmp <= ‘1’; | Y | N | Y if tmp = ‘1’ then… | Y tmp2 <= tmp & ‘1’; | N | N | |
bit_vector | Y tmp <= “11”; | Y tmp<=tmp or “11”; | N | Y if tmp = ‘11’ then… | Y | Y tmp <= tmp sll 1 | N | |
boolean | Y | Y | N | Y | N | N | N | |
std_logic | Y | N | N | Y | Y | N | N | |
std_logic _vector | Y tmp <= “11”; | Y | N | Y | Y | N | N | |
unsigned /signed | Y tmp <= “11”; | Y | Y tmp <= tmp+”11”; | Y | Y | N | Y tmp <= shift_left(tmp,2); |
三、类型转换函数
只介绍std_logic_1164,numeric_std包含的转换函数
integer | bit | bit_vector | std_logic | std_logic_vector | unsigned/signed | |
integer | NA | NA | NA | NA | integer<= to_integer(unsigned(tmp)); | integer<= unsigned(tmp); |
bit | NA | NA | NA | bit<= to_bit(tmp); | NA | NA |
bit_vetor | NA | NA | NA | NA | bit_vector<=to_bitvector(tmp); | NA |
std_logic | NA | NA | NA | NA | NA | NA |
std_logic _vector | ① | NA | ② | NA | NA | ③ |
unsigned/ signed | ④ | NA | NA | NA | ⑤ | NA |
①:std_logic_vector<=std_logic_vector(to_unsigned(tmp,N));
②:std_logic_vector<=to_stdlogicvector(tmp);
③:std_logic_vector<=std_logic_vector(tmp);
④:unsigned<=to_unsigned(tmp,N);
⑤:unsigned<=unsigned(tmp);
N为对应比特数;
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)