FPGA选修部分 quatusii 中实现有符号数比较和无符号数比较

今天学习FPGA的时候纠结了很久有符号和无符号该如何编写代码,后来百度了一些东西,才慢慢有印象,发现计算机里面都是执行的有符号数运算,那么我们直接用一段代码来简单说一下区别吧


  • 有符号数运算
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all; --necessary!

entity comparator is 
generic ( n:integer:=3);
port( a,b : in signed(n downto 0); 
			x1,x2,x3:out std_logic);
end comparator;

architecture rtl of comparator is 
begin
	x1<='1' when a>b else '0'; 
	x2<='1' when a=b else '0'; 
	x3<='1' when a<b else '0'; 
end rtl;

  • 无符号数运算
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all; --necessary!

entity comparator is 
generic ( n:integer:=3);
port( a,b : in unsigned(n downto 0); 
			x1,x2,x3:out std_logic);
end comparator;

architecture rtl of comparator is 
begin
	x1<='1' when a>b else '0'; 
	x2<='1' when a=b else '0'; 
	x3<='1' when a<b else '0'; 
end rtl;

那现在就是来找不同了,两段代码都是说输入两个数,参数化为4位的,然后比较大小,根据不同值得输出判断哪个数大一些
而区别就是在定义a,b输入的时候

分别定义signed和unsigned就是最简单的方法啦!
(但是记得调用ieee中的arith库)


新增一种实现无符号数比较的方法

library ieee;
use ieee.std_logic_1164.all;
//不需要arith

entity comparator is 
generic ( n:integer:=3);
port( a,b : in std_logic_vector(n downto 0); //一般形式的输入
			x1,x2,x3:out std_logic);
end comparator;

architecture rtl of comparator is 
begin
	x1<='1' when a>b else '0'; 
	x2<='1' when a=b else '0'; 
	x3<='1' when a<b else '0'; 
end rtl;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值