首页 > 试题广场 >

4bit超前进位加法器电路

[编程题]4bit超前进位加法器电路
  • 热度指数:46543 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

4bit超前进位加法器的逻辑表达式如下:

中间变量G_i=A_i B_i,<br />P_i=A_i⊕B_i

和:S_i=P_i⊕C_{i-1},进位:

请用Verilog语言采用门级描述方式,实现此4bit超前进位加法器,接口电路如下:


输入描述:
输入信号:
A_in[3:0],
B_in[3:0]
C_1
类型:wire


输出描述:
输出信号:
S[3:0]
CO
类型:wire
`timescale 1ns/1ns

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_1   ,
 
 	output	 wire			CO    ,
	output   wire [3:0]	    S
);
    wire [3:0] G , P  , C , PC;
	and and0 (G[0],A_in[0],B_in[0]),
		and1 (G[1],A_in[1],B_in[1]),
		and2 (G[2],A_in[2],B_in[2]),
		and3 (G[3],A_in[3],B_in[3]);

	xor xor0 (P[0],A_in[0],B_in[0]),
		xor1 (P[1],A_in[1],B_in[1]),
		xor2 (P[2],A_in[2],B_in[2]),
		xor3 (P[3],A_in[3],B_in[3]);

	and and4 (PC[0],P[0],C_1),
		and5 (PC[1],P[1],C[0]),
		and6 (PC[2],P[2],C[1]),
		and7 (PC[3],P[3],C[2]);

	or  or0 (C[0],G[0],PC[0]),
		or0 (C[1],G[1],PC[1]),
		or0 (C[2],G[2],PC[2]),
		or0 (CO,G[3],PC[3]);

	xor xor4 (S[0],P[0],C_1),
		xor5 (S[1],P[1],C[0]),
		xor6 (S[2],P[2],C[1]),
		xor7 (S[3],P[3],C[2]);

endmodule
题目要求用门级电路,起码给出了式子而且不太复杂,比上一题人性化多了
发表于 2025-07-12 11:38:58 回复(0)
`timescale 1ns/1ns

module lca_4(
    input       [3:0]       A_in  ,
    input       [3:0]       B_in  ,
    input                   C_1   ,
 
    output   wire           CO    ,
    output   wire [3:0]     S
);
    wire c1,c2,c3;
    wire cout1, cout2, cout3;
    wire [3:0] g, p;
   
    // 算出 G 和 P
    and a1 (g[0], A_in[0], B_in[0]);
    xor x1 (p[0], A_in[0], B_in[0]);

    and a2 (g[1], A_in[1], B_in[1]);
    xor x2 (p[1], A_in[1], B_in[1]);
   
    and a3 (g[2], A_in[2], B_in[2]);
    xor x3 (p[2], A_in[2], B_in[2]);
   
    and a4 (g[3], A_in[3], B_in[3]);
    xor x4 (p[3], A_in[3], B_in[3]);
   
    // 算出Cout(本级) = G(本级) + P(本级) * Cout(上一级)
    and a5 (c1, p[0], C_1);
  &nbs***bsp;o1 (cout1, g[0], c1);

    and a6 (c2, p[1], cout1);
  &nbs***bsp;o2 (cout2, g[1], c2);

    and a7 (c3, p[2], cout2);
  &nbs***bsp;o3 (cout3, g[2], c3);

    and a8 (c4, p[3], cout3);
  &nbs***bsp;o4 (CO, g[3], c4);

    // 算出S
    xor x5 (S[0], p[0], C_1);
    xor x6 (S[1], p[1], cout1);
    xor x7 (S[2], p[2], cout2);
    xor x8 (S[3], p[3], cout3);

endmodule
发表于 2024-01-30 06:32:43 回复(0)
`timescale 1ns/1ns

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_1   ,
 
 	output	 wire			CO    ,
	output   wire [3:0]	    S
);
    wire P0,P1,P2,P3;
	wire G0,G1,G2,G3;
	wire C0,C1,C2,C3;
	wire P0C_1,P1G0,P1P0C_1,P2G1,P2P1G0,P2P1P0C_1,P3G2,P3P2G1,P3P2P1G0,P3P2P1P0C_1;

	xor xor0(P0,A_in[0],B_in[0]),
		xor1(P1,A_in[1],B_in[1]),
		xor2(P2,A_in[2],B_in[2]),
		xor3(P3,A_in[3],B_in[3]),
		xor4(S[0],P0,C_1),
		xor5(S[1],P1,C0),
		xor6(S[2],P2,C1),
		xor7(S[3],P3,C2);

	and and0(G0,A_in[0],B_in[0]),
		and1(G1,A_in[1],B_in[1]),
		and2(G2,A_in[2],B_in[2]),
		and3(G3,A_in[3],B_in[3]),
		and4(P0C_1,P0,C_1),
		and5(P1G0,P1,G0),
		and6(P1P0C_1,P1,P0,C_1),
		and7(P2G1,P2,G1),
		and8(P2P1G0,P2,P1,G0),
		and9(P2P1P0C_1,P2,P1,P0,C_1),
		and10(P3G2,P3,G2),
		and11(P3P2G1,P3,P2,G1),
		and12(P3P2P1G0,P3,P2,P1,G0),
		and13(P3P2P1P0C_1,P3,P2,P1,P0,C_1);

	or or0(C0,G0,P0C_1),
	   or1(C1,G1,P1G0,P1P0C_1),
	   or2(C2,G2,P2G1,P2P1G0,P2P1P0C_1),
	   or3(C3,G3,P3G2,P3P2G1,P3P2P1G0,P3P2P1P0C_1);

	assign CO = C3;

endmodule

发表于 2023-10-31 20:44:23 回复(0)
建议搞个通俗点的说明

发表于 2023-07-28 20:44:13 回复(0)
`timescale 1ns/1ns

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_1   ,
 
 	output	 wire			CO    ,
	output   wire [3:0]	    S
);

wire [3:0]G,P;
wire [2:0]C;

assign G = A_in & B_in;
assign P = A_in ^ B_in;

assign S = P ^ {C,C_1};
assign {CO,C} = G | (P & {C,C_1});
    
endmodule

发表于 2023-07-28 15:44:41 回复(0)
module lca_4(
	input     wire   [3:0]       A_in,
	input     wire   [3:0]		    B_in,
    input     wire               C_1,
					  
 	output    wire			          CO,
	output    wire   [3:0]	      S
);
    and ad0(G0, A_in[0], B_in[0]),
		    ad1(G1, A_in[1], B_in[1]),
		    ad2(G2, A_in[2], B_in[2]),
		    ad3(G3, A_in[3], B_in[3]);
				
		xor xr0(P0, A_in[0], B_in[0]),
				xr1(P1, A_in[1], B_in[1]),
				xr2(P2, A_in[2], B_in[2]),
				xr3(P3, A_in[3], B_in[3]);
				
		and ad4(PC0, P0, C_1),
				ad5(PC1, P1, C0),
				ad6(PC2, P2, C1),
				ad7(PC3, P3, C2);
				
		or or0(C0, PC0, G0),
			 or1(C1, PC1, G1),
			 or2(C2, PC2, G2),
			 or3(CO, PC3, G3);
			 
		xor xr4(S[0], P0, C_1),
				xr5(S[1], P1, C0),
				xr6(S[2], P2, C1),
				xr7(S[3], P3, C2);
endmodule

发表于 2023-05-06 16:50:51 回复(0)

题解:

门级描述

笔者理解为使用逻辑门实现,即例化门级原语(and / or / not / xor 等)

超前进位:

首先想到单bit迭代,即行波进位,尝试后通过案例,并分析发现此法所需的逻辑门延时很长。
令n=串联单bit全加器的数目,则:
计算CO的门延时为2n+1,本案例采用4级串行,共需要需要9级门;
计算S各级bit的门延时为2n,即本例bit从0到3所需门延时分别为2、4、6、8。

然后根据题目,决定采用超前进位。即在理解行波进位的基础上,代入消除中间变量,将各级C和S都用初始输入表示。公式太复杂,图解太复杂,因此并未画出。此时,可发现:
计算各级C都统一需要3级
计算S需要在得到C的基础上再异或一次,共需要4级。


参考代码:

`timescale 1ns/1ns

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_1   ,
 
 	output	 wire			CO    ,
	output   wire [3:0]	    S
);
// 法一:超前进位
// 第1级:异或门
wire	q0,q1,q2,q3;
xor	my_xor0(q0,A_in[0],B_in[0]);
xor	my_xor1(q1,A_in[1],B_in[1]);
xor	my_xor2(q2,A_in[2],B_in[2]);
xor	my_xor3(q3,A_in[3],B_in[3]);


// 第2级:与门
wire	a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13;
and	my_and0(a0,A_in[0],B_in[0]);
and	my_and1(a1,C_1,q0);

and	my_and2(a2,A_in[1],B_in[1]);
and	my_and3(a3,A_in[0],B_in[0],q1);
and	my_and4(a4,C_1,q0,q1);

and	my_and5(a5,A_in[2],B_in[2]);
and	my_and6(a6,A_in[1],B_in[1],q2);
and	my_and7(a7,A_in[0],B_in[0],q1,q2);
and	my_and8(a8,C_1,q0,q1,q2);

and	my_and9 (a9, A_in[3],B_in[3]);
and	my_and10(a10,A_in[2],B_in[2],q3);
and	my_and11(a11,A_in[1],B_in[1],q2,q3);
and	my_and12(a12,A_in[0],B_in[0],q1,q2,q3);
and	my_and13(a13,C_1,q0,q1,q2,q3);

// 第3级:或门 —— 得到Cout
wire	C0,C1,C2;
or	my_or0 (C0,a0,a1);
or	my_or1 (C1,a2,a3,a4);
or	my_or2 (C2,a5,a6,a7,a8);
or	my_or3 (CO,a9,a10,a11,a12,a13);

// 第4级:异或门 —— 得到S[3:0]
xor	my_xor4(S[0],q0,C_1);
xor	my_xor5(S[1],q1,C0);
xor	my_xor6(S[2],q2,C1);
xor	my_xor7(S[3],q3,C2);



// // 法二:行波进位
// wire	C_2,C_3,C_4;
// lca_1	my_lca1(A_in[0],B_in[0],C_1,C_2,S[0]);
// lca_1	my_lca2(A_in[1],B_in[1],C_2,C_3,S[1]);
// lca_1	my_lca3(A_in[2],B_in[2],C_3,C_4,S[2]);
// lca_1	my_lca4(A_in[3],B_in[3],C_4,CO ,S[3]);

// endmodule

// module lca_1(
// 	input		A_in  ,
// 	input		B_in  ,
//     input		C_1   ,
 
//  	output	wire	CO  ,
// 	output	wire	S
// );

// wire	G,P,Q;
// xor		my_xor1(P,A_in,B_in);
// xor		my_xor2(S,P,C_1);
// and		my_and1(G,A_in,B_in);
// and		my_and2(Q,P,C_1);
// or		my_or1(CO,G,Q);

endmodule


发表于 2023-04-02 13:52:32 回复(0)
还是门级电路
wire [4:0] carry;
assign carry[0] = C_1;
assign CO = carry[4];

genvar i;
generate 
	for (i = 0; i < 4; i = i + 1) begin 
		assign S[i] = carry[i] ^ (A_in[i] ^ B_in[i]);
		assign carry[i + 1] = (A_in[i] & B_in[i]) | (carry[i] & (A_in[i] ^ B_in[i]));
	end
endgenerate


发表于 2023-03-17 10:56:04 回复(0)
`timescale 1ns/1ns

module lca_4(
    input        [3:0]       A_in  ,
    input        [3:0]        B_in  ,
    input                   C_1   ,

     output     wire            CO    ,
    output   wire [3:0]        S
);
wire [3:0]G;
wire [3:0]P;
wire [2:0]C;

assign G = A_in & B_in;
assign P = A_in ^ B_in;
assign S = P ^ {C,C_1};
assign {CO,C} = G | (P&{C,C_1});
endmodule

直接把公式翻译就可以了

发表于 2023-02-20 22:58:50 回复(3)
`timescale 1ns/1ns

module lca_4(
    input       [3:0]   A_in,
    input       [3:0]   B_in,
    input               C_1,

    output wire         CO,
    output wire [3:0]   S
);
    wire [4-1: 0]        g;
    wire [4-1: 0]        p;
    wire [4-1:-1]        c;
    wire [4-1: 0]        s;

    assign g = A_in & B_in;
    assign p = A_in ^ B_in;
    assign c[-1] = C_1;
    assign c[3:0] = g | p & c[2:-1];
    assign s = p ^ c[2:-1];

    // Output Logic
    assign CO = c[3];
    assign S = s;
endmodule

发表于 2023-01-24 16:37:04 回复(0)
`timescale 1ns/1ns

module lca_4(
    input       [3:0]       A_in  ,
    input       [3:0]       B_in  ,
    input                   C_1   ,
 
    output   wire           CO    ,
    output   wire [3:0]     S
);
    wire [4:0]CO_temp;
    genvar i;
    generate
        for(i=0;i<4;i=i+1)begin
            lca_1 lca_1_U4(
                .A_in(A_in[i]),
                .B_in(B_in[i]),
                .C_in(CO_temp[i]),
         
                .CO(CO_temp[i+1]),
                .S(S[i])
            );
        end    
    endgenerate

    assign CO_temp[0] = C_1;
    assign CO = CO_temp[4];
endmodule

module lca_1(
    input             A_in,
    input             B_in,
    input             C_in,
   
    output            CO,
    output            S
);
 
    assign S = A_in ^ B_in ^ C_in;
    assign CO = A_in & B_in | (A_in ^ B_in) & C_in;
   
endmodule
发表于 2023-01-14 16:19:21 回复(0)
`timescale 1ns/1ns

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_1   ,
 
 	output	 wire			CO    ,
	output   wire [3:0]	    S
);
wire [4:0] G,P,C;
genvar i;
assign C[0]=C_1;
assign CO=C[3];
for(i=0;i<4;i=i+1)begin:mk
assign	G[i]=A_in[i]&B_in[i];
assign	P[i]=A_in[i]^B_in[i];
assign S[i]=P[i]^C[i];
assign C[i+1]=G[i]|P[i]&C[i];
end


endmodule

发表于 2022-11-29 15:00:30 回复(0)
`timescale 1ns/1ns

/*
4位***:

  0 1 1 0
 +1 0 0 1
 ——————————
  1 1 1 1

*/

module lca_4(
    
    input       [N-1:0]       A_in  ,
    input       [N-1:0]     B_in  ,
    input                   C_1   ,
 
    output   wire           CO    ,
    output   wire [N-1:0]       S
    
);
parameter N=4;//数据长度为4,4位超前进位加法器
wire[N-1:0] C;

genvar i;
generate 
    for(i=0;i<N;i=i+1)
    begin:zyl1
        if(!i)begin
            assign S[i]=(A_in[i]^B_in[i])^C_1;
            assign C[i]=(A_in[i]&B_in[i])|((A_in[i]^B_in[i])&C_1);
        end
        else begin
            assign S[i]=(A_in[i]^B_in[i])^C[i-1];
            assign C[i]=(A_in[i]&B_in[i])|((A_in[i]^B_in[i])&C[i-1]);
        end     
    end
endgenerate
assign CO=C[N-1];
endmodule
发表于 2022-09-17 00:17:36 回复(0)
不用想那么多,直接按照题目中的逻辑表达式来

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_1   ,
 
 	output	 wire			CO    ,
	output   wire [3:0]	    S
);
    wire [3:0] tmpg;
    wire [3:0] tmpp;
    
    wire [3:0] tmpc;
    
    assign tmpg=A_in & B_in;
    assign tmpp=A_in ^ B_in;
    
    assign tmpc[3:0]=tmpg | ( tmpp & {tmpc[2:0],C_1});
    
    assign S=(tmpp ^ {tmpc[2:0],C_1});
    
    assign CO=tmpc[3];
    
endmodule


发表于 2022-08-24 14:43:44 回复(0)
`timescale 1ns/1ns

module lca_4(
	input		[3:0]       A_in  ,
	input	    [3:0]		B_in  ,
    input                   C_0   ,
 
 	output	 wire			CO    ,
	output    wire [3:0]	    S
);
    wire [4:0] g,p,c;
    assign g=A_in&B_in;
    assign p=A_in|B_in;
    assign c[0]=C_0;
    assign c[1] = g[0]|(p[0]&c[0]);
    assign c[2] = g[1]|(p[1]&(g[0]|(p[0]&c[0])));
    assign c[3] = g[2]|(p[2]&( g[1]|(p[1]&(g[0]|(p[0]&c[0])))));
    assign c[4] = g[3]|(p[3]&(g[2]|(p[2]&( g[1]|(p[1]&(g[0]|(p[0]&c[0])))))));
    assign C0 = c[4];
    assign S = g^p^c[3:0];
endmodule


发表于 2022-08-16 11:12:40 回复(0)

问题信息

难度:
32条回答 2345浏览

热门推荐

通过挑战的用户

查看代码
4bit超前进位加法器电路