verilog 实现8位无符号加法器(串行进位)

一、首先定义一个1位全加器

module adder_1bit(a,b,cin,sum,cout);
	input a,b,cin;
	output sum,cout;
	
	sum sum1(a,b,cin,sum);
	carry carry1(a,b,cin,cout);
endmodule

由于上述模块使用到了我们自己定义的求和以及求进位模块,所以下面实现以下。这也是为了练习一下模块例化,增加结构化概念。

module sum(a,b,cin,sum);
	input a,b,cin;
	output sum;
	
	assign sum=a+b+cin;

endmodule

module carry(a,b,cin,cout);
	input a,b,cin;
	output cout;
	wire [1:0] sum;
	
	assign sum = a+b+cin;
		
	assign cout=sum[1];

endmodule

二、例化1位全加器构造8位加法器

需要注意的是,分别负责计算1位的8个1位加法器之间是通过进位输入cin和进位输出cout联系起来的。

module adder_8bit(a,b,cin,sum,cout);
	input[7:0] a,b;
	input cin;
	output[7:0] sum;
	output cout;
	wire c1,c2,c3,c4,c5,c6,c7;
	
	adder_1bit u1(a[0],b[0],cin,sum[0],c1);
	adder_1bit u2(a[1],b[1],c1,sum[1],c2);
	adder_1bit u3(a[2],b[2],c2,sum[2],c3);
	adder_1bit u4(a[3],b[3],c3,sum[3],c4);
	adder_1bit u5(a[4],b[4],c4,sum[4],c5);
	adder_1bit u6(a[5],b[5],c5,sum[5],c6);
	adder_1bit u7(a[6],b[6],c6,sum[6],c7);
	adder_1bit u8(a[7],b[7],c7,sum[7],cout);
	
endmodule

三、仿真程序

module tb_adder_8bit();
	
	reg[7:0] a,b;
	reg cin;
	wire[7:0] sum;
	wire cout;


	initial
	
		begin
			#400 $finish;
		end
	

	initial 
	

		begin
			a=255;
			b=0;
			cin=0;
			
			forever 
				begin
					#5 a=a-3;b=b+1;
				end
	
		end
	
	adder_8bit U1(
		      .a(a),
			  .b(b),
			  .cin(cin),
			  .sum(sum),
			  .cout(cout)
		     );
endmodule

四、仿真结果

在这里插入图片描述

posted @ 2020-09-27 10:11  耐心的小黑  阅读(497)  评论(0编辑  收藏  举报