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;

发布了78 篇原创文章 · 获赞 181 · 访问量 8704

猜你喜欢

转载自blog.csdn.net/qq_44790423/article/details/100144747