niceman1992

FPGA 初学者征程(8) 基本组合电路之比较器

0
阅读(3298)

   好了,今天我们来说是比较器,比较器的真值表如下图所示:

 

  闲话少说,直接看代码

/* module name    :compare.v;
	 module function:compare A and B ;
	 Author:niceman
	 contact:18921428564@163.com
	 weibo :niceman1992
	 version : v1.0
	 write date :2015/4/18
	 */
module compare( Y ,A ,B );
 
 input [3:0] A ;
 input [3:0] B ;
 
 output [2:0] Y ;
 reg [2:0] Y ;
 
 always @ ( A or B )
     begin 
         if ( A > B ) 
             Y <= 3'b001;
         else if ( A == B)
             Y <= 3'b010;
         else 
             Y <= 3'b100;
       end
 endmodule
生成的RTL视图如下图所示


testbench如下

`timescale 1 ps/ 1 ps
module compare_vlg_tst();
// constants                                           
// general purpose registers

// test vector input registers
reg [3:0] A;
reg [3:0] B;
// wires                                               
wire [2:0]  Y;

// assign statements (if any)                          
compare i1 (
// port map - connection between master ports and signals/registers   
	.A(A),
	.B(B),
	.Y(Y)
);
initial                                                
begin    
     A=3'b001;B=3'b010;
#10  A=3'b011;B=3'b010;
#10  A=3'b001;B=3'b001;
#10  A=3'b001;B=3'b010;    
#10  $stop;               
end                                                    
                                               
endmodule

 

modelsim仿真的波形如下图所示

 

今天就到这里吧,如果大家有什么问题可以联系我,18921428564@163.com.