之前的文章《verilog学习:使用VCS仿真验证一个全加器》,示范了下全加器,这篇文章就展开说一下加法器。
不考虑低位的进位信号,将两个一位二进制数相加,只求本位和
被加数A | 加数B | 和数S | 进位CO |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 1 |
由表中可以看见,这种加法没有考虑低位来的进位,所以称为半加。
module half_adder(
input a, //第一个加数a
input b, //第二个加数b
output sum, //和
output cout //位
);
assign sum=a ^ b; //sum=a⊕b
assign cout=a & b; //cout=ab
endmodule
module half_adder(cout,sum,a,b);
output cout;
output sum;
input a,b;
wire cout,sum ;
//assign {cout,sum}=a+b;
assign cout = a & b;
assign sum = a ^ b;
endmodule
module half_adder(cout,sum,a,b);
output cout;
output sum;
input a,b;
wire cout,sum ;
assign {cout,sum}=a+b;
//assign cout = a & b;
//assign sum = a ^ b;
endmodule
`timescale 1ns/10ps
//`include "adder.v"
module adder_testbench;
reg a,b;
wire sum,cout;
integer i,j;
adder adder_te(
.sum ( sum ),
.cout ( cout),
.a ( a ),
.b ( b )
);
initial begin
a=0;b=0;
for(i=1;i<16;i=i+1)
#20 a=i;
end
initial begin
for(j=1;j<16;j=j+1)
#10 b=j;
end
initial begin
$monitor($time,,,"%d + %d ={%b,%d}",a,b,cout,sum);
#160 $finish;
end
endmodule
一位全加器的真值表如下图,其中Ai为被加数,Bi为加数,相邻低位来的进位数为Ci-1,输出本位和为Si。向相邻高位进位数为Ci
进位输入Ci-1 | 被加数Ai | 加数Bi | 本位和Si | 进位输出Ci |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 |
1 | 0 | 0 | 1 | 0 |
1 | 0 | 1 | 0 | 1 |
1 | 1 | 0 | 0 | 1 |
1 | 1 | 1 | 1 | 1 |
module full_add (a,b,cin,sum,cout);
input a,b,cin;
output sum,cout;
reg sum,cout;
always @(a or b or cin)begin
sum = a ^ b ^ cin;
cout = a & b |(cin&(a^b));
end
endmodule
module full_add (a,b,cin,sum,cout);
input a,b,cin;
output sum,cout;
reg sum,cout;
always @(a or b or cin)begin
sum = a ^ b ^ cin;
//cout = a & b |(cin&(a^b));
cout = (a & b)|(b & cin)|(a & cin);
end
endmodule
module full_add (a,b,cin,sum,cout);
input a,b,cin;
output sum,cout;
reg sum,cout;
reg m1,m2,m3;
always @(a or b or cin)begin
{cout,sum} = a + b + cin;
end
endmodule
`timescale 1ns/10ps
//`include "full_add.v"
module full_add_testbench;
reg a,b,cin;
wire sum,cout;
integer i,j,k;
full_add full_adder_te(
.sum ( sum ),
.cout ( cout),
.a ( a ),
.b ( b ),
.cin ( cin)
);
initial begin
a=0;
for(i=1;i<16;i=i+1)
#20 a=~a;
end
initial begin
b=0;
for(j=1;j<16;j=j+1)
#10 b=~b;
end
initial begin
cin=0;
for(k=1;k<16;k=k+1)
#5 cin=~cin;
end
initial begin
$monitor($time,,,"%d + %d + %d ={%b,%d}",a,b,cin,cout,sum);
#80 $finish;
end
endmodule