题解 | #超前进位加法器#
超前进位加法器
https://www.nowcoder.com/practice/c4c6afdab9ce45a3a2279a98391686ca
`timescale 1ns/1ns
module huawei8//四位超前进位加法器
(
input wire [3:0]A,
input wire [3:0]B,
output wire [4:0]OUT
);
//*************code***********//
wire [3:0] P;
wire [3:0] G;
wire [3:0] F;
wire [4:1] C;
CLA_4 cla(.P(P),.G(G),.C_in(0),.Ci(C),.Gm(),.Pm());
Add1 a0(.a(A[0]),.b(B[0]),.C_in(0),.f(F[0]),.g(G[0]),.p(P[0]));
Add1 a1(.a(A[1]),.b(B[1]),.C_in(C[1]),.f(F[1]),.g(G[1]),.p(P[1]));
Add1 a2(.a(A[2]),.b(B[2]),.C_in(C[2]),.f(F[2]),.g(G[2]),.p(P[2]));
Add1 a3(.a(A[3]),.b(B[3]),.C_in(C[3]),.f(F[3]),.g(G[3]),.p(P[3]));
assign OUT={C[4],F};
//*************code***********//
endmodule
//////////////下面是两个子模块////////
module Add1
(
input a,
input b,
input C_in,
output f,
output g,
output p
);
assign f=a^b^C_in;
assign g=a|b;
assign p=a&b;
endmodule
module CLA_4(
input [3:0]P,
input [3:0]G,
input C_in,
output [4:1]Ci,
output Gm,
output Pm
);
assign Ci[1]=P[0]|G[0]&C_in;
assign Ci[2]=P[1]|G[1]&Ci[1];
assign Ci[3]=P[2]|G[2]&Ci[2];
assign Ci[4]=P[3]|G[3]&Ci[3];
endmodule


