介绍
比较器就是通过比较输入的大小,然后输出给出判断。
在这个比较器中,有两个输入,三个输出。根据输出就可以判断出哪个输入值大了。
设计文件
library ieee;
 use ieee.std_logic_1164.all;
 use ieee.std_logic_arith.all;
 entity comparator is
     generic(n : integer := 7);
     port( inp1,inp2 : in signed (n downto 0);
             outp1,outp2,outp3 : out std_logic);
 end entity;
 architecture behavior of comparator is
 begin 
     outp1 <= '1' when inp1 > inp2 else '0';
     outp2 <= '1' when inp1 = inp2 else '0';
     outp3 <= '1' when inp1 < inp2 else '0';
 end architecture;
测试文件
library ieee;
 use ieee.std_logic_1164.all;
 use ieee.std_logic_arith.all;
 entity tb_comparator is
     generic(n : integer := 7);
     
 end entity;
 architecture behavior of tb_comparator is
     component comparator is
         port( inp1,inp2 : signed (n downto 0);
                 outp1,outp2,outp3 : out std_logic);
     end component;
     signal inp1,inp2 : signed (n downto 0);
     signal outp1,outp2,outp3 : std_logic;
 begin
     dut : comparator
     port map(inp1,inp2,outp1,outp2,outp3);
     process
     begin
         inp1 <= "01101100";
         inp2 <= "10110000";
         wait for 10ns;
         inp1 <= "11101100";
         inp2 <= "11101100";
         wait for 10ns;
         inp1 <= "01101100";
         inp2 <= "01110000";
         wait for 10ns;
     end process;
 end architecture;
仿真结果

结语
实现这个比较器是很容易的,要注意使用无符号数和有符号数必须要声明std_logic_arith这个包集。上面给出了有符号数进行比较的方法,对于无符号数,只需要更改输入数据的数据类型就可以了。
有什么问题欢迎大家指出。