module ha(a,b,s,c);
input a,b;
output s,c;
assign s=a^b;
assign c=a&b;
endmodule
module fa(cin,a,b,s,cout);
input cin,a,b;
output s,cout;
assign s=cin^a^b;
assign cout=(a&b)|(a|b)&cin;
endmodule
module rcadder4(a,b,sum,c);
input [3:0] a,b;
output [3:0] sum;
output c;
wire [2:0] co;
ha U1(a[0],b[0],sum[0],co[0]);
fa U2(co[0],a[1],b[1],sum[1],co[1]);
fa U3(co[1],a[2],b[2],sum[2],co[2]);
fa U4(co[2],a[3],b[3],sum[3],c);
endmodule
`timescale 1ns/100ps
`include "rcadder4.v"
module testadder4;
reg [3:0] a,b;
wire [3:0] sum;
wire c;
rcadder4 u1(a,b,sum,c);
initial begin
$dumpfile("rcadder4.vcd");
$dumpvars;
end
integer i,j;
initial begin
for(i=0;i<16;i=i+1) begin
#0 a=i;
for(j=0;j<16;j=j+1) begin
#0 b=j;
#10;
end
end
end
endmodule
===============================================================
module ha(a,b,s,c);
input a,b;
output s,c;
assign #1 s=a^b;
assign #1 c=a&b;
endmodule
module fa(cin,a,b,s,cout);
input cin,a,b;
output s,cout;
assign #1 s=cin^a^b;
assign #1 cout=(a&b)|(a|b)&cin;
endmodule
module rcadder4(a,b,sum,c);
input [3:0] a,b;
output [3:0] sum;
output c;
wire [2:0] co;
ha U1(a[0],b[0],sum[0],co[0]);
fa U2(co[0],a[1],b[1],sum[1],co[1]);
fa U3(co[1],a[2],b[2],sum[2],co[2]);
fa U4(co[2],a[3],b[3],sum[3],c);
endmodule
module cla4(a,b,sum,c);
input [3:0] a,b;
output [3:0] sum;
output c;
wire [2:0] co;
assign #1 co[0]=a[0]&b[0];
assign #1 co[1]=(a[1]&b[1])|((a[1]|b[1])&(a[0]&b[0]));
assign #1 co[2]=(a[2]&b[2])|((a[2]|b[2])&((a[1]&b[1])|((a[1]|b[1])&(a[0]&b[0]))));
assign #1 c=(a[3]&b[3])|((a[3]|b[3])&((a[2]&b[2])|((a[2]|b[2])&((a[1]&b[1])|((a[1]|b[1])&(a[0]&b[0]))))));
assign #1 sum[0]=a[0]^b[0];
assign #1 sum[1]=a[1]^b[1]^co[0];
assign #1 sum[2]=a[2]^b[2]^co[1];
assign #1 sum[3]=a[3]^b[3]^co[2];
endmodule
`timescale 1ns/100ps
`include "rcadder4.v"
`include "claadder4.v"
module testadder4;
reg [3:0] a,b;
wire [3:0] sum1;
wire c1;
wire [3:0] sum2;
wire c2;
rcadder4 u1(a,b,sum1,c1);
cla4 u2(a,b,sum2,c2);
initial begin
$dumpfile("rcadder4.vcd");
$dumpvars;
end
integer i,j;
initial begin
for(i=0;i<16;i=i+1) begin
#0 a=i;
for(j=0;j<16;j=j+1) begin
#0 b=j;
#10;
end
end
end
endmodule
沒有留言:
張貼留言
文章有誤或有問題麻煩您留言告知! 謝謝您~~