verilog设计加法器

https://www.cnblogs.com/mxdon/p/11324582.html

概述

本文利用了硬件行为描述、数据流描述、结构描述三种方法分别写了几个加法器

一位半加法器

即两个一位的二进制数相加,得到其正常相加的结果的最后一位。

仿真波形图

 

硬件行为描述

设计文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module bjqxw(a,b,sum,cout);
    input a,b;
    output sum,cout;
    reg sum,cout;
    always @(a or b)
        begin
            case({a,b})
                2'b00:begin
                    sum=0;cout=0;
                    end
                2'b01:begin
                    sum=1;cout=0;
                    end
                2'b10:begin
                    sum=1;cout=0;
                    end
                2'b11:begin
                    sum=0;cout=1;
                    end 
            endcase
        end
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
module bjqxwsimu;
    reg a,b;
    wire sum,cout;
    bjqxw sl(a,b,sum,cout);
    initial
        begin
            a=0;b=0;
        end
    always #10 {a,b}={a,b}+1;
endmodule

结构描述

设计文件

1
2
3
4
5
6
module add(a,b,sum,cout);
    input a,b;
    output sum,cout;
    xor(sum,a,b);
    and(cout,a,b);
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
module add1;
    reg a,b;
    wire sum,cout;
    add ul(a,b,sum,cout);
    initial
    begin
        a=0;b=0;
    end
    always #10 {a,b}={a,b}+1;
endmodule

数据流描述

设计文件

1
2
3
4
5
6
7
endmodulemodule add3(a,b,sum,cout);
    input a,b;
    output sum,cout;
    wire sum,cout;
    assign sum=a^b;
    assign cout=a&b;
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module add1;
    reg ain,bin;
    reg clk;
    wire sum1,cout1;
    initial
    begin
        ain=0;bin=0;clk=0;
    end
    always #50 clk=~clk;
    always @(posedge clk)
    begin
        ain={$random}%2;
        #3 bin={$random}%2;
    end
    add3 ul(.a(ain),.b(bin),.sum(sum1),.cout(cout1));
endmodule

一位全加器

仿真波图

 

硬件行为描述

设计文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module qjq(a,b,cin,sum,cout);
    input a,b,cin;
    output sum,cout;
    reg sum,cout;
    always @(a or b or cin)
    begin
    case ({cin,a,b})
        3'b000:begin
            sum=0;cout=0;
        end
        3'b001:begin
            sum=1;cout=0;
        end
        3'b010:begin
            sum=1;cout=0;
        end
       3'b011:begin
            sum=0;cout=1;
        end
        3'b100:begin
            sum=1;cout=0;
        end
        3'b101:begin
            sum=0;cout=1;
        end
        3'b110:begin
            sum=0;cout=1;
        end
        3'b111:begin
            sum=1;cout=1;
        end
      endcase 
     end
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
module qjq1;
    reg a,b,cin;
    wire sum,cout;
    qjq ul(a,b,cin,sum,cout);
    initial
    begin
        a=0;b=0;cin=0;
    end
    always #10 {a,b,cin}={a,b,cin}+1;
endmodule

结构描述

设计文件

1
2
3
4
5
6
7
8
9
10
module qiq(a,b,cin,sum,cout);
    input a,b,cin;
    output sum,cout;
    wire q1,q2,q3;
    xor(sum,a,b,cin);
    or(q1,a,b);
    or(q2,b,cin);
    or(q3,a,cin);
    and(cout,q1,q2,q3);
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
module qjq1;
    reg a,b,cin;
    wire sum,cout;
    qiq ul(a,b,cin,sum,cout);
    initial
    begin
        a=0;b=0;cin=0;
    end
    always #10 {a,b,cin}={a,b,cin}+1;
endmodule

数据流描述

设计文件

1
2
3
4
5
module qjq(a,b,cin,sum,cout);
    input a,b,cin;
    output sum,cout;
    assign {sum,cout}=a+b+cin;
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
module qjqsimu;
    reg a,b,cin;
    wire sum,cout;
    qjq sl(a,b,cin,sum,cout);
    initial
        begin
            a=0;b=0;cin=a&b;
        end
    always #20 {a,b}={a,b}+1;
endmodule

四位全加器

数据流描述

设计文件

1
2
3
4
5
6
7
module qjq(a,b,cin,sum,cout);
    input [3:0] a,b;
    input cin;
    output [3:0] sum;
    output cout;
    assign {sum,cout}=a+b+cin;
endmodule

仿真结构图

 

仿真文件

1
2
3
4
5
6
7
8
9
10
11
12
module qjqsimu;
    reg [3:0] a,b;
    reg cin;
    wire [3:0] sum;
    wire cout;
    qjq sl(a,b,cin,sum,cout);
    initial
        begin
            a=4'b0000;b=4'b0000;cin=0;
        end
    always #20 {a,b}={a,b}+4'b0001;
endmodule

仿真波图

 

ps:将上述输入输出的字段长度对应修改,可得到相应数位的全加器数据流描述

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值