HDLbits——算术电路

算术电路

1 Hadd

半加器

module top_module( 
    input a, b,
    output cout, sum );
    assign sum=a^b;
    assign cout=a&b;
endmodule
//两种方法
module top_module( 
    input a, b,
    output cout, sum );
    assign {cout,sum}=a+b;
endmodule

2 Fadd

全加器

module top_module( 
    input a, b, cin,
    output cout, sum );
    assign {cout,sum}=a+b+cin;
endmodule

3 Adder3

module top_module( 
    input [2:0] a, b,
    input cin,
    output [2:0] cout,
    output [2:0] sum );
    add add1[2:0](.a(a[2:0]),.b(b[2:0]),.cin({cout[1:0],cin}),.cout(cout[2:0]),.sum(sum[2:0]));
endmodule
module add(input a,input b,input cin,output cout,output sum);
    assign {cout,sum}=a+b+cin;
endmodule

4 Exams/m2014 q4j

//输出结果错误
module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    wire [3:0] cout;
    add add1[3:0](.a(x[3:0]),.b(y[3:0]),.cin({cout[2:0],1'b0}),.cout({cout[3:0]}),.sum(sum[3:0]));
    assign sum={cout[4],sum};//最后的进位并没有输出,这行并没有运行
endmodule
module add(input a,input b,input cin,output cout,output sum);
    assign {cout,sum}=a+b+cin;
endmodule
//输出结果正确
module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    wire [3:0] cout;
    add add1[3:0](.a(x[3:0]),.b(y[3:0]),.cin({cout[2:0],1'b0}),.cout({sum[4],cout[2:0]}),.sum(sum[3:0]));//sum[4]需要包含到cout中
endmodule
module add(input a,input b,input cin,output cout,output sum);
    assign {cout,sum}=a+b+cin;
endmodule
///输出结果正确,可以直接相加
module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    assign sum=x+y;
endmodule

5 Exams/ece241 2014 q1c

本题讨论的是有符号数相加的溢出问题中,需要实现一个 2 进制 8bit 有符号数加法器,加法器将输入的两个 8bit数补码相加,产生相加之和以及进位。

module top_module (
    input [7:0] a,
    input [7:0] b,
    output [7:0] s,
    output overflow
); //
    assign s=a+b;
    assign overflow=(a[7]&b[7]&(~s[7]))|((~a[7])&(~b[7])&s[7]);
endmodule

6 Adder100

module top_module( 
    input [99:0] a, b,
    input cin,
    output cout,
    output [99:0] sum );
    assign {cout,sum}=a+b+cin;
endmodule

7 Bcdadd4

module top_module ( 
    input [15:0] a, b,
    input cin,
    output cout,
    output [15:0] sum );
    reg [3:0] cout1;
    bcd_fadd bcd_fadd1[3:0](.a(a[15:0]),.b(b[15:0]),.cin({cout1[2:0],cin}),.cout({cout,cout1[2:0]}),.sum(sum[15:0]));
endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NDLilaco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值