4bit超前进位加法器的逻辑表达式如下:
中间变量
和:,进位:
请用Verilog语言采用门级描述方式,实现此4bit超前进位加法器,接口电路如下:
`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
直接把公式翻译就可以了
`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 ); and ad0(ad0_o,A_in[0],B_in[0]), ad1(ad1_o, A_in[1],B_in[1]), ad2(ad2_o, A_in[2],B_in[2]), ad3(ad3_o, A_in[3],B_in[3]), ad4(ad4_o, xr0_o,C_1), ad5(ad5_o,C0,xr1_o), ad6(ad6_o,C1,xr2_o), ad7(ad7_o,C2,xr3_o); xor xr0(xr0_o,A_in[0],B_in[0]), xr1(xr1_o,A_in[1],B_in[1]), xr2(xr2_o,A_in[2],B_in[2]), xr3(xr3_o,A_in[3],B_in[3]), xr4(S[0],xr0_o,C_1), xr5(S[1],xr1_o,C0), xr6(S[2],xr2_o,C1), xr7(S[3],xr3_o,C2); &nbs***bsp; or0(C0,ad0_o,ad4_o), or1(C1,ad1_o,ad5_o), or2(C2,ad2_o,ad6_o), or3(C3,ad3_o,ad7_o); assign CO = C3; endmodule
C\AB | 00 | 01 | 11 | 10 |
0 | | | 1 | |
1 | | 1 | 1 | 1 |
module full_add ( input a, input b, input c, output s, output co ); assign s = (a & b & c) | (~a & ~b & c) | (~a & b & ~c) | (a & ~b & ~c); assign co = (a & b) | (a & c) | (b &c); endmodule总结,4bit超前进位加法只需要将单bit加法器级联即可实现。
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] C; full_add u_add0( .a (A_in[0]), .b (B_in[0]), .c (C_1 ), .s (S[0] ), .co (C[0] ) ); full_add u_add1( .a (A_in[1]), .b (B_in[1]), .c (C[0] ), .s (S[1] ), .co (C[1] ) ); full_add u_add2( .a (A_in[2]), .b (B_in[2]), .c (C[1] ), .s (S[2] ), .co (C[2] ) ); full_add u_add3( .a (A_in[3]), .b (B_in[3]), .c (C[2] ), .s (S[3] ), .co (C[3] ) ); assign CO = C[3]; endmodule
`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
`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 [3:0] c_o; assign CO = c_o[3]; generate genvar i; for(i=0; i<4; i=i+1) begin: cl_adder assign g[i] = A_in[i] & B_in[i]; assign p[i] = A_in[i] ^ B_in[i]; if(i==0) begin assign S[i] = p[i] ^ C_1; assign c_o[i] = g[i] | p[i] & C_1; end else begin assign S[i] = p[i] ^ c_o[i-1]; assign c_o[i] = g[i] | p[i] & c_o[i-1]; end end endgenerate endmodule
`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] C;//每一位上获得的前一时刻的进位 assign C[0] = C_1; genvar i; generate for (i = 0;i<4;i=i+1) begin:process_1 assign S[i] = A_in[i] ^ B_in[i] ^ C[i]; assign C[i+1] = (A_in[i] & B_in[i]) || ((A_in[i] ^ B_in[i]) & C[i]); end endgenerate assign CO = C[4]; endmodule
`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
`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
`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 [2:0]CO_temp; lca_1 lca_1_U1( .A_in(A_in[0]), .B_in(B_in[0]), .C_in(C_1), .CO(CO_temp[0]), .S(S[0]) ); lca_1 lca_1_U2( .A_in(A_in[1]), .B_in(B_in[1]), .C_in(CO_temp[0]), .CO(CO_temp[1]), .S(S[1]) ); lca_1 lca_1_U3( .A_in(A_in[2]), .B_in(B_in[2]), .C_in(CO_temp[1]), .CO(CO_temp[2]), .S(S[2]) ); lca_1 lca_1_U4( .A_in(A_in[3]), .B_in(B_in[3]), .C_in(CO_temp[2]), .CO(CO), .S(S[3]) ); endmodule module lca_1( input A_in, input B_in, input C_in, output CO, output S ); wire G; wire P; assign G = A_in & B_in; assign P = A_in ^ B_in; assign S = P ^ C_in; assign CO = G | (P & C_in); endmodule
`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题目要求用门级电路,起码给出了式子而且不太复杂,比上一题人性化多了
`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 g1,g2,g3,g4; wire p1,p2,p3,p4; wire c1,c2,c3,c4; assign g1=A_in[0]&B_in[0]; assign g2=A_in[1]&B_in[1]; assign g3=A_in[2]&B_in[2]; assign g4=A_in[3]&B_in[3]; assign p1=A_in[0]^B_in[0]; assign p2=A_in[1]^B_in[1]; assign p3=A_in[2]^B_in[2]; assign p4=A_in[3]^B_in[3]; assign c1=g1|(p1&C_1); assign c2=g2|(p2&(g1|(p1&C_1))); assign c3=g3|(p3&(g2|(p2&(g1|(p1&C_1))))); assign CO=g4|(p4&g3|(p3&(g2|(p2&(g1|(p1&C_1)))))); assign S[0]=p1^C_1; assign S[1]=p2^(g1|(p1&C_1)); assign S[2]=p3^(g2|(p2&(g1|(p1&C_1)))); assign S[3]=p4^(g3|(p3&(g3|(p3&(g2|(p2&(g1|(p1&C_1)))))))); endmodule
`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 c0, c1, c2; wire g0, g1, g2, g3; wire p0, p1, p2, p3; wire pc0, pc1, pc2, pc3; and g0(g0, A_in[0], B_in[0]), g1(g1, A_in[1], B_in[1]), g2(g2, A_in[2], B_in[2]), g3(g3, A_in[3], B_in[3]); xor p0(p0, A_in[0], B_in[0]), p1(p1, A_in[1], B_in[1]), p2(p2, A_in[2], B_in[2]), p3(p3, A_in[3], B_in[3]); and pc0(pc0, p0, C_1), pc1(pc1, p1, c0), pc2(pc2, p2, c1), pc3(pc3, p3, c2); //C out or c0(c0, g0, pc0), c1(c1, g1, pc1), c2(c2, g2, pc2), c3(CO, g3, pc3); //S out xor s0(S[0], p0, C_1), s1(S[1], p1, c0), s2(S[2], p2, c1), s3(S[3], p3, c2); endmodule照着公式无脑写
wire [4:0]C;
assign C[0]=C_1;
assign p=A_in|B_in;
assign g=A_in&B_in;
assign C[1]=g[0]|(p[0]&C[0]);
assign C[2]=g[1]|(p[1]&C[1]);
assign C[3]=g[2]|(p[2]&C[2]);
assign C[4]=g[3]|(p[3]&C[3]);
assign CO=C[4];
assign S=A_in^B_in^C[3:0];
endmodule