HDLBits学习笔记(66~72)

HDLBits学习笔记(66~72)

学习阶段:有问题发873727286@qq.com大家一起讨论。

题目66 Hadd

题干:Create a half adder. A half adder adds two bits (with no carry-in) and produces a sum and carry-out.

题目大意:构建一个半加器。输出两个输入的加法和进位。

题目分析:有两种方法
1、画出真值表
在这里插入图片描述
可以看出:cout = a&b ; sum=a^b;

2、直接用软件方法计算。

答案:

(1)
module top_module( 
    input a, b,
    output cout, sum );
    assign cout = a&b;
    assign sum  = a^b; 
endmodule
(2)
module top_module( 
    input a, b,
    output cout, sum );
    assign {cout,sum} = a+b;
endmodule

题目67 Hadd

题干:Create a full adder. A full adder adds three bits (including carry-in) and produces a sum and carry-out.

题目大意:构建一个全加器。

题目分析:同样的也有两种方法:
1、画出真值表:
在这里插入图片描述
可以看出:cout = a&b ; sum=a^b*cin;

2、用软件方法计算。

答案:

(1)
module top_module( 
    input a, b, cin,
    output cout, sum );
    assign cout =((~a)&b&cin)|((~b)&a&cin)|((~cin)&b&a)|(a&b&cin) ;
    assign sum  = a^b^cin;
endmodule
(2)
module top_module( 
    input a, b, cin,
    output cout, sum );
    assign {cout,sum} = a+b+cin;
endmodule

题目68 Adder3

题干:Now that you know how to build a full adder, make 3 instances of it to create a 3-bit binary ripple-carry adder. The adder adds two 3-bit numbers and a carry-in to produce a 3-bit sum and carry out. To encourage you to actually instantiate full adders, also output the carry-out from each full adder in the ripple-carry adder. cout[2] is the final carry-out from the last full adder, and is the carry-out you usually see.

题目大意:实例化三个一位全加器,实现三位全加器。

题目分析:实例化的运用。

答案:

module top_module( 
    input [2:0] a, b,
    input cin,
    output [2:0] cout,
    output [2:0] sum );
    fadd instance1(
        .a(a[0]),
        .b(b[0]),
        .cin(cin),
        .sum(sum[0]),
        .cout(cout[0])
    );
    fadd instance2(
        .a(a[1]),
        .b(b[1]),
        .cin(cout[0]),
        .sum(sum[1]),
        .cout(cout[1])
    );
    fadd instance3(
        .a(a[2]),
        .b(b[2]),
        .cin(cout[1]),
        .sum(sum[2]),
        .cout(cout[2])
    );  
endmodule

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

题目69 Exams/m2014 q4j

题干:Implement the following circuit:
在这里插入图片描述
题目分析:也是全加器的实例化,具体情况按照图来。

答案:

module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    wire [4:0] cout;
    assign cout[0] = 1'b0;
    assign sum[4] = cout[4]; 
	genvar i;
    generate
        for(i=0;i<4;i++)begin:add
            fadd ins(
                .a(x[i]),
                .b(y[i]),
                .cin(cout[i]),
                .sum(sum[i]),
                .cout(cout[i+1])
            );
        end
    endgenerate
endmodule

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

题目70 Exams/ece241 2014 q1c

题干:Assume that you have two 8-bit 2’s complement numbers, a[7:0] and b[7:0]. These numbers are added to produce s[7:0]. Also compute whether a (signed) overflow has occurred.

题目大意:有两个8位的补码相加,计算其结果s和溢出标志位。

题目分析:补码是有符号的。并且只有同号的输入相加才会产生溢出。当输入同号时,如果输入与输出不同号,此时就会产生溢出。
对于减法来说,如果被减数和结果符号相反,则产生溢出。

答案:

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])?((a[7]==s[7])?(0):(1)):(0);
endmodule

题目71 Adder100

题干:Create a 100-bit binary adder. The adder adds two 100-bit numbers and a carry-in to produce a 100-bit sum and carry out.、

题目大意:构建一个100位的加法器,该加法器有两个100位的输入信号和进位,输出100位的结果和进位。

题目分析:因为此题的要求用一行解决。就可以用assign直接赋值来实现。

答案:

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

题目72 Bcdadd4

题干:You are provided with a BCD (binary-coded decimal) one-digit adder named bcd_fadd that adds two BCD digits and carry-in, and produces a sum and carry-out.

题目大意:题目提供了1位BCD码加法器的模块,先要求实现4位的BCD加法器。

题目分析:位BCD码加法器结构实例化,和题68 69类似。

答案:

module top_module( 
    input [15:0] a, b,
    input cin,
    output cout,
    output [15:0] sum );
    wire [4:0] cout1;
    assign cout1[0] = cin;
    assign cout = cout1[4];
    genvar i;
    generate
        for(i=0;i<4;i++)begin:add
            bcd_fadd ins(
                .a(a[4*i+:4]),
                .b(b[4*i+:4]),
                .cin(cout1[i]),
                .sum(sum[4*i+:4]),
                .cout(cout1[i+1])
            );
        end
    endgenerate
endmodule
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值