Carry look-ahead adder[16位超前进位加法器]

本文同期BLOG:http://hi.baidu.com/benclarkkevin/blog/item/1ce5ad1887e9d15043a9ad90.html

普通的ALU在运算时信号在传输上的延时引起结果的跳动。当最低位进位时,如果你的ALU很长,比如16BIT,

那最坏结果下需要32个GATE传输延时才能稳定。为了固定加法进位引起的延时,超前进位加法器就此被引入。

 

上图中的B1-4,C1-4分别为CLA(下图紫色部分)产生的4个全加器的借、进位。D,S为全加、减器(蓝色部分)生产的差、和。

这里的全加、减器没有传统上的进、借位输出,代替为P、G输出,进借位逻辑信号全部由CLA产生。

P这个进、借位预测就是如果1位全加器进、借位变为1时,进、借位的输出也会变为1,所以叫预测。

G半加、减进位,就是不考虑低位进位的全加、减结果是否会产生向高进、借位。

C进、借位的产生的条件就是,G已经进、借位或P与低位进、借位同时有效。

从上图中的如果还是按4段P、G表达式编写逻辑,那并不能得到超前进位的结果,需要把所有逻辑展开,

把前一级代入下一级变成一个金字塔的电路形态。如A(1111)+B(0000)+C(1)四位都是同时输出结果,

不需要原始的加法,进位从最低位一级一级的传递,因并行从而节约了加法时间。

把上面独立的1位加、减单元合并,并使用M选择,就得到下面的真值列表。

以下代码原创,但图片来自WIKI。

LCU(Lookahead Carry Unit)

CLA(Carry Lookahead Adder)

PG(Group Propagate)

GG(Group Generate)

在有效阅读下面的代码以前请务必先阅读文章尾部列出的文章。

 

 1 //-----------------------------------------------------
2 // This is Carry look-ahead adder demo program
3 // Design Name : 16bit adder/substractor with Carry Look Ahead(CLA)
4 // File Name : Adder.v
5 // Writer : Ben.C.Kevin
6 // DateTime : 2011.01.22(V1.0)
7 // Description : Read the Adder.docx file for detail.
8 //-----------------------------------------------------
9
10 module Adder(A,B,M,C0,Y,C4,PG,GG);
11 //This only the test one.
12
13 input [7:0]A;
14 input [7:0]B;
15 input M,C0;
16
17 output [15:0]Y;
18 output C4,PG,GG;
19
20 wire [4:0]C;
21 wire [3:0]P;
22 wire [3:0]G;
23 wire [15:0]A1;
24 wire [15:0]B1;
25
26 assign C[0] = C0;
27 assign C4 = C[4];
28
29 //Because DE2 has only 17 switches,
30 //So we just use top and bottom 4bits
31 //for every 16bits inputs.
32 //SW0 is the c0.
33 //SW17 is the mode selection.
34 //0 for add,1 for the sub
35 //LEDR15..0 is the summer.
36 //LEDR16 is the C16,the top carry bit.
37 //LEDG1 is PG, LEDR0 is GG.
38 assign A1 = {A[7:4],8'h00,A[3:0]};
39 assign B1 = {B[7:4],8'h00,B[3:0]};
40
41
42 //four 4bit adder with Carry Look Ahead(CLA)
43 Adder4bit a4_0(M,A1[3:0],B1[3:0],C[0],P[0],G[0],Y[3:0]);
44 Adder4bit a4_1(M,A1[7:4],B1[7:4],C[1],P[1],G[1],Y[7:4]);
45 Adder4bit a4_2(M,A1[11:8],B1[11:8],C[2],P[2],G[2],Y[11:8]);
46 Adder4bit a4_3(M,A1[15:12],B1[15:12],C[3],P[3],G[3],Y[15:12]);
47
48 //Carry Look Ahead Unit.
49 CLUclu(P,G,C[0],PG,GG,C[4:1]);
50
51 endmodule

 

为方便理解上图仅供结构参考,下面的设计中M是加减法选择。 

 1 module Adder16(A,B,M,C0,Y,C4,PG,GG);
2 //This is the full functional 16bit adder whit LCU
3
4 input [15:0]A;
5 input [15:0]B;
6 input M,C0;
7
8 output [15:0]Y;
9 output C4,PG,GG;
10
11 wire [4:0]C;
12 wire [3:0]P;
13 wire [3:0]G;
14
15 assign C[0] = C0;
16 assign C4 = C[4];
17
18 //four 4bit adder with Carry Look Ahead(CLA)
19 Adder4bit a4_0(M,A[3:0],B[3:0],C[0],P[0],G[0],Y[3:0]);
20 Adder4bit a4_1(M,A[7:4],B[7:4],C[1],P[1],G[1],Y[7:4]);
21 Adder4bit a4_2(M,A[11:8],B[11:8],C[2],P[2],G[2],Y[11:8]);
22 Adder4bit a4_3(M,A[15:12],B[15:12],C[3],P[3],G[3],Y[15:12]);
23
24 //Carry Look Ahead Unit.
25 CLUclu(P,G,C[0],PG,GG,C[4:1]);
26
27 endmodule

 

为方便理解上图仅供结构参考,下面的设计中M是加减法选择。

 1 module Adder4bit(M,A,B,C0,PG,GG,Y,C4);
2 //This is 4bit CLA adder
3
4 input [3:0]A;
5 input [3:0]B;
6 input M,C0;
7
8 output [3:0]Y;
9 output C4,PG,GG;
10
11 wire [4:0]C;
12 wire [3:0]P;
13 wire [3:0]G;
14
15 assign C[0] = C0;
16 assign C4 = C[4];
17
18 bit_full_adder fa_0(M,A[0],B[0],C[0],P[0],G[0],Y[0]);
19 bit_full_adder fa_1(M,A[1],B[1],C[1],P[1],G[1],Y[1]);
20 bit_full_adder fa_2(M,A[2],B[2],C[2],P[2],G[2],Y[2]);
21 bit_full_adder fa_3(M,A[3],B[3],C[3],P[3],G[3],Y[3]);
22
23
24 //Carry Look Ahead Unit.
25 CLUclu(P,G,C[0],PG,GG,C[4:1]);
26
27
28 endmodule

 

 1 module bit_full_adder(M,A,B,C,P,G,S);
2 //Full adder and subtractor whit the mode selection.
3
4 input A,B,C,M;
5 output P,G,S;
6
7 assign P = M^A^B;
8 assign G = (M^A)&B;
9 assign S = A^B^C;
10
11 endmodule

 

 1 module CLU(P,G,C0,PG,GG,C1to4);
2 //Common Carry Look Ahead Unit.
3 //Used by every adder layer.
4
5 input [3:0]P;
6 input [3:0]G;
7 input C0;
8
9 output PG,GG;
10 output [4:1]C1to4;
11
12 wire [4:0]C;
13
14 assign C1to4 = C[4:1];
15 assign PG = &P;
16 assign GG = G[3]|(G[2]&P[3])|(G[1]&P[3]&P[2])|(G[0]&P[3]&P[2]&P[1]);
17 assign C[1] = G[0]|(P[0]&C0);
18 assign C[2] = G[1]|(G[0]&P[1])|(C0&P[0]&P[1]);
19 assign C[3] = G[2]|(G[1]&P[2])|(G[0]&P[1]&P[2])|(C0&P[0]&P[1]&P[2]);
20 assign C[4] = G[3]|(G[2]&P[3])|(G[1]&P[2]&P[3])|(G[0]&P[1]&P[2]&P[3])|(C0&P[0]&P[1]&P[2]&P[3]);
21
22 endmodule

使用上面的16位超前加减法可以拼出64位超前加减法,以此也可以生成128位或更高。

 

参考:(www.wikipedia.org)

Adder(electronics)

Carry look-ahead adder

Lookahead Carry Unit

posted @ 2011-12-17 00:02  NeverExist  阅读(5645)  评论(0编辑  收藏  举报