基于FPGA的四级流水线8位加法器设计

四级流水线8位加法器

代码

//////////////////////////////////////////////////////////////////////////////////
// Company: NanJing University of Information Science & Technology
// Engineer: Yang Cheng Yu
// 
// Create Date: 2020/01/21 20:01:50
// Design Name: add_8bit
// Module Name: add_8bit
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////
//四级流水线8位加法器
module add_8bit(
	input 				clk,
	input[7:0] 			a_in,		//8位数输入a
	input[7:0] 			b_in,		//8位数输入b
	input 				cin,		//进位输入
	output reg[7:0]  	sum_out,	//加和输出
	output reg 			cout		//进位输出
);
	reg 					c_0to1bit;		//0-1位进位输出寄存器
	reg[1:0] 			sum_0to1bit;	//0-1位加和输出寄存器
	reg 					c_2to3bit;		//2-3位进位输出寄存器
	reg[1:0] 			sum_2to3bit;	//2-3位加和输出寄存器
	reg 					c_4to5bit;		//4-5位进位输出寄存器
	reg[1:0] 			sum_4to5bit;	//4-5位加和输出寄存器
	reg 					c_6to7bit;		//6-7位进位输出寄存器
	reg[1:0] 			sum_6to7bit;	//6-7位加和输出寄存器
	
	reg[1:0] 			tempa_2to3bit;	//2-3位输入a寄存器
	reg[1:0] 			tempb_2to3bit;	//2-3位输入b寄存器
	reg[1:0] 			tempa_4to5bit;	//4-5位输入a寄存器
	reg[1:0] 			tempb_4to5bit;	//4-5位输入b寄存器
	reg[1:0] 			tempa_6to7bit;	//6-7位输入a寄存器
	reg[1:0] 			tempb_6to7bit;	//6-7位输入b寄存器
	//0-1位加法计算,第一级流水线
	always@(posedge clk)begin
		{c_0to1bit,sum_0to1bit} <= a_in[1:0] + b_in[1:0] + cin;
		tempa_2to3bit <= a_in[3:2];
		tempb_2to3bit <= b_in[3:2];
	end
	//2-3位加法计算,第二级流水线
	always@(posedge clk)begin
		{c_2to3bit,sum_2to3bit} <= tempa_2to3bit + tempb_2to3bit + c_0to1bit;
		tempa_4to5bit <= a_in[5:4];
		tempb_4to5bit <= b_in[5:4];
	end
	//4-5位加法计算,第三级流水线
	always@(posedge clk)begin
		{c_4to5bit,sum_4to5bit} <= tempa_4to5bit + tempb_4to5bit + c_2to3bit;
		tempa_6to7bit <= a_in[7:6];
		tempb_6to7bit <= b_in[7:6];
	end
	//6-7位加法计算,第四级流水线
	always@(posedge clk)begin
		{c_6to7bit,sum_6to7bit} <= tempa_6to7bit + tempb_6to7bit + c_4to5bit;
	end
	//最终结果输出
	always@(posedge clk)begin
		cout <= c_6to7bit;
		sum_out <= {sum_6to7bit,sum_4to5bit,sum_2to3bit,sum_0to1bit};
	end
endmodule

RTL视图

在这里插入图片描述
在本设计中使用流水线的本质目的是为了将每一级的进位输出寄存起来。

仿真代码

`timescale 1ns/1ps
`define clock_period 20
module add_8bit_tb;
	reg 			clk		;
	reg[7:0] 	a_in		;
	reg[7:0] 	b_in		;
	reg 			cin		;
	wire[7:0] 	sum_out	;
	wire 			cout		;

add_8bit add_8bit(
	.clk		(clk),
	.a_in		(a_in),
	.b_in		(b_in),
	.cin		(cin),
	.sum_out	(sum_out),
	.cout		(cout)
);

	initial clk=1;
	always#(`clock_period/2)clk=~clk;
	
	initial begin
		a_in = 8'b10010010;
		b_in = 8'b01001101;
		cin = 1'b1;
		#(`clock_period*100);
		
		a_in = 8'b11110110;
		b_in = 8'b01101001;
		cin = 1'b1;
		#(`clock_period*100);
		$stop;
	end
endmodule

门级仿真波形

在这里插入图片描述
由仿真代码可看出,计算过程耗费86.916ns。

普通8位加法器

代码

module add_8bit_common(
	input 				clk,
	input [7:0]			a_in,
	input [7:0] 		b_in,
	input 				cin,
	output reg			cout,
	output reg[7:0] 	sum_out
);
	always@(posedge clk)begin
		{cout,sum_out} <= cin + a_in + b_in;
	end
endmodule

RTL视图

在这里插入图片描述

仿真代码

`timescale 1ns/1ps
`define clock_period 20
module add_8bit_common_tb;
	reg 				clk		;	
	reg[7:0] 		a_in		;	
	reg[7:0] 		b_in		;	
	reg 				cin		;	
	wire 				cout		;	
	wire[7:0] 		sum_out	;
	
add_8bit_common add_8bit_common(
	.clk		(clk),
	.a_in		(a_in),
	.b_in		(b_in),
	.cin		(cin),
	.cout		(cout),
	.sum_out	(sum_out)
);
	initial clk=1;
	always#(`clock_period/2)clk=~clk;
	
	initial begin
		a_in=8'b10010010;
		b_in=8'b00011110;
		cin=1'b1;
		#(`clock_period*100);
		
		a_in=8'b11100010;
		b_in=8'b01111010;
		cin=1'b1;
		#(`clock_period*100);
		$stop;
	end
endmodule

门级仿真波形

在这里插入图片描述
计算出最终结果消耗27.872ns。

若两种方案连续计算1000组数据,理论上普通加法器需要耗费27872ns,
流水线加法器约需要86.916+20*999=20,066.916ns。因此可以看出,流水线处理法在连续处理大批量数据时会比非流水线电路速度更高,但在处理少量数据,尤其在电路简单时,流水线处理反而速度会比较慢。

发布了10 篇原创文章 · 获赞 17 · 访问量 7340

猜你喜欢

转载自blog.csdn.net/qq_43650722/article/details/104060552