HDLBits个人刷题详解合集8-Circuits-Combinational Logic-Arithmetic Circuits-HDBits题目分析

Hadd

Create a half adder. A half adder adds two bits (with no carry-in进位输入) and produces a sum and carry-out.

代码如下:

module top_module(

    input a, b,

    output cout, sum );

    assign {cout,sum} = a + b;

endmodule

Fadd

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

代码如下:

module top_module(

    input a, b, cin,

    output cout, sum );

    assign {cout,sum} = a + b + cin;

endmodule

Adder3

现在您已经知道如何构建完整的加法器,请创建 3 个实例以创建 3 位二进制波纹携带加法器。加法器将两个 3 位数字和一个转入数相加,以生成 3 位总和并进行。为了鼓励您实际实例化完整加法器,还要输出波纹携带加法器中每个完整加法器的携带。cout[2]是最后一个完整加法器的最终执行,也是您通常看到的执行。

代码如下:

module top_module(

    input [2:0] a, b,

    input cin,

    output [2:0] cout,

    output [2:0] sum );

    fulladder f0(a[0], b[0], cin, cout[0], sum[0]);

    fulladder f1(a[1], b[1], cout[0], cout[1], sum[1]);

    fulladder f2(a[2], b[2], cout[1], cout[2], sum[2]);

endmodule

module fulladder(

    input a, b, cin,

    output cout, sum );

    assign {cout,sum} = a + b + cin;

endmodule

Exams/m2014 q4j

代码如下:

module top_module (

    input [3:0] x,

    input [3:0] y,

    output [4:0] sum);

assign sum = x + y;

endmodule

Exams/ece241 2014 q1c

假设您有两个 8 位 2 的补码,a[7:0] 和 b[7:0]。这些数字相加以产生[7:0]。还要计算是否发生了(有符号)溢出。

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]);

    // assign s = ...

    // assign overflow = ...

Endmodule

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.

代码如下:

module top_module(

    input [99:0] a, b,

    input cin,

    output cout,

    output [99:0] sum );

    assign {cout,sum} = a + b + cin;

endmodule

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.

module bcd_fadd (

    input [3:0] a,

    input [3:0] b,

    input     cin,

    output   cout,

    output [3:0] sum );

Instantiate 4 copies of bcd_fadd to create a 4-digit BCD ripple-carry adder. Your adder should add two 4-digit BCD numbers (packed into 16-bit vectors) and a carry-in to produce a 4-digit sum and carry out.

代码如下:

module top_module (

    input [15:0] a, b,

    input cin,

    output cout,

    output [15:0] sum );

    wire [4:0] cin_mid;//设计一个中间变量

    assign cin_mid[0] = cin;

    generate

        genvar i;

        for(i=0; i<4; i++) begin:bcd_fadd_loop

            //使用名字式实例化的方式

            bcd_fadd bcd_fadd_inst (

                .a(a[4*i+3:4*i]),

                .b(b[4*i+3:4*i]),

                .cin(cin_mid[i]),

                .cout(cin_mid[i+1]),

                .sum(sum[4*i+3:4*i])

            );

        end

    endgenerate

    assign cout = cin_mid[4];

endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值